Last active
October 26, 2018 21:11
-
-
Save JeffJacobson/9513812 to your computer and use it in GitHub Desktop.
Google Analytics setup respecting navigator.doNotTrack.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup Google Analytics, but not if user has specified that they don't want to be tracked. | |
(function (dnt, cookieDomain) { | |
var scriptTag, hostRe = new RegExp(cookieDomain.replace(".", "\\.") + "$"); | |
if (dnt !== "yes" && dnt !== "1") { | |
window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) }; ga.l = +new Date; | |
ga('create', 'YOUR-ID-HERE', hostRe.test(location.host) ? cookieDomain : "auto"); | |
ga(function (tracker) { | |
tracker.set("appName", "Your app name here"); | |
tracker.send('pageview'); | |
}); | |
// Add the script tag. | |
scriptTag = document.createElement("script"); | |
scriptTag.src = "//www.google-analytics.com/analytics.js"; | |
scriptTag.async = true; | |
document.head.appendChild(scriptTag); | |
} | |
}(navigator.doNotTrack || navigator.msDoNotTrack || null, "wsdot.wa.gov")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment