Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Last active October 26, 2018 21:11
Show Gist options
  • Save JeffJacobson/9513812 to your computer and use it in GitHub Desktop.
Save JeffJacobson/9513812 to your computer and use it in GitHub Desktop.
Google Analytics setup respecting navigator.doNotTrack.
// 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