-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracking.js
538 lines (491 loc) · 20.8 KB
/
tracking.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
537
538
/**
* mo-tracking - A tracking module.
* @version v0.1.0
* @link https://github.com/moreonion/tracking
* @license AGPL-3.0
*/
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.tracking = mod.exports;
}
})(this, function (exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Tracker = Tracker;
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
/* global window global */
/**
* Tracking module.
*
* @module tracking
*/
var root;
if (typeof window !== 'undefined') {
root = window;
} else if (typeof global !== 'undefined') {
root = global;
} else {
root = {};
}
var MARKED_VALUE = '1';
var UNMARKED_VALUE = '0';
/**
* Creates a Tracker instance.
*
* @constructor
* @this {Tracker}
* @param {object} [options] - the options
* @param {string} [options.root=window|global|{}] - the root object<br>
* Per default tries to set this to <code>window</code> (Browsers),
* <code>global</code> (nodejs), or an empty object <code>{}</code>.
* @param {string} [options.gaFnName='ga'] - Google Analytics command queue
* function name
* @param {string} [options.uniqueEvents=true] - send events only once
* @param {function}
* [options.storageIdentifierFn=this.defaultStorageIdentifierFn] - function to
* determine the storage identifier of an action<br>
* <br>
* The <code>storageIdentifierFn</code> needs at least an
* <code>actionName</code> (string) as first parameter. Optional parameter
* object which can be used in determining the storage key are:
* <code>object</code>, <code>event</code>, and <code>action</code>.
* @param {function} [options.uniquifyFn=this.defaultUniquifyingFn] - function to
* determine if something should count as unique compared to the.<br>
* Default is to use <code>window.location.pathname</code>
* @param {string} [options.storage='local'] - the storage to use, for now<br>
* This could be <code>local</code> for <code>root.localStorage</code> or
* <code>session</code> for <code>root.sessionStorage</code>
* @param {object} [options.eventDefaults={}] - the default event
* @param {string} [options.eventDefaults.hitType='event'] - the default hit type
* @param {string} [options.eventDefaults.eventCategory] - the default event category
* @param {string} [options.eventDefaults.eventAction] - the default event action
* @param {string} [options.eventDefaults.eventLabel] - the default event label
* @param {number} [options.eventDefaults.eventValue] - the default event value
* @param {string} [options.defaultCurrency='EUR'] - the default currency for enhanced ecommerce
* @public
* @todo make storage customizable with own implementation
* @todo factor out storage into own module
* @todo check for window.GoogleAnalyticsObject to set gaFnName
* @todo load from storage on a possible second page/step?
*/
function Tracker(options) {
/**
* The default settings.
* @var {object}
* @inner
*/
var defaults = {
root: root,
gaFnName: 'ga',
storage: 'local',
defaultCurrency: 'EUR',
storageIdentifierFn: this.defaultStorageIdentifierFn,
uniquifyFn: this.defaultUniquifyingFn,
uniqueEvents: true,
eventDefaults: {
hitType: 'event'
}
};
/**
* The active settings.
* @member {object} settings
* @instance
* @memberof module:tracking~Tracker
*/
this.settings = _extends(defaults, options);
/**
* The state storage to use.
*
* Can be `localStorage` or `sessionStorage`
*
* @member {Storage} storage to use
* @instance
* @memberof module:tracking~Tracker
* @todo add storage `cookie`
*/
this.storage = null;
if (this.settings.storage === 'local') {
this.storage = this.settings.root['localStorage'];
}
if (this.settings.storage === 'session') {
this.storage = this.settings.root['sessionStorage'];
}
/**
* The Google Analytics command queue to use
*
* @member {function} ga() to use
* @instance
* @memberof module:tracking~Tracker
*/
this.ga = null;
if (this._checkGA()) {
this.ga = this.settings.root[this.settings.gaFnName];
} else {
console.error('Tracker: "' + this.settings.gaFnName + '" is not defined yet or not a function'); // eslint-disable-line no-console
}
}
/**
* Check whether the Google Analytics queue function is available
*
* @private
* @returns {boolean}
*/
Tracker.prototype._checkGA = function () {
return typeof this.settings.root[this.settings.gaFnName] === 'function';
};
/**
* Ensure the plugin is loaded.
*
* @private
*/
Tracker.prototype._ensurePlugin = function () {
var name = arguments.length <= 0 || arguments[0] === undefined ? 'ec' : arguments[0];
this.ga('require', name);
};
/**
* Set basic properties for donation.
*
* @param {object} [options] - the options
* @param {string} [options.defaultCurrency='EUR'] - the default currency for enhanced ecommerce
*/
Tracker.prototype.initializeDonation = function (options) {
var defaults = {
currency: this.settings.defaultCurrency
};
var settings = _extends(defaults, options);
this._ensurePlugin('ec');
this.ga('set', '&cu', settings.currency);
};
/**
* Validate an object.
*
* @param {string} [type='event'] - the type to validate against<br>
* Can be on of: <code>event</code>, <code>impression</code>,
* <code>product</code>, <code>action</action>, or <code>purchase</code>.
* @param {object} [object={}] - the object to validate
* @private
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference
*/
Tracker.prototype._validateObject = function () {
var type = arguments.length <= 0 || arguments[0] === undefined ? 'event' : arguments[0];
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
// check if we really get an object to validate against
if (typeof object !== 'object' || object === null) {
return false;
}
// initialize
var success = true;
var oneOfAttributesRequired = [];
var allAttributesRequired = [];
// setup requirements
if (type === 'event') {
// see https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields
allAttributesRequired = ['eventCategory', 'eventAction'];
} else if (type === 'impression' || type === 'product') {
// see https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#impression-data
// https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#product-data
oneOfAttributesRequired = ['id', 'name'];
} else if (type === 'action') {
success = true;
} else if (type === 'purchase') {
// see https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce#action-data
allAttributesRequired = ['id'];
}
if (oneOfAttributesRequired.length > 0) {
// one-of needs reverted start state (false)
success = false;
oneOfAttributesRequired.forEach(function (el) {
if (el in object) {
success = true;
}
});
}
allAttributesRequired.forEach(function (el) {
success = success && el in object;
});
return success;
};
/**
* Send an impression.
*
* @param {impressionFieldObject} [object] - the
* <code>impressionFieldObject</code> to send
* @param {event} [object] - the event to send
*/
Tracker.prototype.sendImpression = function (impression) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'impression'
});
var donationEvent = _extends(ev, event);
if (this._validateObject('impression', impression) && this._validateObject('event', donationEvent)) {
if (this.settings.uniqueEvents && !this.isSent('impression', impression, donationEvent)) {
this.ga('ec:addImpression', impression);
this.ga('send', donationEvent);
this.markSent('impression', impression, donationEvent);
}
} else {
console.error('Tracker: not a valid impressionFieldObject and/or event.'); // eslint-disable-line no-console
}
};
/**
* Send a detail view.
*
* @param {productFieldObject} [object] - the
* <code>productFieldObject</code> to send
* @param {event} [object] - the event to send
*/
Tracker.prototype.sendView = function (product) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'view'
});
var donationEvent = _extends(ev, event);
if (this._validateObject('product', product) && this._validateObject('event', donationEvent)) {
if (this.settings.uniqueEvents && !this.isSent('view', product, donationEvent)) {
this.ga('ec:addProduct', product);
this.ga('ec:setAction', 'detail');
this.ga('send', donationEvent);
this.markSent('view', product, donationEvent);
}
} else {
console.error('Tracker: not a valid productFieldObject and/or event.'); // eslint-disable-line no-console
}
};
/**
* Send a product add.
*
* @param {productFieldObject} [object] - the
* <code>productFieldObject</code> to send
* @param {event} [object] - the event to send
*/
Tracker.prototype.sendAdd = function (product) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'add to cart'
});
var donationEvent = _extends(ev, event);
if (this._validateObject('product', product) && this._validateObject('event', donationEvent)) {
if (this.settings.uniqueEvents && !this.isSent('add', product, donationEvent)) {
this.ga('ec:addProduct', product);
this.ga('ec:setAction', 'add');
this.ga('send', donationEvent);
this.markSent('add', product, donationEvent);
}
} else {
console.error('Tracker: not a valid productFieldObject and/or event.'); // eslint-disable-line no-console
}
};
/**
* Send a checkout begin.
*
* @param {productFieldObject} [object] - the
* <code>productFieldObject</code> to send
* @param {event} [object] - the event to send
* @param {action} [object] - the action parameters
*/
Tracker.prototype.sendBeginCheckout = function (product) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var action = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'checkout'
});
var donationEvent = _extends(ev, event);
var actionDefaults = {
step: 1
};
var checkoutAction = _extends(actionDefaults, action);
if (this._validateObject('product', product) && this._validateObject('event', donationEvent) && this._validateObject('action', checkoutAction)) {
if (this.settings.uniqueEvents && !this.isSent('checkoutBegin', product, donationEvent, checkoutAction)) {
this.ga('ec:addProduct', product);
this.ga('ec:setAction', 'checkout', checkoutAction);
this.ga('send', donationEvent);
this.markSent('checkoutBegin', product, donationEvent, checkoutAction);
}
} else {
console.error('Tracker: not a valid productFieldObject and/or event and/or checkout action.'); // eslint-disable-line no-console
}
};
/**
* Send a checkout end.
*
* @param {productFieldObject} [object] - the
* <code>productFieldObject</code> to send
* @param {event} [object] - the event to send
* @param {action} [object] - the action parameters
*/
Tracker.prototype.sendEndCheckout = function (product) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var action = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'checkout'
});
var donationEvent = _extends(ev, event);
var actionDefaults = {
step: 2
};
var checkoutAction = _extends(actionDefaults, action);
if (this._validateObject('product', product) && this._validateObject('event', donationEvent) && this._validateObject('action', checkoutAction)) {
if (this.settings.uniqueEvents && !this.isSent('checkoutEnd', product, donationEvent, checkoutAction)) {
this.ga('ec:addProduct', product);
this.ga('ec:setAction', 'checkout', checkoutAction);
this.ga('send', donationEvent);
this.markSent('checkoutEnd', product, donationEvent, checkoutAction);
}
} else {
console.error('Tracker: not a valid productFieldObject and/or event and/or checkout action.'); // eslint-disable-line no-console
}
};
/**
* Send a purchase.
*
* @param {productFieldObject} [object] - the
* <code>productFieldObject</code> to send
* @param {event} [object] - the event to send
* @param {action} [object] - the action parameters
*/
Tracker.prototype.sendPurchase = function (product) {
var event = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var action = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var ev = _extends(this.settings.eventDefaults, {
eventCategory: 'donation',
eventAction: 'purchase'
});
var donationEvent = _extends(ev, event);
var actionDefaults = {
currency: this.settings.defaultCurrency
};
var purchaseAction = _extends(actionDefaults, action);
if (this._validateObject('product', product) && this._validateObject('event', donationEvent) && this._validateObject('purchase', purchaseAction)) {
if (this.settings.uniqueEvents && !this.isSent('purchase', product, donationEvent, purchaseAction)) {
this.ga('ec:addProduct', product);
this.ga('ec:setAction', 'purchase', purchaseAction);
this.ga('send', donationEvent);
this.markSent('purchase', product, donationEvent, purchaseAction);
}
} else {
console.error('Tracker: not a valid productFieldObject and/or event and/or purchase action.'); // eslint-disable-line no-console
}
};
/**
* Default uniquifiying function.
*
* @returns {string}
*/
Tracker.prototype.defaultUniquifyingFn = function () {
if ('location' in root) {
return root.location.pathname;
}
};
/**
* Default storage identifier function.
*
* @param {string} actionName - the action name
* @param {object} [object={}] - the object, i.e. product, impression
* @returns {string}
*/
Tracker.prototype.defaultStorageIdentifierFn = function (actionName) {
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var prefix = this.settings.uniquifyFn.call(this);
var objectId = typeof object.id === 'undefined' ? '' : object.id;
return prefix + '::' + actionName + '::' + objectId;
};
/**
* Check if an action was sent.
*
* @param {string} actionName - the action to check
* @param {object} [object={}] - the object, i.e. product, impression
* @param {event} [event={}] - the event parameters
* @param {action} [action={}] - the action parameters
* @returns {boolean}
*/
Tracker.prototype.isSent = function (actionName) {
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var event = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var action = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var identifier = this.settings.storageIdentifierFn.call(this, actionName, object, event, action);
var val = this.storage.getItem(identifier);
if (val === MARKED_VALUE) {
return true;
} else {
return false;
}
};
/**
* Mark an action as sent.
*
* @param {string} actionName - the action to mark sent
* @param {object} [object={}] - the object, i.e. product, impression
* @param {event} [event={}] - the event parameters
* @param {action} [action={}] - the action parameters
*/
Tracker.prototype.markSent = function (actionName) {
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var event = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var action = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var identifier = this.settings.storageIdentifierFn.call(this, actionName, object, event, action);
this.storage.setItem(identifier, MARKED_VALUE);
};
/**
* Unmark an action as sent.
*
* @param {string} actionName - the action to unmark sent
* @param {object} [object={}] - the object, i.e. product, impression
* @param {event} [event={}] - the event parameters
* @param {action} [action={}] - the action parameters
*/
Tracker.prototype.unmarkSent = function (actionName) {
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var event = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var action = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var identifier = this.settings.storageIdentifierFn.call(this, actionName, object, event, action);
this.storage.setItem(identifier, UNMARKED_VALUE);
};
/**
* Remove an action state from storage.
*
* @param {string} actionName - the action state to remove
* @param {object} [object={}] - the object, i.e. product, impression
* @param {event} [event={}] - the event parameters
* @param {action} [action={}] - the action parameters
*/
Tracker.prototype.removeSent = function (actionName) {
var object = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var event = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var action = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
var identifier = this.settings.storageIdentifierFn.call(this, actionName, object, event, action);
this.storage.removeItem(identifier);
};
/**
* Reset the storage.
*/
Tracker.prototype.resetStorage = function () {
this.storage.clear();
};
// vim: set et ts=4 sw=4 :
});