Created
November 5, 2018 01:59
-
-
Save budui/65305b9e7ed2d5f0a552dc77e17af5b6 to your computer and use it in GitHub Desktop.
Disable Google Analytics tracking codes if DNT is set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Global site tag (gtag.js) - Google Analytics --> | |
<script> | |
var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack); | |
var doNotTrack = (dnt == "1" || dnt == "yes"); | |
if (!doNotTrack) { | |
(function () { | |
var ga = document.createElement('script'); | |
ga.type = 'text/javascript'; | |
ga.async = true; | |
ga.src = 'https://www.googletagmanager.com/gtag/js?id=UA-ADD-YOUR-ID'; | |
var s = document.getElementsByTagName('script')[0]; | |
s.parentNode.insertBefore(ga, s); | |
})(); | |
window.dataLayer = window.dataLayer || []; | |
function gtag() { | |
dataLayer.push(arguments); | |
} | |
gtag('js', new Date()); | |
gtag('config', 'UA-ADD-YOUR-ID'); | |
}else{ | |
console.debug("Respecting Do-Not-Track, not loading analytics."); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment