Created
July 14, 2019 23:17
-
-
Save mermop/373439b7be9e7580b78da67be820026d to your computer and use it in GitHub Desktop.
Respect do not track when loading Google Analytics
This file contains 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
<!-- borrowing from https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file --> | |
<script> | |
var ga_id = "UA-your-id-here" | |
function DNT () { | |
if ('msDoNotTrack' in navigator) return navigator.msDoNotTrack; | |
if ('doNotTrack' in window) return window.doNotTrack; | |
if ('doNotTrack' in navigator) return navigator.doNotTrack; | |
return 0; | |
}; | |
function dynamicallyLoadScript(url, callback) { | |
var script = document.createElement("script"); | |
script.src = url; | |
script.type = 'text/javascript'; | |
script.onreadystatechange = callback; | |
script.onload = callback; | |
document.head.appendChild(script); | |
} | |
if (DNT() != 1) { | |
dynamicallyLoadScript("https://www.googletagmanager.com/gtag/js?id=" + ga_id, function(){ | |
window.dataLayer = window.dataLayer || []; | |
function gtag() { dataLayer.push(arguments); } | |
gtag('js', new Date()); | |
gtag('config', ga_id); | |
}) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment