Fire Tag after Datalayer Values have changed

倖福魔咒の 提交于 2019-12-02 15:29:05

问题


We do have a Datalayer which looks like this:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
Example1: 'false'
Example2: 'false'
Example3: 'false'
Example4: 'false'
Example5: 'false'
});

We want to fire a Tag after the some values have changed, without a pagereload. When Example 1 and Example 3 are having the state true, a tag should be fired immediately.

Someone told me to add an Event to the datalayer for example:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'exampleHappened',
Example1: 'true'
});

Is it possible to add 5 events to the Datalayer without problems?

Edit: Ive splited the datalayer in 5 datalayers, each of them have

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'exampleHappened',
Example1: 'true'
});

Last thing: how to I setup the Tagmanager setting to fire when the Event has been fired?

thank you in advance


回答1:


Yes, you need an event - GTM overwrites the native "push" method on the datalayer array to scan incoming objects for the "event" keyword. Only if the event keyword is present GTM will update its internal state and make the new values available for use in tags and triggers. However you cannot use several events within a single push.

However your first example requires only a single event:

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
"Example1": "false",
"Example2": "false",
"Example3": "false",
"Example4": "false",
"Example5": "false",
"event":"examplesHappened"
});

If you insist on splitting this up into several pushes you could use a trigger group - first create a custom event trigger with your "exampleAdded" event, then create a trigger group and add the trigger five times:

Note that you can add a trigger more than once. By adding a trigger more than once into the group, that particular trigger must fire as many times as it has been added to the group for the Trigger Group to work.

(Source)

The downside is that this will only work once per page.

If your pushed are created by some sort of loop you could just the increment to number your events:

for(i=1;i<6;i++) {
 var obj = {};
 obj["example"+i] = "false";
 obj["event"] = "exampleHappened_"+i
 dataLayer.push(obj);
}

and then just fire on the event "exampleHappened5". This would work multiple times.



来源:https://stackoverflow.com/questions/57610373/fire-tag-after-datalayer-values-have-changed

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