0

I am planning to put google analytics tracking code on my website, but I don't know how to make it respond to those who send the "Do not track" signal. How can I make GA tracking code track those who don have DNT signal while protecting those who have it?

Anmicius
  • 71
  • 8

5 Answers5

3

You can’t respond to this request in your GA dashboard, BUT, you can activate analytics scripts by condition:

<script>

    let dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;

    if (dnt != "1" && dnt != "yes") {

        // past analytics scripts here

    } else {
       console.debug("Request to cancel loading analytics scripts (Do-Not-Track).");
    }
</script>

See more: Navigator.doNotTrack and Tracking Preference Expression.

1

Use the following google analytics tag to respect the DNT browser setting (replace XX-XXXXXX-X with your Tracking-ID).

With this solution, no data is transferred to google when dnt is active. When using the "google way" with the user-opt-out, there is still a google script included in your page, they still know that you're on this page (even when they claim to not track you).

<script>
    <!-- Global site tag (gtag.js) - Google Analytics respecting DNT -->
    if ([true, 1, '1', 'on', 'yes'].indexOf(navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack) === -1) {
        let gtagId='XX-XXXXXX-X', sc=document.createElement('script');
        sc.async=true;
        sc.src='https://www.googletagmanager.com/gtag/js?id=' + gtagId;
        document.head.appendChild(sc);
        
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', gtagId);
    }
</script>
Lukas
  • 23
  • 3
0

I don't know if there is any particular support for DNT within the config for GA, but it's academic anyway, as the simple act of loading GA's script transmits all that google needs for tracking - IP, browser fingerprint, referrer, cookies — and google can't really be trusted to not use such information when it's dropped into their lap like that.

The only way around that is to filter it yourself pre-emptively - if DNT is set in the browser, don't even load the GA script.

Bear in mind that cases in the last couple of months in Europe (search for "Planet49") have reiterated that analytics cookies (especially third-party ones) are not considered "necessary", and as such require consent before you can load the script or let it set any cookies.

The way to avoid all this fuss is of course to self-host your own analytics (e.g. with Matomo or similar) and don't use cookies unnecessarily, as GA does.

Synchro
  • 35,538
  • 15
  • 81
  • 104
0

I just want to jump in to say that you are using the wrong tool for this job. If you want an analytics platform that respects DNT, then you will need to use something other than Google Analytics. There aren't a lot of privacy-friendly analytcis - this was actually the reasoning that led us to create Insights. The following tools are privacy-focused and, as far as I know, respect DNT requests:

  • Insights: Tracks events, page views, and more. Respects DNT requests.

  • SimpleAnalytics: Tracks page views. Respects DNT requests.

  • Matomo: Probably the most complete privacy-friendly analytics tool. It can be configured to respect DNT requests. Self-hosted.

  • Hi @Henrique Spotorno, thanks for the insights. I ran into a similar question about the `dnt` field (https://stackoverflow.com/questions/67540315/how-to-check-if-a-website-complies-the-do-not-track-request). I checked your site (https://getinsights.io/) and don't see the `tk` in the response header. How can I verify if your server complied with my `dnt-enabled` request or not? Also, looks like none of the three services you mentioned above have implemented the `tk` in their response header. – weefwefwqg3 May 14 '21 at 20:10
  • It appears as of now the tk header is deprecated and should not be used. developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Tk – EHLOVader Feb 01 '22 at 16:25
  • [It also makes you more likely to be tracked by fingerprinting](https://getinsights.io/blog/posts/fingerprinting-do-not-track), so disabling it seems to be more privacy-friendly as well – Henrique Spotorno Feb 02 '22 at 17:23
0

Google Analytics now supports a method for user opt-out.

This can be used to implement both:

  • Obey browers' DoNotTrack
  • Cookie consent banners