Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User opt-out with gtag.js

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?

like image 904
Heronymo Avatar asked Jan 26 '18 19:01

Heronymo


People also ask

How to implement the user ID with GTAG?

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.

How does GTAG use cookies?

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:

How do I Opt Out of Google Analytics data collection?

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.

Can I disable Google Analytics on a page without removing the tag?

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.


1 Answers

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>
like image 180
Heronymo Avatar answered Oct 19 '22 10:10

Heronymo