javascriptsecuritygoogle-analyticsprivacy

How to make Google Analytics respond to "Do Not Track"


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?


Solution

  • 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") {
    
            // paste analytics scripts here
    
        } else {
           console.debug("Request to cancel loading analytics scripts (Do-Not-Track).");
        }
    </script>
    

    See more: Navigator.doNotTrack and Tracking Preference Expression.