Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This little snippet provides a simple way to a) implement Google Tag Manager with anonymized IP and b) provide a Opt-Out / Opt-In mechanism: gtagOptOutIn();
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
window.gtagTrackingID = 'UA-XXXXXXXX-X';
window.gtagDisableStr = 'gtag-disable-' + gtagTrackingID;
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
function gtagIsDisabled() {
return (document.cookie.indexOf(gtagDisableStr + '=true') > -1);
}
function gtagOptOutIn() {
if (gtagIsDisabled()) {
document.cookie = gtagDisableStr + "=-10; path=/";
window['ga-disable-' + gtagTrackingID] = false;
} else {
document.cookie = gtagDisableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window['ga-disable-' + gtagTrackingID] = true;
}
}
window['ga-disable-' + gtagTrackingID] = false;
if (gtagIsDisabled()) {
window['ga-disable-' + gtagTrackingID] = true;
}
gtag('js', new Date());
gtag('config', gtagTrackingID, {
'anonymize_ip': true
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.