Respecting Do Not Track with Google Analytics

I'll disable google analytics

It's hard for me to talk about user privacy on this blog when I have Google Analytics measuring everything you do on the website. I've tried to move away from it in the past and use the self-hosted open-source alternative but it quickly failed. When I received a burst of traffic, the server on which it was hosted crashed. I'm still looking for a self-hosted solution but in the meanwhile, I will do what I can to limit tracking by respecting users that specifically ask not to be tracked.

Every browser gives you the option not to be tracked. When you activate this option (see below), the browser sets a global variable in JavaScript navigator.doNotTrack and it is accessible to all scripts that have the intention of tracking you. At that point, it is up to those scripts to decide to respect it or not. After doing some research, I found that Google Analytics does not respect this directive. On my websites it is up to me to respect it.

So as of September 26 2018 I have added a snippet of code that will respect the Do Not Track directive.

Below is the shim that tests it:

var _doNotTrack = navigator.doNotTrack ?
        navigator.doNotTrack === "1" || navigator.doNotTrack === "yes": 
        (window.doNotTrack? window.doNotTrack === "1": false);
if (_doNotTrack){
    // do not load GA
}

For now this script will determine if you have opted out of being tracked and will prevent Google Analytics from loading.

How to set Do Not Track on you browser

Google Chrome (Version 69):

Firefox:

Microsoft Edge:

Safari:

As far as I am concerned, if users have chosen not to be tracked, they will not be tracked with Google Analytics on this website. I hope other developers will join me on making sure we respect this rule.


Comments(4)

Thkx for your post Ibrahim. Only two questions:

1.Where and how you implement this snippet, in Google Analytics tracking code? or in each page. Whats the method? 2.In vier this page in HTML code and in the end is this code:

<script type="text/javascript">
var _doNotTrack = navigator.doNotTrack?navigator.doNotTrack === "1" || navigator.doNotTrack === "yes": (window.doNotTrack? window.doNotTrack=== "1": false);
...
</script>

This script is different that your proposal in this post, Whats the correct implementation.

Regards

ibrahim :

Hi Carlos,

This is implemented on the client side. What I did is detect if the user chose "do not track" in the bool _doNotTrack then I check for it before loading google analytics:

if (_doNotTrack) return;

Thkx.

But your code:

var doNotTrack = navigator.doNotTrack ? navigator.doNotTrack === "1" || navigator.doNotTrack === "yes": (window.doNotTrack? window.doNotTrack === "1": false); if (doNotTrack){ // do not load GA }

Must be implement in server, is it like that?

I understand thats tis code must be implement in all server pages, before gtag.js code...

For example, in WordPress mus be implemented vía MU-Plugin?

Regards

Ibrahim :

The code you see runs on the browser. Meaning it appear on every single page on the client.

You can also implement it on the server side but you have to look for a specific http header: DNT.

Let's hear your thoughts

For my eyes only