DigitalA
DigitalA

Reputation: 71

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

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 :)

Upvotes: 7

Views: 4796

Answers (2)

Baster
Baster

Reputation: 51

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) {
    ...


Upvotes: 5

XTOTHEL
XTOTHEL

Reputation: 5208

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>

Upvotes: 5

Related Questions