- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正如标题所说,我需要在操作完成后将变量传递给 GTM 数据层。更具体地说,我想在日期选择器中选择日期后将游览日期传递给 GTM 层。
我不知道怎样做才是正确的。现在我有以下数据层脚本:
<script >
var dataLayer = [({
"customerLoggedIn": 0,
"customerId": 0,
"customerGroupId": "1",
"customerGroupCode": "GENERAL",
"productId": "6",
"productName": "Big Loop Boat Tour",
"productSku": "boat-tour",
"productPrice": "0.00",
"productPriceExcludingTax": "0.00",
"productTax": "0.00",
"productTaxRate": 0,
"productGender": false,
"pageType": "catalog\/product\/view"
})];
dataLayer.push({
'ecommerce': {
"detail": [{
"id": "6",
"name": "Big Loop Boat Tour",
"sku": "boat-tour",
"price": 0,
"priceexcludingtax": "0.00",
"tax": "0.00",
"taxrate": 0,
"brand": "",
"gender": false,
"category": null,
"children": []
}]
}
});
(function(w, d, s, l, i) {
if (i == '') {
return console.log('No GTM ID provided');
}
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'XXXXXXXX');
</script>
我应该在 dataLayer 数组中添加额外的变量吗?比如说 selectedDate,它将在页面加载时未定义。
var dataLayer = [({
"customerLoggedIn": 0,
"customerId": 0,
"customerGroupId": "1",
"customerGroupCode": "GENERAL",
"productId": "6",
"productName": "Big Loop Boat Tour",
"productSku": "boat-tour",
"productPrice": "0.00",
"productPriceExcludingTax": "0.00",
"productTax": "0.00",
"productTaxRate": 0,
"productGender": false,
"pageType": "catalog\/product\/view"
"selectedDate" : ""
})];
将 javascript 函数添加到页面以获取选定的日期并将其写入变量 $date.selected
。
并使用此自定义脚本在日期选择器点击触发的 GTM 中创建自定义标签
<script>
dataLayer.push({"selectedDate": $date.selected});
</script>
这看起来对吗?
或者我可以像这样在主数据层脚本中传递变量吗?
<script >
var dataLayer = [({
"customerLoggedIn": 0,
"customerId": 0,
"customerGroupId": "1",
"customerGroupCode": "GENERAL",
"productId": "6",
"productName": "Big Loop Boat Tour",
"productSku": "boat-tour",
"productPrice": "0.00",
"productPriceExcludingTax": "0.00",
"productTax": "0.00",
"productTaxRate": 0,
"productGender": false,
"pageType": "catalog\/product\/view",
"selectedDate": $date.selected
})];
dataLayer.push({
'ecommerce': {
"detail": [{
"id": "6",
"name": "Big Loop Boat Tour",
"sku": "boat-tour",
"price": 0,
"priceexcludingtax": "0.00",
"tax": "0.00",
"taxrate": 0,
"brand": "",
"gender": false,
"category": null,
"children": []
}]
}
});
(function(w, d, s, l, i) {
if (i == '') {
return console.log('No GTM ID provided');
}
w[l] = w[l] || [];
w[l].push({
'gtm.start': new Date().getTime(),
event: 'gtm.js'
});
var f = d.getElementsByTagName(s)[0],
j = d.createElement(s),
dl = l != 'dataLayer' ? '&l=' + l : '';
j.async = true;
j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'XXXXXXXX');
</script>
最佳答案
无需在开始时将变量定义为未定义(或空字符串)。当您的操作完成时,只需将您想要的值推送到 dataLayer
。对于您的情况,由于用户可能会改变主意并再次更改日期选择器值,因此可能会在 dataLayer
中多次推送新日期。
每次您向 dataLayer
添加内容时,都会推送一条新的“消息”。将 dataLayer
视为包含您推送的所有消息的数组。
在页面加载时初始化只会添加一个没有值的变量条目。
关于javascript - 特定操作后将变量传递给 GTM 数据层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46792655/
几周前,我将我的 Google 标签管理器容器迁移到 GTM v2(Android 和 iOS)。自从我这样做以来,我一直无法设置任何移动分析实验。 我已将容器连接到分析 View ,并在创建实验时选
我遇到了问题,我似乎无法找到解决方法。情况如下营销部门想整合GTM。这很好,因为他们希望完全控制他们希望集成的不同第三方提供商。但是为了实现这一点,他们希望能够跟踪页面上的所有点击。 他们告诉我,目前
我想创建一个postgres-xl 集群。该集群包括 5 个节点、1 个 GTM、2 个 Coordinator 和 2 个 Datanode。以下是节点的详细信息 GTM: hostname=l
我正在客户网站上实现 Google 跟踪代码管理器脚本。 我遇到了一个随机问题:dataLayer 没有获得它的 gtm.dom 和 gtm.load 对象。它只有两个对象,一个包含我正在传递的一堆变
这是我在这个网站上的第一个问题,我很好奇是否有人可以帮助我。在 Google 跟踪代码管理器中,我尝试使用一些 jQuery 设置自定义 JavaScript 变量,该变量应返回特定数组中所有产品价格
我在我的 chrome 扩展页面中添加了这个 gtm 代码(由内容脚本和 iframe 注入(inject)) // (function(w,d,s,l,i){w[l]=w[l]||[];w[l].
在 GTM 中编写一个小的自定义 javascript 宏以在单击出站链接时设置类别值。大多数内部链接都是相对 URL。 function(){ var eventCat = ""; if({{ele
我在任何地方都找不到这个问题的答案。我正在尝试在 Google 跟踪代码管理器中设置触发器和事件标记,以便在内容存在时触发。本质上,我正在 Google Analytics 中为产品 View 创建一
坐在一个我正忙着标记的网站上,上面有这个凌乱的小面包屑...... li class="home" itemprop="itemListElem
我的目标是在 Firebase Analytics 中获取应用数据,在 Google Universal Analytics 中获取其他自定义数据和应用数据。 我的问题是我是否在我的应用上安装 Fir
GTM 已启动并正在运行,主 UA 标记与 ClickListener 标记一起就位。 为了减少宏的数量,我使用 dataLayer 变量宏来表示事件类别、操作、标签、值和交互,因此它们可以用于许多规
为什么我的 GA View 中没有任何数据?编译 'com.google.android.gms:play-services-location:9.4.0' 所有写入/复制都像手册中一样(https:
我正在尝试跟踪使用 GTM 单击按钮后出现的元素,但使用元素可见性触发器根本不会触发。我为一些点击设置了 GTM,它运行良好,但我对这个可见性触发器感到困惑。我尝试切换 Observe DOM cha
我正在尝试在 GTM 中设置一个悬停事件标记,而不对页面上的代码进行任何更改,因此纯粹是通过 GTM。 我尝试使用 GTM Hover Recipe来自 Bounteous,但我似乎无法触发自定义事件
我有Pageview Google 标签管理器中跟踪 SPA 页面浏览量的标签,与 this guide 中描述的标签相同.基本上它是 Universal Analytics 与在 History C
在网站 GTM 实现中,您可以使用 javascript 创建自己的自定义标签,因此只需使用 GTM 界面即可轻松设置对您自己的系统的 Web 服务调用等。 现在对于 Firebase - GTM 实
我正在尝试发送 screen-view hits至Google Analytics使用 Google Tag Manager ,但在实时 View 中,屏幕名称值显示为 (not set) .实际的屏
我目前有一个带有相关文章元素的表单,该元素在填写表单主题时建议用户相关文章。 我正在 try catch 单击相关文章的所有表单提交。 我创建了 2 个触发器和 2 个标签: 一旦用户点击其中一篇相关
我正在根据 google documentation 实现购买跟踪. 我对购买标签配置有一个小问题。 如果我使用提供的配置创建标签: Tag type : Universal Analytics Tr
我使用 Google 标签管理器已经有几个星期了。最近我有一个特殊的要求,我还没有找到答案。是否有可能多个容器共享同一个数据层?在两个容器的Js代码中,我试图给datalayer一个同名:结果比较出人
我是一名优秀的程序员,十分优秀!