0
votes

I have a Google Tag Manager tag configured to fire on all page views. However, on some pages, I may need to disable the tag based on internal data. To me, this sounds like a job for the dataLayer.

My initial thought was to create a dataLayer variable called "disableGTMTag" and give it a value to validate against. Then GTM could evaluate that value and take appropriate action.

So, in the dataLayer I created a disableGTMTag variable and hard-coded it to always have a value of "google" (for testing purposes). Running the page in GTM debug mode, I can confirm that the disableGTMTag variable is present in the dataLayer with the value "google".

To set up the tag, I updated the tag's trigger to fire only when disableGTMTag does not contain "google". But the tag still fired. I then tried setting the trigger to fire on All Pages but added an exception when disableGTMTag contains "google". The tag still fired.

My understanding is that a trigger exception must be the same type as the firing trigger. But I went ahead and created a secondary exception for a custom event that matches the regex ".*" that looked at the disableGTMTag value. Still, the tag fired when the page loaded.

This seems like a straightforward thing: always run this tag, unless this dataLayer variable tells you not to. But it's just not working for me.

Is there a different approach to restrict page view firing? How do I prevent page view-based tags from firing using a dataLayer variable?

1
What do you see in the debug mode, when you open the given tag within the 'Tags fired' block, and check the firing and/or blocking conditions. That should give you a clue, what value was seen by GTM, and what was matching or not your conditions. - kgrg

1 Answers

0
votes

I've found a solution. The recommended way to reference the dataLayer is as:

window.dataLayer = window.dataLayer || []

But I found that when I used this convention, my Page View event fired first and then all my data messages happened after making the dataLayer values unavailable. Changing it to:

window.dataLayer = []

forced the messages to happen before the page view event, making the dataLayer values available to be evaluated. I understand that this is going to re-declare the dataLayer, but this worked.