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