A while ago, I posted #GTMTips where I detailed the steps you can take to opt out of Google Analytics tracking and the DoubleClick redirect you often track. It was a fun exercise, but because it relies on blocking requests on a tag-by-tag basis (using
customTask), it can be a chore to handle in large containers.
In this article, we will continue the topic of opting out of Google Analytics tracking by making use of the solution provided by the tool itself. To make it work, we’ll use a custom HTML tag with some tag sequences.
Simmer . Newsletter
Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava right in your inbox!
Tip 68: Opt out of Google Analytics tracking with a global variable
The trick is to set a global variable before The first GA tag on the page is activated. This is critical, because the global variable must be created before the tracking object is created. So, unfortunately,
customTask It won’t work this time (I know, big disappointment!).
The variable itself is very easy to create. It should look like this:
window['ga-disable-UA-XXXXXX-Y'] = true
UA-XXXXXX-Y It is the tracking ID that you want to block for all subsequent GA requests sent on the page. You can create multiple global variables like this, one for each tracking ID you want to block.
So if I want to block GA tracking to UA-12345-1 when a user has a certain cookie in their browser, I can use something like this in Custom HTML tag:
<script> if (Cookie - _ga_opt_out === 'true') window['ga-disable-GA ID'] = true; </script>
Here, Cookie – _ga_opt_out It is a first-party (dummy) cookie, which stores the value
true If the user chooses not to be tracked on my (virtual) site. GA ID It is a static variable that displays the Google Analytics tracking ID that the user chooses to opt out of.
Or if I want to use a file
doNotTrack The feature that most browsers allow you to set in their settings:
<script> if (navigator.doNotTrack && navigator.doNotTrack === 1) window['ga-disable-GA ID'] = true; </script>
Note that You should not add any operators to this custom HTML tag, since it will only fire when it is in the tag sequence.
Finally, you need to find a file first The Universal Analytics tag that is activated on the page with this tracking ID. Typically, this is the Page View flag that is activated on the All Pages launcher.
Then, scroll down to File Advanced Settings, and be sure to set up the tag sequence setting as follows:
This setting ensures that before the pageview tag is activated, the opt-out script has time to complete, and sets the browser to opt-out of Google Analytics tracking to the tracking ID generated in the global variable.
That’s really all there is to it. The obvious downside is that if you are following more than one GA property, you will need to exclude them all. You will also need to modify the conditional statement in the script to match the opt-out scheme offered by your website.