Disable google tag manager according to the decision of single users (opt-out)

Gamby picture Gamby · May 5, 2016 · Viewed 12.7k times · Source

In some countries (i.e. Italy), legal requirements oblige website to provide disable/enable analytics cookies

For instance: for Google Tag Manager.

I read this post covers this "opt-in/opt-out analytics topic".


However I still have a question:

Why can't I simply read a cookie server-side which contains the user preference and if I find a given value (i.e. "disable"), set an attribute to be able to conditionally include (or not) the GTM script in my JSP page?

Answer

Eike Pierstorff picture Eike Pierstorff · May 6, 2016

Just to point out, GTM is neither a tracking tool nor does it by itself set any cookies.

More programming related, the GTM code cannot disable itself based on a cookie because GTM needs to be loaded to check if a cookie exists.

Cookies are sent as part of the http request only to the domain that has set the server; GTM resides on a Google server that will not have access to the cookies set on your domain. So if an opt-out cookie is set on your domain the GTM server will not know about it.

Cookies are mostly a client-side technology; GTM interacts with cookies by injecting JavaScript into your page so that it runs in the context of your domain, and then have the script evaluate the contents of your cookie (if you set up a cookie variable or a custom script). At that point the GTM code is already loaded.

That is why you cannot use a cookie to prevent GTM from loading; however you can use a cookie to disable all tags within GTM. If that's not good enough you have to write your own logic to disable GTM conditionally (you could even write a routine in your CMS that doesn't render GTM code based on a cookie - after all that's your own domain, so cookie data is sent along the request; you just cannot expect Google to do this for you).

GTM cannot set the cookie by itself (unless you write a custom HTML tag with a script that sets cookies, which is not different from doing it via inline code), so I will assume for an example that you already a cookie called "opt-out". It does not matter what value is stored in that cookie, we will just check if it is there.

Go to GTM, to the "variables" section, click "new" and select "First Party Cookie". Name it e.g. "Opt Out Cookie" and set the name field to "opt-out". Save. Now you have a variable that checks for the opt-out cookie, returns a value if it is set and returns "undefined" if the cookie is not there.

Now go to the triggers section and create a new trigger of the type page view. Call it e.g. "Opt Out Trigger". In the "fire on" section you select the "Opt Out Cookie" variable in the first field, set "does not equal" as condition and "undefined" as value (so the trigger evaluates true when the cookie is set).

Now go through your tags and add the "Opt Out Trigger" to the tags you want to disable when the cookie is set. Save, publish.

The only caveat is that a pageview trigger might either fire on pageview, DOM ready or pageload. An exception trigger that fires on pageview will not prevent tags from firing that are set to DOM ready or pageload, so you might need multiple exceptions, one for each stage of the loading process.