Disable Google Analytics measurement

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.

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 first check if this property is set, and will take no action if the value is set to true.

To disable Analytics programmatically, set the following window property to true:

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

Replace GA_MEASUREMENT_ID with the Analytics ID of the property that you would like to disable.

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 Analytics. If the property is not set or set to false, then Analytics will work as usual.