This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
metrics.js
536 lines (469 loc) · 15.8 KB
/
metrics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* A metrics module!
*
* An instantiated metrics object has two primary APIs:
*
* metrics.logEvent(<event_name>);
* metrics.startTimer(<timer_name>)/metrics.stopTimer(<timer_name);
*
* Metrics are automatically sent to the server on window.unload
* but can also be sent by calling metrics.flush();
*/
define(function (require, exports, module) {
'use strict';
const $ = require('jquery');
const _ = require('underscore');
const Constants = require('lib/constants');
const Backbone = require('backbone');
const Duration = require('duration');
const Environment = require('lib/environment');
const p = require('lib/promise');
const speedTrap = require('speedTrap');
const Strings = require('lib/strings');
const xhr = require('lib/xhr');
// Speed trap is a singleton, convert it
// to an instantiable function.
const SpeedTrap = function () {};
SpeedTrap.prototype = speedTrap;
const ALLOWED_FIELDS = [
'broker',
'context',
'duration',
'entrypoint',
'events',
'experiments',
'flowBeginTime',
'flowId',
'flushTime',
'isSampledUser',
'lang',
'marketing',
'migration',
'navigationTiming',
'numStoredAccounts',
'referrer',
'screen',
'service',
'startTime',
'timers',
'uniqueUserId',
'utm_campaign',
'utm_content',
'utm_medium',
'utm_source',
'utm_term'
];
var DEFAULT_INACTIVITY_TIMEOUT_MS = new Duration('2m').milliseconds();
var NOT_REPORTED_VALUE = 'none';
var UNKNOWN_CAMPAIGN_ID = 'unknown';
// convert a hash of metrics impressions into an array of objects.
function flattenHashIntoArrayOfObjects (hashTable) {
return _.reduce(hashTable, function (memo, key) {
return memo.concat(_.map(key, function (value) {
return value;
}));
}, []);
}
function marshallFlowEvent (eventName, viewName) {
if (! viewName) {
return `flow.${eventName}`;
}
// Strip out the `oauth.` prefix if present because
// OAuthiness is already encoded in the service property.
return `flow.${viewName.replace(/^oauth\./, '')}.${eventName}`;
}
function Metrics (options = {}) {
this._speedTrap = new SpeedTrap();
this._speedTrap.init();
// `timers` and `events` are part of the public API
this.timers = this._speedTrap.timers;
this.events = this._speedTrap.events;
this._window = options.window || window;
this._able = options.able;
this._activeExperiments = {};
this._brokerType = options.brokerType || NOT_REPORTED_VALUE;
this._clientHeight = options.clientHeight || NOT_REPORTED_VALUE;
this._clientWidth = options.clientWidth || NOT_REPORTED_VALUE;
// by default, send the metrics to the content server.
this._collector = options.collector || '';
this._context = options.context || Constants.CONTENT_SERVER_CONTEXT;
this._devicePixelRatio = options.devicePixelRatio || NOT_REPORTED_VALUE;
this._entrypoint = options.entrypoint || NOT_REPORTED_VALUE;
this._env = options.environment || new Environment(this._window);
this._eventMemory = {};
this._inactivityFlushMs = options.inactivityFlushMs || DEFAULT_INACTIVITY_TIMEOUT_MS;
// All user metrics are sent to the backend. Data is only
// reported to Heka and Datadog if `isSampledUser===true`.
this._isSampledUser = options.isSampledUser || false;
this._lang = options.lang || 'unknown';
this._marketingImpressions = {};
this._migration = options.migration || NOT_REPORTED_VALUE;
this._numStoredAccounts = options.numStoredAccounts || '';
this._referrer = this._window.document.referrer || NOT_REPORTED_VALUE;
this._screenHeight = options.screenHeight || NOT_REPORTED_VALUE;
this._screenWidth = options.screenWidth || NOT_REPORTED_VALUE;
this._service = options.service || NOT_REPORTED_VALUE;
// if navigationTiming is supported, the baseTime will be from
// navigationTiming.navigationStart, otherwise Date.now().
this._startTime = options.startTime || this._speedTrap.baseTime;
this._uniqueUserId = options.uniqueUserId || NOT_REPORTED_VALUE;
this._utmCampaign = options.utmCampaign || NOT_REPORTED_VALUE;
this._utmContent = options.utmContent || NOT_REPORTED_VALUE;
this._utmMedium = options.utmMedium || NOT_REPORTED_VALUE;
this._utmSource = options.utmSource || NOT_REPORTED_VALUE;
this._utmTerm = options.utmTerm || NOT_REPORTED_VALUE;
this._xhr = options.xhr || xhr;
}
_.extend(Metrics.prototype, Backbone.Events, {
ALLOWED_FIELDS: ALLOWED_FIELDS,
init () {
this._flush = _.bind(this.flush, this, true);
$(this._window).on('unload', this._flush);
// iOS will not send events once the window is in the background,
// meaning the `unload` handler is ineffective. Send events on blur
// instead, so events are not lost when a user goes to verify their
// email.
$(this._window).on('blur', this._flush);
// Set the initial inactivity timeout to clear navigation timing data.
this._resetInactivityFlushTimeout();
},
destroy () {
$(this._window).off('unload', this._flush);
$(this._window).off('blur', this._flush);
this._clearInactivityFlushTimeout();
},
/**
* Send the collected data to the backend.
*
* @param {String} isPageUnloading
* @returns {Promise}
*/
flush (isPageUnloading) {
// Inactivity timer is restarted when the next event/timer comes in.
// This avoids sending empty result sets if the tab is
// just sitting there open with no activity.
this._clearInactivityFlushTimeout();
var filteredData = this.getFilteredData();
if (! this._isFlushRequired(filteredData, this._lastFlushedData)) {
return p();
}
this._lastFlushedData = filteredData;
this._speedTrap.events.clear();
this._speedTrap.timers.clear();
// numStoredAccounts should only be counted once by the backend
// for this user. After a flush, unset the value so it is not
// reported again.
this._numStoredAccounts = '';
const send = () => this._send(filteredData, isPageUnloading);
return send()
// Retry once in case of failure, then give up
.then(sent => sent || send());
},
/**
* Check if a flush is required for the given `data`. A flush is
* required if any data has changed since the last flush.
*
* @param {Object} data - potential data to flush
* @param {Object} lastFlushedData - last data that was flushed.
* @returns {Boolean}
* @private
*/
_isFlushRequired (data, lastFlushedData) {
if (! lastFlushedData) {
return true;
}
// Only check fields that are in the new payload. `data` could be
// a subset of `_lastFlushedData`, in which case no flush should occur.
return _.any(data, (value, key) => {
// these keys are distinct every flush attempt, ignore.
if (key === 'duration' || key === 'flushTime') {
return false;
}
// _.isEqual does a deep comparision of objects and arrays.
return ! _.isEqual(lastFlushedData[key], value);
});
},
_clearInactivityFlushTimeout () {
clearTimeout(this._inactivityFlushTimeout);
},
_resetInactivityFlushTimeout () {
this._clearInactivityFlushTimeout();
this._inactivityFlushTimeout =
setTimeout(() => {
this.logEvent('inactivity.flush');
this.flush();
}, this._inactivityFlushMs);
},
/**
* Get all the data, whether it's allowed to be sent or not.
*
* @returns {Object}
*/
getAllData () {
var loadData = this._speedTrap.getLoad();
var unloadData = this._speedTrap.getUnload();
var allData = _.extend({}, loadData, unloadData, {
broker: this._brokerType,
context: this._context,
entrypoint: this._entrypoint,
experiments: flattenHashIntoArrayOfObjects(this._activeExperiments),
flowBeginTime: this._flowBeginTime,
flowId: this._flowId,
flushTime: Date.now(),
isSampledUser: this._isSampledUser,
lang: this._lang,
marketing: flattenHashIntoArrayOfObjects(this._marketingImpressions),
migration: this._migration,
numStoredAccounts: this._numStoredAccounts,
referrer: this._referrer,
screen: {
clientHeight: this._clientHeight,
clientWidth: this._clientWidth,
devicePixelRatio: this._devicePixelRatio,
height: this._screenHeight,
width: this._screenWidth
},
service: this._service,
startTime: this._startTime,
uniqueUserId: this._uniqueUserId,
utm_campaign: this._utmCampaign, //eslint-disable-line camelcase
utm_content: this._utmContent, //eslint-disable-line camelcase
utm_medium: this._utmMedium, //eslint-disable-line camelcase
utm_source: this._utmSource, //eslint-disable-line camelcase
utm_term: this._utmTerm, //eslint-disable-line camelcase
});
// Create a deep copy of the data so that any modifications to contained
// objects or arrays do not affect the returned copy of the data.
return JSON.parse(JSON.stringify(allData));
},
/**
* Get the filtered data.
* Filtered data is data that is allowed to be sent,
* that is defined and not an empty string.
*
* @returns {Object}
*/
getFilteredData () {
var allowedData = _.pick(this.getAllData(), ALLOWED_FIELDS);
return _.pick(allowedData, function (value, key) {
return ! _.isUndefined(value) && value !== '';
});
},
_send (data, isPageUnloading) {
var url = this._collector + '/metrics';
var payload = JSON.stringify(data);
if (this._env.hasSendBeacon()) {
// Always use sendBeacon if it is available because:
// 1. it works asynchronously, even on unload.
// 2. user agents SHOULD make "multiple attempts to transmit the
// data in presence of transient network or server errors".
return p().then(() => {
return this._window.navigator.sendBeacon(url, payload);
});
}
// XHR is a fallback option because synchronous XHR has been deprecated,
// but we must call it synchronously in the unload case.
return this._xhr.ajax({
async: ! isPageUnloading,
contentType: 'application/json',
data: payload,
type: 'POST',
url: url
})
// Boolean return values imitate the behaviour of sendBeacon
.then(function () {
return true;
}, function () {
return false;
});
},
/**
* Log an event
*
* @param {String} eventName
*/
logEvent (eventName) {
this._resetInactivityFlushTimeout();
this.events.capture(eventName);
},
/**
* Log an event only if it never happened before during this page load.
*
* @param {String} eventName
*/
logEventOnce (eventName) {
if (! this._eventMemory[eventName]) {
this.logEvent(eventName);
this._eventMemory[eventName] = true;
}
},
/**
* Start a timer
*
* @param {String} timerName
*/
startTimer (timerName) {
this._resetInactivityFlushTimeout();
this.timers.start(timerName);
},
/**
* Stop a timer
*
* @param {String} timerName
*/
stopTimer (timerName) {
this._resetInactivityFlushTimeout();
this.timers.stop(timerName);
},
/**
* Log an error.
*
* @param {Error} error
*/
logError (error) {
this.logEvent(this.errorToId(error));
},
/**
* Convert an error to an identifier that can be used for logging.
*
* @param {Error} error
* @returns {String}
*/
errorToId (error) {
var id = Strings.interpolate('error.%s.%s.%s', [
error.context || 'unknown context',
error.namespace || 'unknown namespace',
error.errno || String(error)
]);
return id;
},
/**
* Log a view
*
* @param {String} viewName
*/
logView (viewName) {
this.logEvent(this.viewToId(viewName));
},
/**
* Log an event with the view name as a prefix
*
* @param {String} viewName
* @param {String} eventName
*/
logViewEvent (viewName, eventName) {
var event = Strings.interpolate('%(viewName)s.%(eventName)s', {
eventName: eventName,
viewName: viewName,
});
this.logEvent(event);
},
/**
* Convert a viewName to an identifier used in metrics.
*
* @param {String} viewName
* @return {String} identifier
*/
viewToId (viewName) {
// `screen.` is a legacy artifact from when each View was a screen.
// The idenifier is kept to avoid updating all metrics queries.
return 'screen.' + viewName;
},
/**
* Log when an experiment is shown to the user
*
* @param {String} choice - type of experiment
* @param {String} group - the experiment group (treatment or control)
*/
logExperiment (choice, group) {
if (! choice || ! group) {
return;
}
var experiments = this._activeExperiments;
if (! experiments[choice]) {
experiments[choice] = {};
}
experiments[choice][group] = {
choice: choice,
group: group
};
},
/**
* Log when a marketing snippet is shown to the user
*
* @param {String} campaignId - marketing campaign id
* @param {String} url - url of marketing link
*/
logMarketingImpression (campaignId, url) {
campaignId = campaignId || UNKNOWN_CAMPAIGN_ID;
var impressions = this._marketingImpressions;
if (! impressions[campaignId]) {
impressions[campaignId] = {};
}
impressions[campaignId][url] = {
campaignId: campaignId,
clicked: false,
url: url
};
},
/**
* Log whether the user clicked on a marketing link
*
* @param {String} campaignId - marketing campaign id
* @param {String} url - URL clicked.
*/
logMarketingClick (campaignId, url) {
campaignId = campaignId || UNKNOWN_CAMPAIGN_ID;
var impression = this.getMarketingImpression(campaignId, url);
if (impression) {
impression.clicked = true;
}
},
getMarketingImpression (campaignId, url) {
var impressions = this._marketingImpressions;
return impressions[campaignId] && impressions[campaignId][url];
},
setBrokerType (brokerType) {
this._brokerType = brokerType;
},
isCollectionEnabled () {
return this._isSampledUser;
},
logFlowBegin (flowId, flowBeginTime) {
// Don't emit a new flow.begin event unless flowId has changed.
if (flowId !== this._flowId) {
this._flowId = flowId;
this._flowBeginTime = flowBeginTime;
this.logFlowEvent('begin');
}
},
logFlowEvent (eventName, viewName) {
this.logEvent(marshallFlowEvent(eventName, viewName));
},
logFlowEventOnce (eventName, viewName) {
this.logEventOnce(marshallFlowEvent(eventName, viewName));
},
getFlowEventMetadata () {
const metadata = (this._flowModel && this._flowModel.attributes) || {};
return {
flowBeginTime: metadata.flowBegin,
flowId: metadata.flowId
};
},
setFlowModel (flowModel) {
this._flowModel = flowModel;
},
/**
* Log the number of stored accounts
*
* @param {Number} numStoredAccounts
*/
logNumStoredAccounts (numStoredAccounts) {
this._numStoredAccounts = numStoredAccounts;
}
});
module.exports = Metrics;
});