GTM - 一个用于多个容器的数据层 [英] GTM - One datalayer for multiple containers
问题描述
具体来说,我尝试使用两个不同容器中的两个标签发送事件。我希望避免在我的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屋!