|
| 1 | +/* global localStorage, location, Raven */ |
| 2 | +(function (window) { |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +if (!('MozillaResearch' in window)) { |
| 6 | + window.MozillaResearch = {}; |
| 7 | +} |
| 8 | + |
| 9 | +if (!('telemetry' in window.MozillaResearch)) { |
| 10 | + window.MozillaResearch.telemetry = {}; |
| 11 | +} |
| 12 | + |
| 13 | +var navigator = window.navigator; |
| 14 | +var telemetry = window.MozillaResearch.telemetry; |
| 15 | + |
| 16 | +telemetry.ga = { |
| 17 | + create: function (trackingId, cookieDomain, name, fieldsObject) { |
| 18 | + window.ga('create', trackingId, cookieDomain, name, fieldsObject); |
| 19 | + return function (command) { |
| 20 | + if (navigator.doNotTrack === '1') { return; } |
| 21 | + var args = Array.prototype.slice.call(arguments); |
| 22 | + if (name && command !== 'provide') { |
| 23 | + command = name + '.' + command; |
| 24 | + args[0] = command; |
| 25 | + } |
| 26 | + window.ga.apply(undefined, args); |
| 27 | + }; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +var NO_OP = function () {}; |
| 32 | + |
| 33 | +telemetry.performance = { |
| 34 | + mark: NO_OP, |
| 35 | + measure: NO_OP |
| 36 | +}; |
| 37 | + |
| 38 | +telemetry.start = function (config) { |
| 39 | + config = config || {}; |
| 40 | + if (navigator.doNotTrack === '1') { |
| 41 | + return; |
| 42 | + } |
| 43 | + if (config.errorLogging) { |
| 44 | + startErrorLogging(); |
| 45 | + } |
| 46 | + if (config.analytics) { |
| 47 | + var researchAnalytics = startAnalytics(); |
| 48 | + if (config.performance) { |
| 49 | + configurePerformanceAPI(researchAnalytics); |
| 50 | + } |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +setupAnalytics(); |
| 55 | + |
| 56 | +function setupAnalytics() { |
| 57 | + window.ga = window.ga || function () { |
| 58 | + (window.ga.q = (window.ga.q || [])).push(arguments) |
| 59 | + }; |
| 60 | + window.ga.l = +(new Date()); |
| 61 | + |
| 62 | + if (navigator.doNotTrack === '1') { |
| 63 | + return; |
| 64 | + } |
| 65 | + injectScript('https://www.google-analytics.com/analytics.js', function (err) { |
| 66 | + if (err) { |
| 67 | + console.warn('Could not load Analytics.js script:', err); |
| 68 | + return; |
| 69 | + } |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function startErrorLogging() { |
| 74 | + injectScript('https://cdn.ravenjs.com/3.22.3/console/raven.min.js', function (err) { |
| 75 | + if (err) { |
| 76 | + console.warn('Could not load Raven.js script:', err); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (!('Raven' in window)) { |
| 80 | + console.warn('Could not find `window.Raven` global'); |
| 81 | + return; |
| 82 | + } |
| 83 | + configureRaven(); |
| 84 | + }); |
| 85 | + |
| 86 | + function configureRaven () { |
| 87 | + console.log('Raven.js script loaded'); |
| 88 | + Raven.config('https://e359be9fb9324addb0dc97b664cf5ee6@sentry.io/294878') |
| 89 | + .install(); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +function startAnalytics() { |
| 94 | + var CURRENT_VERSION = '1.0.1'; |
| 95 | + var ga = telemetry.ga.create('UA-77033033-6', 'auto', 'mozillaResearch'); |
| 96 | + ga('set', 'dimension1', CURRENT_VERSION); |
| 97 | + ga('send', 'pageview'); |
| 98 | + return ga; |
| 99 | +} |
| 100 | + |
| 101 | +function configurePerformanceAPI(ga) { |
| 102 | + telemetry.performance = { |
| 103 | + mark: function (name) { |
| 104 | + performance.mark(name); |
| 105 | + }, |
| 106 | + |
| 107 | + measure: function (name, start, end) { |
| 108 | + if (navigator.doNotTrack === '1') { |
| 109 | + return; |
| 110 | + } |
| 111 | + performance.measure(name, start, end); |
| 112 | + var performanceEntry = performance.getEntriesByName(name)[0]; |
| 113 | + var duration = performanceEntry.duration; |
| 114 | + ga('send', 'event', 'Performance', name, undefined, duration); |
| 115 | + } |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +function injectScript (src, callback) { |
| 120 | + var script = document.createElement('script'); |
| 121 | + script.src = src; |
| 122 | + script.crossorigin = 'anonymous'; |
| 123 | + script.addEventListener('load', function () { |
| 124 | + if (callback) { |
| 125 | + callback(null, true); |
| 126 | + } |
| 127 | + }); |
| 128 | + script.addEventListener('error', function (err) { |
| 129 | + if (callback) { |
| 130 | + callback(err); |
| 131 | + } |
| 132 | + }); |
| 133 | + document.head.appendChild(script); |
| 134 | + return script; |
| 135 | +} |
| 136 | + |
| 137 | +})(window); |
0 commit comments