Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Analytics that respects Do-Not-Track header
<script>
var dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
if (dnt != "1" && dnt != "yes") {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
} else {
console.debug("Respecting Do-Not-Track, not loading analytics. See https://www.paulfurley.com/google-analytics-dnt/");
}
</script>
@fawkesley

This comment has been minimized.

Copy link
Owner Author

@fawkesley fawkesley commented Apr 12, 2018

Thanks @dracos for pointing out:

that needs to be window.doNotTrack for Safari/IE11/Edge, and navigator.msDoNotTrack for IE9/10, and Firefox <32 uses "yes" not 1

Will update!

@dracos

This comment has been minimized.

Copy link

@dracos dracos commented Apr 12, 2018

Have done that at https://gist.github.com/dracos/caec9344285fce857d9af0fa6cc25e63 – also you might want to stick in ga('set', 'anonymizeIp', true); before the send line, because I think everyone should set that :)

@fawkesley

This comment has been minimized.

Copy link
Owner Author

@fawkesley fawkesley commented Apr 12, 2018

👍 thank you!

@HermannBjorgvin

This comment has been minimized.

Copy link

@HermannBjorgvin HermannBjorgvin commented Jul 8, 2018

Love this, thanks. I'm going to use this from now on because I know about this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment