User opt-out with gtag.js

In some cases, it may be necessary to disable the Google Analytics tracking code on a page without removing the gtag.js tracking snippet. For example, you might do this if your site's privacy policy includes the ability for a user to opt-out of Google Analytics tracking.

Opt-out of tracking for your site

The gtag.js library includes a window property that, when set to true, disables gtag.js from sending data to Google Analytics. When Google Analytics attempts to set a cookie or send data back to the Google Analytics servers, it will check whether this property is set to true. If it is, no action will be taken.

To disable tracking, set the following window property to true:

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

where GA_TRACKING_ID is the tracking ID of the property on which you would like to disable tracking.

This window property must be set before any calls to gtag() are made, and it must be set on each page for which you want to disable Google Analytics tracking. If the property is not set or set to false, then tracking will work as usual.

Opt-out of tracking on all sites

Users can opt-out of Google Analytics tracking for all sites, even sites whose source code they do not control, by installing the Google Analytics opt-out browser add-on.