GTM - One datalayer for multiple containers

柔情痞子 提交于 2019-12-10 11:28:11

问题


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)


回答1:


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.




回答2:


This might be obvious (and I haven't tested it) but have you tried to make a copy of your dataLayer ? If you just add below your datalayer variable an assignment

dataLayer2 = dataLayer 

and configure you second container to user dataLayer2 ? That would surely be a PITA with dynamically pushed variables, but everything that is rendered at page load should work.




回答3:


One way that seems to work for us so far is to rename the datalayer variable of the second container like Eike said. GTM allows you to change the name of the variable when it initializes as explained here: https://developers.google.com/tag-manager/devguide#renaming

To track events on both containers you will have to manually push to each:

var data = {some:'data'};

dataLayer.push(data);
otherDataLayer.push(data);

You can have different triggers on each of the container for selectively fire tracking events to GA or other.



来源:https://stackoverflow.com/questions/17718483/gtm-one-datalayer-for-multiple-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!