Skip to content

Instantly share code, notes, and snippets.

@schalkneethling
Created October 7, 2018 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schalkneethling/2ea8e5be0ee84ac4c4a2dce2961e6ddc to your computer and use it in GitHub Desktop.
Save schalkneethling/2ea8e5be0ee84ac4c4a2dce2961e6ddc to your computer and use it in GitHub Desktop.
Code snippets that go along with the blog post ~ Respect User Choice ~ Do Not Track
var anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3'];
// include the helper
<script src="js/libs/dnt-helper.js"></script>
// If doNotTrack is not enabled, it is ok to add GTM
if (!_dntEnabled()) {
(function(w,d,s,l,i,j,f,dl,k,q){
w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});f=d.getElementsByTagName(s)[0];
k=i.length;q='//www.googletagmanager.com/gtm.js?id=@&l='+(l||'dataLayer');
while(k--){j=d.createElement(s);j.async=!0;j.src=q.replace('@',i[k]);f.parentNode.insertBefore(j,f);}
}(window,document,'script','dataLayer',['YOUR-GTM-ID}}']));
}
var dntStatus = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
} else if (fxMatch && parseInt(fxMatch[1], 10) < 32) {
// Can't say for sure if it is 1 or 0, due to Fx bug 887703
dntStatus = 'Unspecified';
// With old versions of IE, DNT did not exist so we simply return false;
if (isIE && typeof Array.prototype.indexOf !== 'function') {
return false;
...
} else {
// sets dntStatus to Disabled or Enabled based on the value returned by the browser.
// If dntStatus is undefined, it will be set to Unspecified
dntStatus = { '0': 'Disabled', '1': 'Enabled' }[dntStatus] || 'Unspecified';
}
} else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) {
// default is on, which does not honor the specification
dntStatus = 'Unspecified';
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment