简体   繁体   English

稍后如何将数据“键和值”从网站推送到 Google 标签管理器数据?

[英]How to push data "key and value" from website to Google Tag Manager Data Later?

First of all I am new to Google Tag Manager so apologies for my explanation.首先,我是 Google 标签管理器的新手,因此对我的解释深表歉意。

I am trying to implement a Google Tag Manager to my website.我正在尝试为我的网站实施 Google 标签管理器。 I am not trying to use too many features but the main thing I am trying to do is load my website with a query string parameter:我不想使用太多功能,但我想做的主要事情是使用查询字符串参数加载我的网站:

Example: ( key & value : www.myweb.com?key=key&value=value )示例:(键和值www.myweb.com? key=key&value=value

which then I am trying to create a function that pushes the key and value to the Google Tag Manager Data Layer然后我尝试创建一个将键和值推送到 Google 标签管理器数据层的函数

I have added the following code:我添加了以下代码:

$(document).ready(function (){

    const urlSearchParams = new URLSearchParams(window.location.search);
    const params = Object.fromEntries(urlSearchParams.entries());

    console.log(params)

    window.dataLayer = window.dataLayer || [];
    dataLayer.push({params.key:params.value})
    //window.dataLayer.push({"Event":"dog"})
    
})

I have an index.html and have added the head script and body script from google tag manager (the page is hosted online with Google Firebase).我有一个 index.html 并添加了来自 google 标签管理器的头脚本和正文脚本(该页面通过 Google Firebase 在线托管)。 I am not sure how to send the key value to Google Tag Manager Data Layer but the main thing is when I do send it, where do I find this information on Google Tag Manager , do I need to create a variable or something on GLM ?我不确定如何将键值发送到 Google 标签管理器数据层,但主要是当我发送它时,我在哪里可以找到 Google 标签管理器上的这些信息,我是否需要在 GLM 上创建一个变量或其他东西

Thank you in advance.先感谢您。

Please do let me know if you require anymore information如果您需要更多信息,请告诉我

This is not a good solution - by writing your own javascript function to capture query strings you are basically negating the purpose of GTM.这不是一个好的解决方案 - 通过编写自己的 javascript 函数来捕获查询字符串,您基本上否定了 GTM 的目的。 Use a "Url" type variable instead, set "component type" to "query string" and in the "query key" input field enter the name of your parameter.改用“Url”类型变量,将“组件类型”设置为“查询字符串”,然后在“查询键”输入字段中输入参数名称。

url类型变量配置

But to answer your question (even if your approach for that particular problem is not good), yes, you need to create a variable to read data from the dataLayer.但是要回答您的问题(即使您针对该特定问题的方法不好),是的,您需要创建一个变量来从 dataLayer 读取数据。 If you want to read a dataLayer value, you need to create a dataLayer variable with the key whose value you want to retrieve.如果要读取 dataLayer 值,则需要使用要检索其值的键创建一个 dataLayer 变量。

数据层变量配置

There is basically no use case to change the dataLayer version.基本上没有用例可以更改 dataLayer 版本。 You can specify a default value, in case the key does not exist in the dataLayer, else the variable will be undefined.您可以指定默认值,以防数据层中不存在键,否则变量将是未定义的。

Note that you need to push an event along with any new or updated key/value pairs, because the "event" key tells GTM to update its internal state;请注意,您需要将事件与任何新的或更新的键/值对一起推送,因为“事件”键告诉 GTM 更新其内部状态; else the pushed values will be inaccessible.否则推送的值将无法访问。

There are some events that occur "naturally" - eg you do not need ab event in the dataLayer to capture values with the Url type variable, because in most cases there will be a pageload event (other "built in" events are DOM ready, window loaded, and click and submit if you have any of the built-in click or form variables enabled).有一些事件是“自然”发生的——例如,您不需要 dataLayer 中的 ab 事件来捕获具有 Url 类型变量的值,因为在大多数情况下会有一个页面加载事件(其他“内置”事件是 DOM 就绪的,窗口已加载,如果您启用了任何内置的单击或表单变量,则单击并提交)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM