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):
- Enter this URL chrome://settings
- Click on the Settings menu
- Click on Advanced
- Click on Privacy and security
- Click on the option Send a "Do Not Track" request with your browsing traffic
- You are set
Firefox:
- Click on the browser hamburger menu (top right)
- Click on Options
- Click on Privacy & Security (left menu)
- Scroll down to the section Tracking Protection
- Select the option that is convenient to you.
Microsoft Edge:
- Click on the browser menu (3 dots on the top right)
- Click on Settings
- Click on View Advanced Settings
- Scroll down to the section Privacy and Services
- Select Send Do Not Track requests
Safari:
- Go to the browser settings
- Go to Privacy & Settings
- Click on View Advanced Settings
- Select Do Not Track
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)
Carlos M. Díaz Honrado :
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:
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:Carlos M. Díaz Honrado :
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