Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit e3f39a6

Browse files
authored
Merge pull request #236 from cvan/telemetry-for-secure-origins-only
improve `telemetry.js` for disabling localhost/non-HTTPS origins
2 parents a79e5b0 + ac59523 commit e3f39a6

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

Assets/WebGLTemplates/WebVR/lib/telemetry.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ var endsWith = function (str, suffix) {
2828

2929
// Check if the origin looks like a production, non-development host (i.e., public and served over HTTPS).
3030
// Relevant reading: https://w3c.github.io/webappsec-secure-contexts/#localhost
31-
var isSecureOrigin = function (win) {
32-
return !(
33-
win.isSecureContext === false ||
34-
win.location.protocol === 'http:' ||
31+
var isInsecureOrigin = function (win) {
32+
// Allow HTTPS and HTTP.
33+
if (win.isSecureContext === true || win.location.protocol === 'http:') {
34+
return false;
35+
}
36+
return (
3537
win.location.hostname === 'localhost' ||
3638
endsWith(win.location.hostname, '.localhost') ||
3739
win.location.hostname === '127.0.1' ||
@@ -42,17 +44,6 @@ var isSecureOrigin = function (win) {
4244
);
4345
};
4446

45-
// IE9/IE10 uses a prefixed version while MS Edge sets the property in
46-
// `window` instead of `navigator`:
47-
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack#Browser_compatibility
48-
var doNotTrack = onlyOnce(function () {
49-
// We also will not engage Telemetry if the origin appears to be in a development (i.e., non-production) environment.
50-
return navigator.doNotTrack === '1' ||
51-
navigator.msDoNotTrack === '1' ||
52-
window.doNotTrack === '1' ||
53-
!isSecureOrigin(window);
54-
});
55-
5647
var CURRENT_VERSION = '1.1.0';
5748
var MOZILLA_RESEARCH_TRACKER = 'UA-77033033-6';
5849

@@ -96,7 +87,7 @@ telemetry.start = onlyOnce(function (config) {
9687
setupAnalytics();
9788

9889
function setupAnalytics() {
99-
if (doNotTrack()) { return; }
90+
if (isTelemetryDisabled()) { return; }
10091

10192
window.dataLayer = window.dataLayer || [];
10293
window.gtag = window.gtag || function () { dataLayer.push(arguments); };
@@ -111,7 +102,7 @@ function setupAnalytics() {
111102
}
112103

113104
function setupErrorLogging() {
114-
if (doNotTrack()) { return; }
105+
if (isTelemetryDisabled()) { return; }
115106

116107
injectScript('https://cdn.ravenjs.com/3.22.3/console/raven.min.js', function (err) {
117108
if (err) {
@@ -145,13 +136,13 @@ function startAnalytics() {
145136
function setupPerformanceAPI(tracker) {
146137
telemetry.performance = {
147138
mark: function (name) {
148-
if (doNotTrack()) { return; }
139+
if (isTelemetryDisabled()) { return; }
149140

150141
performance.mark(name);
151142
},
152143

153144
measure: function (name, start, end) {
154-
if (doNotTrack()) { return; }
145+
if (isTelemetryDisabled()) { return; }
155146

156147
performance.measure(name, start, end);
157148
var performanceEntry = performance.getEntriesByName(name)[0];
@@ -180,15 +171,15 @@ function setupPerformanceAPI(tracker) {
180171
* commands [2].
181172
*/
182173
function configureBoundTracker(trackingId, options) {
183-
if (doNotTrack()) { return NO_OP; }
174+
if (isTelemetryDisabled()) { return NO_OP; }
184175

185176
options = options || {};
186177
var groups = options.groups;
187178
telemetry._gtag('config', trackingId, options);
188179
return trackingFunction;
189180

190181
function trackingFunction(command, label, options) {
191-
if (doNotTrack()) { return; }
182+
if (isTelemetryDisabled()) { return; }
192183

193184
options = options || {};
194185
if (groups) {
@@ -226,4 +217,18 @@ function onlyOnce(fn) {
226217
};
227218
}
228219

220+
// IE9/IE10 uses a prefixed version while MS Edge sets the property in
221+
// `window` instead of `navigator`:
222+
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack#Browser_compatibility
223+
function doNotTrack () {
224+
return navigator.doNotTrack === '1' ||
225+
navigator.msDoNotTrack === '1' ||
226+
window.doNotTrack === '1';
227+
}
228+
229+
function isTelemetryDisabled () {
230+
// Telemetry is disabled if DNT is enabled or if the origin appears to be for a development environment.
231+
return doNotTrack() || isInsecureOrigin(window);
232+
}
233+
229234
})(window);

Build/lib/telemetry.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ var endsWith = function (str, suffix) {
2828

2929
// Check if the origin looks like a production, non-development host (i.e., public and served over HTTPS).
3030
// Relevant reading: https://w3c.github.io/webappsec-secure-contexts/#localhost
31-
var isSecureOrigin = function (win) {
32-
return true;
33-
return !(
34-
win.isSecureContext === false ||
35-
win.location.protocol === 'http:' ||
31+
var isInsecureOrigin = function (win) {
32+
// Allow HTTPS and HTTP.
33+
if (win.isSecureContext === true || win.location.protocol === 'http:') {
34+
return false;
35+
}
36+
return (
3637
win.location.hostname === 'localhost' ||
3738
endsWith(win.location.hostname, '.localhost') ||
3839
win.location.hostname === '127.0.1' ||
@@ -43,17 +44,6 @@ var isSecureOrigin = function (win) {
4344
);
4445
};
4546

46-
// IE9/IE10 uses a prefixed version while MS Edge sets the property in
47-
// `window` instead of `navigator`:
48-
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack#Browser_compatibility
49-
var doNotTrack = onlyOnce(function () {
50-
// We also will not engage Telemetry if the origin appears to be in a development (i.e., non-production) environment.
51-
return navigator.doNotTrack === '1' ||
52-
navigator.msDoNotTrack === '1' ||
53-
window.doNotTrack === '1' ||
54-
!isSecureOrigin(window);
55-
});
56-
5747
var CURRENT_VERSION = '1.1.0';
5848
var MOZILLA_RESEARCH_TRACKER = 'UA-77033033-6';
5949

@@ -97,7 +87,7 @@ telemetry.start = onlyOnce(function (config) {
9787
setupAnalytics();
9888

9989
function setupAnalytics() {
100-
if (doNotTrack()) { return; }
90+
if (isTelemetryDisabled()) { return; }
10191

10292
window.dataLayer = window.dataLayer || [];
10393
window.gtag = window.gtag || function () { dataLayer.push(arguments); };
@@ -112,7 +102,7 @@ function setupAnalytics() {
112102
}
113103

114104
function setupErrorLogging() {
115-
if (doNotTrack()) { return; }
105+
if (isTelemetryDisabled()) { return; }
116106

117107
injectScript('https://cdn.ravenjs.com/3.22.3/console/raven.min.js', function (err) {
118108
if (err) {
@@ -146,13 +136,13 @@ function startAnalytics() {
146136
function setupPerformanceAPI(tracker) {
147137
telemetry.performance = {
148138
mark: function (name) {
149-
if (doNotTrack()) { return; }
139+
if (isTelemetryDisabled()) { return; }
150140

151141
performance.mark(name);
152142
},
153143

154144
measure: function (name, start, end) {
155-
if (doNotTrack()) { return; }
145+
if (isTelemetryDisabled()) { return; }
156146

157147
performance.measure(name, start, end);
158148
var performanceEntry = performance.getEntriesByName(name)[0];
@@ -181,15 +171,15 @@ function setupPerformanceAPI(tracker) {
181171
* commands [2].
182172
*/
183173
function configureBoundTracker(trackingId, options) {
184-
if (doNotTrack()) { return NO_OP; }
174+
if (isTelemetryDisabled()) { return NO_OP; }
185175

186176
options = options || {};
187177
var groups = options.groups;
188178
telemetry._gtag('config', trackingId, options);
189179
return trackingFunction;
190180

191181
function trackingFunction(command, label, options) {
192-
if (doNotTrack()) { return; }
182+
if (isTelemetryDisabled()) { return; }
193183

194184
options = options || {};
195185
if (groups) {
@@ -227,4 +217,18 @@ function onlyOnce(fn) {
227217
};
228218
}
229219

220+
// IE9/IE10 uses a prefixed version while MS Edge sets the property in
221+
// `window` instead of `navigator`:
222+
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack#Browser_compatibility
223+
function doNotTrack () {
224+
return navigator.doNotTrack === '1' ||
225+
navigator.msDoNotTrack === '1' ||
226+
window.doNotTrack === '1';
227+
}
228+
229+
function isTelemetryDisabled () {
230+
// Telemetry is disabled if DNT is enabled or if the origin appears to be for a development environment.
231+
return doNotTrack() || isInsecureOrigin(window);
232+
}
233+
230234
})(window);

0 commit comments

Comments
 (0)