Skip to content

Commit 2d0fb3a

Browse files
phillipjlpinca
authored andcommitted
Respect visitor's Do-Not-Track setting (#885)
There's been raised some concern about our use of external web tracking/analytics service. In that discussion, there has been agreement that we at least should respect the visitor's Do-Not-Track setting in their browser.
1 parent 7c2845f commit 2d0fb3a

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

layouts/index.hbs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@
126126

127127
{{> footer className="no-margin-top" }}
128128
<script src="/static/js/download.js" async defer></script>
129+
<script src="/static/js/dnt_helper.js"></script>
130+
<script>
131+
if (!_dntEnabled()) {
132+
!function(n,o,d,e,j,s){n.GoogleAnalyticsObject=d;n[d]||(n[d]=function(){
133+
(n[d].q=n[d].q||[]).push(arguments)});n[d].l=+new Date;j=o.createElement(e);
134+
s=o.getElementsByTagName(e)[0];j.async=1;j.src='//www.google-analytics.com/analytics.js';
135+
s.parentNode.insertBefore(j,s)}(window,document,'ga','script');
136+
137+
ga('create', 'UA-67020396-1', 'auto');
138+
ga('send', 'pageview');
139+
}
140+
</script>
129141

130142
</body>
131143
</html>

layouts/partials/html-head.hbs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,5 @@
2121
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600">
2222
<script>
2323
document.querySelector('html').className += " has-js";
24-
25-
!function(n,o,d,e,j,s){n.GoogleAnalyticsObject=d;n[d]||(n[d]=function(){
26-
(n[d].q=n[d].q||[]).push(arguments)});n[d].l=+new Date;j=o.createElement(e);
27-
s=o.getElementsByTagName(e)[0];j.async=1;j.src='//www.google-analytics.com/analytics.js';
28-
s.parentNode.insertBefore(j,s)}(window,document,'ga','script');
29-
30-
ga('create', 'UA-67020396-1', 'auto');
31-
ga('send', 'pageview');
3224
</script>
3325
</head>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"standard": {
3333
"ignore": [
3434
"static/js/modernizr.custom.js",
35+
"static/js/dnt_helper.js",
3536
"static/legacy/*"
3637
]
3738
},

static/js/dnt_helper.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* http://schalkneethling.github.io/blog/2015/11/06/respect-user-choice-do-not-track/
3+
* https://github.com/schalkneethling/dnt-helper/blob/master/js/dnt-helper.js
4+
*
5+
* Returns true or false based on whether doNotTack is enabled. It also takes into account the
6+
* anomalies, such as !bugzilla 887703, which effect versions of Fx 31 and lower. It also handles
7+
* IE versions on Windows 7, 8 and 8.1, where the DNT implementation does not honor the spec.
8+
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1217896 for more details
9+
* @params {string} [dnt] - An optional mock doNotTrack string to ease unit testing.
10+
* @params {string} [userAgent] - An optional mock userAgent string to ease unit testing.
11+
* @returns {boolean} true if enabled else false
12+
*/
13+
function _dntEnabled(dnt, userAgent) {
14+
15+
'use strict';
16+
17+
// for old version of IE we need to use the msDoNotTrack property of navigator
18+
// on newer versions, and newer platforms, this is doNotTrack but, on the window object
19+
// Safari also exposes the property on the window object.
20+
var dntStatus = dnt || navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
21+
var ua = userAgent || navigator.userAgent;
22+
23+
// List of Windows versions known to not implement DNT according to the standard.
24+
var anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3'];
25+
26+
var fxMatch = ua.match(/Firefox\/(\d+)/);
27+
var ieRegEx = /MSIE|Trident/i;
28+
var isIE = ieRegEx.test(ua);
29+
// Matches from Windows up to the first occurance of ; un-greedily
30+
// http://www.regexr.com/3c2el
31+
var platform = ua.match(/Windows.+?(?=;)/g);
32+
33+
// With old versions of IE, DNT did not exist so we simply return false;
34+
if (isIE && typeof Array.prototype.indexOf !== 'function') {
35+
return false;
36+
} else if (fxMatch && parseInt(fxMatch[1], 10) < 32) {
37+
// Can't say for sure if it is 1 or 0, due to Fx bug 887703
38+
dntStatus = 'Unspecified';
39+
} else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) {
40+
// default is on, which does not honor the specification
41+
dntStatus = 'Unspecified';
42+
} else {
43+
// sets dntStatus to Disabled or Enabled based on the value returned by the browser.
44+
// If dntStatus is undefined, it will be set to Unspecified
45+
dntStatus = { '0': 'Disabled', '1': 'Enabled' }[dntStatus] || 'Unspecified';
46+
}
47+
48+
return dntStatus === 'Enabled' ? true : false;
49+
}

0 commit comments

Comments
 (0)