#GTMTips: Respect Opt-Out From Advertising and Analytics

Use customTask to block Google Analytics and the DoubleClick redirect in case the user has opted out from tracking and/or advertising.

With GDPR looming around the corner, it’s time to explore the options you have at your disposal for respecting the new, stricter regulations for tracking users and for collecting data about their visits to your website.

UPDATE 20 June 2018: Google has released the allowAdFeatures field which renders the solution below redundant (at least for the displayFeaturesTask part of it). Please refer to this article for more details on how to conditionally block the advertising hit to DoubleClick.

In this article, we’ll explore the wonderful customTask (again), to see how you can programmatically prevent the request to Google Analytics or the redirect to DoubleClick from ever taking place, in case certain conditions are met. These conditions could be, for example, a cookie which signifies that the user does not want to be tracked, or some other flag in the browser that you can listen to. It’s up to you to determine how you want to persist information about the user’s tracking preferences, so in this article I’ll just show you how to actually implement the blocker, using a browser cookie as an example of where the tracking preferences are stored.

Tip 65: Block the GA request and the DoubleClick redirect

In this hypothetical scenario, we have two 1st party cookies indicating if the user wants to be excluded from Google Analytics tracking and/or the advertising data redirect to DoubleClick:

Next, we’ll create the respective 1st Party Cookie variables in Google Tag Manager:

The variables are named (1) {{Cookie - _ga_opt_out}} and (2) {{Cookie - _dcl_opt_out}}.

Next, you’ll need a Custom JavaScript variable that looks like this:

function() {
  return function(model) {
    if ({{Cookie - _ga_opt_out}} === 'true') {
      model.set('sendHitTask', null);
    }
    if ({{Cookie - _dcl_opt_out}} === 'true') {
      model.set('displayFeaturesTask', null);
    }
  };
}

Finally, you need to add this Custom JavaScript variable to all your Universal Analytics tags by going to Fields to set, and adding a new field:

Note that the easiest way to do this is to use a Google Analytics Settings variable, and set the field there. Then add the GAS variable to all your Universal Analytics tags. If you want to set this per-tag, remember to check Enable overriding settings for this tag to find the Fields to set option.

The way this works now is that if the user has the _ga_opt_out cookie and its value is 'true', the request to Google Analytics will be blocked. And if the user has the _dcl_opt_out cookie and its value is 'true', the redirect to DoubleClick will be blocked, too.

This tip was, again, meant to first and foremost show you the amazing power of the customTask feature. It’s a versatile tool with which you can really manipulate what your website is doing in terms of Universal Analytics tracking.