While I was setting up Google Analytics for this website, something that should be straightforward as Hugo already provides an internal template for this, I’ve noticed that my requests were not being tracked and displayed on the Google Analytics dashboard.

I started by making sure that the internal template was actually being properly used and then, just in case, that the tracking id was correct. Both seemed to be ok.

Only then it came to my mind that I was using Firefox with the protection level set to strict and the DNT header enabled. At first, I thought that the latter was the reason why my requests were not being tracked by Google Analytics.

However, I noticed that the Google Analytics script from Hugo’s internal template was not looking at the DNT header. It was using a default value set to false instead, meaning that it was Firefox’s protection level that was avoiding Google Analytics from tracking myself.

var doNotTrack = false;
if (!doNotTrack){...}

I knew nothing about the DNT or Do Not Track header. After a quick search, I realized that it’s an HTTP header that lets users opt-out from websites’ tracking. It’s up to the website to comply with the DNT header and it’s not widely accepted though.

Either way, I wanted this website to comply with the DNT header, so I’ve updated the internal template configuration to actually take advantage of this header and avoid tracking users if they rather not be tracked.

var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);
var doNotTrack = (dnt == "1" || dnt == "yes");
if (!doNotTrack) {...}