Skip to content

Instantly share code, notes, and snippets.

@budui
Created November 5, 2018 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budui/65305b9e7ed2d5f0a552dc77e17af5b6 to your computer and use it in GitHub Desktop.
Save budui/65305b9e7ed2d5f0a552dc77e17af5b6 to your computer and use it in GitHub Desktop.
Disable Google Analytics tracking codes if DNT is set.
<!-- 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