Max*_*Max 5 google-tag-manager
我有以下代码:
dataLayer.push({event: 'A',eventAttributes: { attrA: 'a' }});
dataLayer.push({event: 'B',eventAttributes: { attrB: 'b' }});
dataLayer.push({event: 'C',eventAttributes: { attrC: 'c' }});
Run Code Online (Sandbox Code Playgroud)
和一个完全空的 GTM 容器(没有标签、没有触发器、没有变量)。
当我检查Data Layer
GTM 调试选项卡时,我看到以下内容:
{
gtm: {start: 1448552543040, uniqueEventId: 1448552543091},
event: 'A',
eventAttributes: {attrA: 'a'}
}
{
gtm: {start: 1448552543040, uniqueEventId: 1448552543092},
event: 'B',
eventAttributes: {attrA: 'a', attrB: 'b'}
}
{
gtm: {start: 1448552543040, uniqueEventId: 1448552543092},
event: 'C',
eventAttributes: {attrA: 'a', attrB: 'b', attrC: 'C'}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,GTM 将值合并到eventAttributes
对象而不是覆盖对象。
Q1:GTM为什么要这么做?
Q2:有没有办法可以防止这种情况发生(除了将每个对象属性定义为单独的变量之外)?
仅供参考,我eventAttributes
在数据层中使用该对象,因为我想简单地重新使用其中的任何内容作为我的基于事件的工具(Mixpanel、KISSmetrics...)的属性。对于上述问题,如果我必须显式声明 DataLayer 和 GTM 中的所有变量,我只是看不到任何方法来扩展 Mixpanel/KISSmetrics 的 GTM 实现。
更新
在控制台本身中查看 dataLayer 后,值似乎已正确推送且未合并:
所以一定是在读取dataLayer的时候GTM决定将它们合并!
对问题 1 的回答:
GTM 这样做是因为这是 DataLayer 的预期行为: https ://github.com/google/data-layer-helper
添加Kitravee Siwatkittisuk的 Q2 答案以使这篇文章更加详尽:
问题 2 的答案:
感谢 Kitravee Siwatkittisuk
将 _clear 设置为 true以防止默认递归合并
dataLayer.push({items: [1], _clear: true})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2893 次 |
最近记录: |