How to set an Google Opt Out Cookie FOR GOOGLE ANALYTICS 4 - not universal one - the new ga4
Asked Answered
D

2

7

For a new project I want to use Google Analytics 4.

Until today I was using the normal Google Analytics and on my privay policy page I have had an Google Opt Out Cookie.
This was implement by an simple JavaScript Code.
The code was loooking like this:

<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXX-Y';

// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
  window[disableStr] = true;
}

// Opt-out function
function gaOptout() {
  document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
  window[disableStr] = true;
}
</script>

And with this code I was able to set an Google Opt Out Cookie on the page I wantet

<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>

So now I have the problem, that with the new ga4 this is not working. On the developer page of ga4 there is an code to disable ga4.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=MEASUREMENT_ID"></script>
<script>
  window['ga-disable-MEASUREMENT_ID'] = true;
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'MEASUREMENT_ID');
</script>

How I have to set up the Button Link to disable GA4?
Anybody can help me here???

Thanks already, stay healthy and enjoy your Life :)

Darrel answered 13/12, 2020 at 11:10 Comment(0)
P
5

You actually have most of the pieces here.

So get your GA4 measurement ID, something that looks like: 249888888

The piece of code in the GA4 tag that disables measurement is this:

window['ga-disable-MEASUREMENT_ID'] = true;

What you need to replace MEASURMENT_ID with your own ID, in this example: 249888888

Combine this with your own code:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=249888888"></script>
<script>
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-249888888';
if (document.cookie.indexOf(disableStr + '=true') > -1) {
   window['ga-disable-249888888'] = true; //this is what disables the tracking, note the measurement ID
}
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '249888888');
</script>

// Opt-out function (this function is unchanged)
function gaOptout() {
  document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
  window[disableStr] = true;
}
</script>
Parasynapsis answered 13/12, 2020 at 14:42 Comment(6)
Hey XTOTHEL, thanks for your reply. My measure-Id begins with an Letter and looks like G-M7562N73457 something similar. Unfortunally this Code is not working... I get No trackingDarrel
Did you try this code for Google Analytics 4 on your own project?Darrel
hey yes, use the G-M756..etc and I realised that "disableStr" wasn't declared.Parasynapsis
Would be awesome if you can publish the full code ;)Darrel
I tried your solution with window['ga-disable-XXXXX']=true for both GA (UA-XXXXX-Y) and GA4 (29888888) but it doesn't seem to prevent calls to google analytics apiVariform
@Darrel if you copied the code as-is, it's perhaps because of the early </script> closing tag that your code didn't work. Otherwise, this is the correct answer to this postSaccharide
M
5

For Google Analytics 4 I had to indicate the Measurement-ID with 'G-' Prefix.

window['ga-disable-G-XXXXXXXXXX']=true;

As you can see it here: https://support.google.com/analytics/answer/9539598?hl=en

In the example above it must be:



    ...
    // Disable tracking if the opt-out cookie exists.
    var disableStr = 'ga-disable-G-249888888';
    if (document.cookie.indexOf(disableStr + '=true') > -1) {
    ...


Malherbe answered 21/10, 2021 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.