Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2017 15:34
Show Gist options
  • Save anonymous/93bdbf6aa394c7176da92d71b44d17e2 to your computer and use it in GitHub Desktop.
Save anonymous/93bdbf6aa394c7176da92d71b44d17e2 to your computer and use it in GitHub Desktop.
<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment