Disable google tag manager according to the decision of single users (opt-out) [closed]
Asked Answered
H

3

11

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?

Histo answered 5/5, 2016 at 13:46 Comment(2)
Just for argument's sake, what if I wanted to opt-in to analytics tracking? How would you be able to tell that, despite my "server-side cookie" says otherwise? I could be a different user on the same computer after all. By the way, this would need to be flagged as off topic as it's not related to programming, unfortunately.Ramberg
You are right, nevertheless that is a general cookie problem, not only mine ;) every time you build features based on cookies you are assuming that always the same user is behind that browserHisto
U
16

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.

Underprivileged answered 6/5, 2016 at 7:12 Comment(5)
Thanks Eike. I know what you said in the first four paragraphs. Maybe one could do something similar to: <script> if (condition) { $('<script>GTM script</' + 'script>').appendTo(document.body); } </script> About your sentence however you can use a cookie to disable all tags within GTM could you please point me to some reference? I'm not surely expecting Google do that for me ;)Histo
@Gamby, I added a bit of explanation.Underprivileged
@EikePierstorff nice answer, I added one mostly based on the ideas you shared. Do you think I missed something?Vibraculum
@EikePierstorff I got excited & added a 2nd answerVibraculum
This work to disable any tags, but my tags are not displayed even if i consent cookies. How should i do ?Flag
V
2

In a nutshell

A Javascript/jQuery / js-cookie only solution, which "only" works partially for the case of GTM (Google Tag Manager) since it has a <noscript> implementation. See details below.


How to

HTML

<span class="js-ga-opt-out">Disable GA - Javascript solution</span>
<span class="js-ga-opt-in">Enable GA - Javascript solution</span>

Javascript

// note that setCookie(key, value) is a custom function

function optOutGoogleTracking(){
    setCookie('_ga', undefined);
    setCookie('optOutGoogleTracking', true);
    alert('disabling GA cookies');
    window.location.reload();
}

function optInGoogleTracking(){
    setCookie('optOutGoogleTracking', false);
    alert('enabling GA cookies');
    window.location.reload();
}

$('html').on("click", ".js-ga-opt-out", function(){
    optOutGoogleTracking();
});

$('html').on("click", ".js-ga-opt-in", function(){
    optInGoogleTracking();
});

Google Tag Manager script

Note that the content within the <noscript></noscript> tag cannot be conditionally skipped with Javascript.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js.cookie.min.js"></script>
<!-- Google Tag Manager -->
<noscript>
<iframe src="//www.googletagmanager.com/ns.html?id=GTM-xxxxx"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<script>
if(Cookies.get('optOutGoogleTracking') === 'false') { // here is your condition
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-xxxxx');
}
</script>
<!-- End Google Tag Manager -->

My understanding is that the <noscript> tag is present for cases where Javascript is disabled. Then the tracking will be done via an iframe.

This solution is probably "good enough" for most cases though.

Is this legally compliant? Probably not.

Vibraculum answered 13/3, 2017 at 11:20 Comment(4)
Cookies is not definedRobynroc
@Robynroc what do you mean ?Vibraculum
Where is the Cookies object defined for Cookies.get('optOutGoogleTracking')?Crelin
From what I can tell, the example above relies on the js-cookie package to provide the Cookies object. github.com/js-cookie/js-cookieCrelin
V
1

In a nutshell:

Yes you can save this opt-in/opt-out choice on the server side, and conditionally add the GTM (Google Tag Manager) script according to the relevant attribute value within your JSP.


How to

Html

<span class="js-ga-opt-out">Disable GA - Server side solution</span>
<span class="js-ga-opt-in">Enable GA - Server side solution</span>

Javascript

function optOutGoogleTracking(){
    setCookie('_ga', undefined);

    // AJAX call to your web service setting the relevant cookie
    $.ajax({ 
      url: <optOutGoogleTrackingUrl>,
      type: 'GET',
      data: { optOutGoogleTracking: true} 
    })
    alert('disabling GA cookies');
    window.location.reload();
}

function optInGoogleTracking(){

    // AJAX call to your web service setting the relevant cookie
    $.ajax({ 
      url: <optOutGoogleTrackingUrl>,
      type: 'GET',
      data: { optOutGoogleTracking: false} 
    })

    alert('enabling GA cookies');
    window.location.reload();
}

$('html').on("click", ".js-ga-opt-out", function(){
    optOutGoogleTracking();
});

$('html').on("click", ".js-ga-opt-in", function(){
    optInGoogleTracking();
});

JSP

Here we assume that you have set the value of an attribute called optOutGoogleTracking.

Where you store it is up to you, but probably at an application-level scope since it's used across all your website.

I'll assume that you have a custom JSP Tag as explained here, called allThatJazz that allows me to access this value.

<c:if test="${allThatJazz.optOutGoogleTracking==false}">
    // put your Google Tag Manager script here
</c:if>

Note: I didn't implement this myself just yet

Vibraculum answered 13/3, 2017 at 10:24 Comment(1)
Just a question: If a customer/visitor click on the link and set that application-scope variable to false, I mean the allThatJazz.optOutGoogleTracking which is a variable, in your JAVA/Spring framework back-end code, then the GTM is disabled for all site visitors, not that specific visitor. Am I wrong?Reliance

© 2022 - 2024 — McMap. All rights reserved.