GTM - 一个用于多个容器的数据层 [英] GTM - One datalayer for multiple containers

查看:112
本文介绍了GTM - 一个用于多个容器的数据层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在使用Google跟踪代码管理器已经有好几个星期了。最近我有一个特殊的要求,我没有找到答案。几个容器可能共享同一个数据层吗?在两个容器的Js代码中,我试图给与datalayer相同的名称:结果相当令人惊讶......每个容器中的所有标记都被执行两次。

具体来说,我尝试使用两个不同容器中的两个标签发送事件。我希望避免在我的onclick上进行最大数量的无数次呼叫(因此有了一个单一的数据层)

解决方案

可以为多个容器使用相同的dataLayer。也可以通过第一个容器(作为标签)包含第二个容器。



但是,所有页面规则将为包含在页。所以如果你有3个容器,每一个在所有页面上触发的标签将会被触发3次。
(其他事件也是如此,例如gtm.js...)

我们为这个问题找到了一个简单的解决方案。只需在每个页面上添加一个page_loaded事件并相应地更改All pages规则即可:

 < script> 
var dataLayer = [];
< / script>
<! - Google跟踪代码管理器 - >
<! - 结束Google跟踪代码管理器 - >
< script>
dataLayer.push({event:page_loaded});
< / script>

page_loaded事件只会触发一次,与页面上使用的容器数量无关。 p>

I use Google Tag Manager for several weeks now. Recently I had a special request and I have not found an answer. Is it possible that several containers share the same datalayer? In the Js code of two containers, I tried to give the same name to datalayer : the result is rather surprising ... All tags in each containers are executed twice.

Specifically, I try to send an event in two tags which are in two different containers. I'd like to avoid having maximum countless number call on my onclick (hence the idea of ​​having a single data layer)

解决方案

It is possible to use the same dataLayer for multiple containers. It is also possible to include a second container through the first one (As a tag).

But the "All pages" rule will fire once for each container included on the page. So if you have 3 containers, every tag fired on "All pages" will be fired 3 times. (The same is true for other events like "gtm.js"...)

We found a simple solution for this problem. Just include a "page_loaded" event on every page and change the "All pages" rule accordingly:

<script>
var dataLayer = [];
</script>
<!-- Google Tag Manager -->
<!-- End Google Tag Manager -->
<script>
dataLayer.push({"event": "page_loaded"});
</script>

The page_loaded event will only fire once, independent from the number of containers used on the page.

这篇关于GTM - 一个用于多个容器的数据层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆