|
| 1 | +/* global Raven */ |
| 2 | +(function (window) { |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +/* |
| 6 | + * The main requirements when dealing with Do-Not-Track (DNT) are: |
| 7 | + * |
| 8 | + * 1. Prevent explicit collection, storage and sending data if DNT is enabled. |
| 9 | + * |
| 10 | + * 2. GA and Telemetry APIs should not throw or error if DNT is enabled, |
| 11 | + * although they must ensure requirement #1. |
| 12 | + * |
| 13 | + * 3. If DNT is enabled in the middle of the gameplay, the API must ensure |
| 14 | + * requirements #1 and #2. If DNT is disabled in the middle of the gameplay, |
| 15 | + * resuming data collection is not mandatory. |
| 16 | + * |
| 17 | + * FOR MAINTAINERS: Some collection can happen implicitly when loading 3rd party |
| 18 | + * libraries. Try to prevent loading external libraries if DNT is enabled and |
| 19 | + * this does not conflict with requirement #2. |
| 20 | + */ |
| 21 | + |
| 22 | +var CURRENT_VERSION = '1.0.2'; |
| 23 | +var MOZILLA_RESEARCH_TRACKER = 'UA-77033033-6'; |
| 24 | + |
| 25 | +if (!('MozillaResearch' in window)) { |
| 26 | + window.MozillaResearch = {}; |
| 27 | +} |
| 28 | + |
| 29 | +if (!('telemetry' in window.MozillaResearch)) { |
| 30 | + window.MozillaResearch.telemetry = {}; |
| 31 | +} |
| 32 | + |
| 33 | +var navigator = window.navigator; |
| 34 | +var telemetry = window.MozillaResearch.telemetry; |
| 35 | + |
| 36 | +var NO_OP = function () {}; |
| 37 | + |
| 38 | +Object.defineProperty(telemetry, '_gtag', { |
| 39 | + get: function () { |
| 40 | + return window.gtag || NO_OP; |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +telemetry.performance = { |
| 45 | + mark: NO_OP, |
| 46 | + measure: NO_OP |
| 47 | +}; |
| 48 | + |
| 49 | +telemetry.start = onlyOnce(function (config) { |
| 50 | + config = config || {}; |
| 51 | + if (config.errorLogging) { |
| 52 | + setupErrorLogging(); |
| 53 | + } |
| 54 | + if (config.analytics) { |
| 55 | + var researchAnalytics = startAnalytics(); |
| 56 | + if (config.performance) { |
| 57 | + setupPerformanceAPI(researchAnalytics); |
| 58 | + } |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +setupAnalytics(); |
| 63 | + |
| 64 | +function setupAnalytics() { |
| 65 | + if (doNotTrack()) { return; } |
| 66 | + |
| 67 | + window.dataLayer = window.dataLayer || []; |
| 68 | + window.gtag = window.gtag || function () { dataLayer.push(arguments); }; |
| 69 | + window.gtag('js', new Date()); |
| 70 | + |
| 71 | + injectScript('https://www.googletagmanager.com/gtag/js?id=' + MOZILLA_RESEARCH_TRACKER, function (err) { |
| 72 | + if (err) { |
| 73 | + console.warn('Could not load Analytics.js script:', err); |
| 74 | + return; |
| 75 | + } |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +function setupErrorLogging() { |
| 80 | + if (doNotTrack()) { return; } |
| 81 | + |
| 82 | + injectScript('https://cdn.ravenjs.com/3.22.3/console/raven.min.js', function (err) { |
| 83 | + if (err) { |
| 84 | + console.warn('Could not load Raven.js script:', err); |
| 85 | + return; |
| 86 | + } |
| 87 | + if (!('Raven' in window)) { |
| 88 | + console.warn('Could not find `window.Raven` global'); |
| 89 | + return; |
| 90 | + } |
| 91 | + startRaven(); |
| 92 | + }); |
| 93 | + |
| 94 | + function startRaven () { |
| 95 | + Raven.config('https://e359be9fb9324addb0dc97b664cf5ee6@sentry.io/294878') |
| 96 | + .install(); |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +function startAnalytics() { |
| 101 | + var tracker = configureBoundTracker(MOZILLA_RESEARCH_TRACKER, { |
| 102 | + 'groups': 'MozillaResearch', |
| 103 | + 'custom_map': { |
| 104 | + 'dimension1': 'version' |
| 105 | + } |
| 106 | + }); |
| 107 | + tracker('event', 'using_webvr_template', { version: CURRENT_VERSION }); |
| 108 | + return tracker; |
| 109 | +} |
| 110 | + |
| 111 | +function setupPerformanceAPI(tracker) { |
| 112 | + telemetry.performance = { |
| 113 | + mark: function (name) { |
| 114 | + if (doNotTrack()) { return; } |
| 115 | + |
| 116 | + performance.mark(name); |
| 117 | + }, |
| 118 | + |
| 119 | + measure: function (name, start, end) { |
| 120 | + if (doNotTrack()) { return; } |
| 121 | + |
| 122 | + performance.measure(name, start, end); |
| 123 | + var performanceEntry = performance.getEntriesByName(name)[0]; |
| 124 | + var duration = performanceEntry.duration; |
| 125 | + tracker('event', name, { |
| 126 | + 'event_category': 'Performance', |
| 127 | + 'value': Math.round(duration) |
| 128 | + }); |
| 129 | + } |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * The function configures Google Analytics sending a `config` command [1] but |
| 135 | + * also returns a function to use instead of `gtag` that respects `Do-Not-Track` |
| 136 | + * and it's bound to the `groups` property of the `options` parameter to avoid |
| 137 | + * adding the `send_to` key [2] to each command. |
| 138 | + * |
| 139 | + * [1] https://developers.google.com/analytics/devguides/collection/gtagjs/pages |
| 140 | + * [2] https://developers.google.com/analytics/devguides/collection/gtagjs/sending-data#groups-and-properties |
| 141 | + * |
| 142 | + * @param {string} trackingId see `'config'` command signature [1] |
| 143 | + * @param {object} options see `'config'` command signature [1] |
| 144 | + * @returns {function} A tracker function to replace invocation of `gtag` that |
| 145 | + * honours Do-Not-Track and automatically adds the `send_to` key to the |
| 146 | + * commands [2]. |
| 147 | + */ |
| 148 | +function configureBoundTracker(trackingId, options) { |
| 149 | + if (doNotTrack()) { return NO_OP; } |
| 150 | + |
| 151 | + options = options || {}; |
| 152 | + var groups = options.groups; |
| 153 | + telemetry._gtag('config', trackingId, options); |
| 154 | + return trackingFunction; |
| 155 | + |
| 156 | + function trackingFunction(command, label, options) { |
| 157 | + if (doNotTrack()) { return; } |
| 158 | + |
| 159 | + options = options || {}; |
| 160 | + if (groups) { |
| 161 | + options.send_to = groups; |
| 162 | + } |
| 163 | + telemetry._gtag(command, label, options); |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +function injectScript(src, callback) { |
| 168 | + var script = document.createElement('script'); |
| 169 | + script.src = src; |
| 170 | + script.crossorigin = 'anonymous'; |
| 171 | + script.addEventListener('load', function () { |
| 172 | + if (callback) { |
| 173 | + callback(null, true); |
| 174 | + } |
| 175 | + }); |
| 176 | + script.addEventListener('error', function (err) { |
| 177 | + if (callback) { |
| 178 | + callback(err); |
| 179 | + } |
| 180 | + }); |
| 181 | + document.head.appendChild(script); |
| 182 | + return script; |
| 183 | +} |
| 184 | + |
| 185 | +// IE9/IE10 uses a prefixed version while MS Edge sets the property in |
| 186 | +// `window` instead of `navigator`: |
| 187 | +// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/doNotTrack#Browser_compatibility |
| 188 | +function doNotTrack() { |
| 189 | + return navigator.doNotTrack === '1' || |
| 190 | + navigator.msDoNotTrack === '1' || |
| 191 | + window.doNotTrack === '1'; |
| 192 | +} |
| 193 | + |
| 194 | +function onlyOnce(fn) { |
| 195 | + var called = false; |
| 196 | + return function () { |
| 197 | + if (called) { return; } |
| 198 | + var returnValue = fn.apply(this, arguments); |
| 199 | + called = true; |
| 200 | + return returnValue; |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +})(window); |
0 commit comments