matomo-org / tag-manager

Free Open Source Matomo Tag Manager - A simple way to manage and maintain all of your (third-party) tags on your website.
https://matomo.org
GNU General Public License v3.0
170 stars 58 forks source link

dataLayer variables can be overwritten if push happens before the tag manager loads #799

Closed DarkLink571 closed 1 month ago

DarkLink571 commented 4 months ago

In some cases, datalayer pushes are made before the Tag Manager has loaded. These pushes may include event calls and setting of datalayer variables. But the pushes are not handled successively and it is possible for a datalayer variable to not have the intended value at the time the event is resolved.

Example: -Create a datalayer variable -Create a customEvent trigger with the variable as filter -Create any tag that uses that trigger -On the website run a script BEFORE the tag manager loads. This script pushes the event twice, the first time only the event, the second time with a fitting value for the variable. -The tag will fire twice, despite the condition should have been only fulfilled once.

snake14 commented 4 months ago

Hi @DarkLink571 . Thank you for taking the time to create this issue. Could we please get a little more information? For example, the script executing before MTM loads, is it using _mtm.push(...) or is it using the GTM format dataLayer = [{...}];?

DarkLink571 commented 4 months ago

Hi @snake14. I used the _mtm dataLayer. Here is an example _mtm = _mtm || []; _mtm.push({event: "testEvent"}); _mtm.push({{event: "testEvent", dlvariable: "testValue"})

snake14 commented 4 months ago

Thank you for the example @DarkLink571 . In that case, I would say that it makes sense that the event is triggered twice as the dataLayer value would already be set when both events are triggered after MTM finishes loading. If those events were pushed after MTM were loaded, I would expect only the second event to trigger due to the dataLayer condition. You may want to make sure that the events aren't pushed until after MTM has loaded if you don't want the first event to trigger, like maybe pushing them as part of a Custom HTML tag. Any other recommendations @AltamashShaikh ?

AltamashShaikh commented 4 months ago

akes sense that the event is triggered twice as the dataLayer value would already be set when both events are triggered after MTM finishes lo

@snake14 CustomHTML should do the trick :+1:

snake14 commented 3 months ago

@DarkLink571 Does using a Custom HTML tag work for you? Please refer to previous comment.

AltamashShaikh commented 2 months ago

@DarkLink571 any update ?

AltamashShaikh commented 1 month ago

Closing this due to inactivity, @DarkLink571 Feel free to comment on this issue, if you are still facing any issues.