We're looking to implement the option for our website visitors to disable tracking.
I've read the Google Analytics opt-out dev guide but I'm still not totally clear on how to do this.
Is it sufficient to just trigger window['ga-disable-GA_TRACKING_ID'] = true;
when a button is clicked?
Will Google Analytics remember the setting for this user, or is it on a per-visit basis?
Can the user re-enable tracking if I have a second button that sets it to false
?
To implement the User ID with gtag.js, update the config for your property to set the User ID: You can configure gtag.js to not read or write cookies until consent is provided from the user.
gtag.js uses cookies to identify unique users across browsing sessions. This page explains how to customize the cookie settings. Note: You can refer to the Cookie usage guide for details on how gtag.js uses cookies. The following table shows the default cookie field values used by gtag.js:
Users of your website can also opt out from data collection using the Google Analytics opt-out browser add-on. You can programmatically disable collection of data for advertising features from Android and iOS apps that use the: Learn more about the data that Google Analytics and Google Analytics for Firebase collect.
In some cases, it may be necessary to disable Google Analytics on a page without removing the gtag.js tag. For example, you might do this if your site's privacy policy provides an option for the user to opt-out of Google Analytics.
Paste the following into the head tag before the Analytics code (replacing the UA with your own UA)
<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 use this to trigger it:
<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With