-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrackingInfo.js
More file actions
156 lines (129 loc) · 5.16 KB
/
Copy pathTrackingInfo.js
File metadata and controls
156 lines (129 loc) · 5.16 KB
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
/* global window, define, Uint8Array: false */
define(['./Stopwatch', './uuid'], function(Stopwatch, generateUUID) {
'use strict';
function firstDefined() {
var args = [].slice.call(arguments),
result = args.shift();
while (args.length && result === undefined) {
result = args.shift();
}
return result;
}
function getValue(params) {
var args = [].slice.call(arguments),
def = args.pop(),
names = args.slice(1);
return names.reduce(function (prev, name) {
return firstDefined((params.data || {})[name], params[name], prev);
}, def);
}
function clone(obj) {
return Object.keys(obj).reduce(function copy(result, key) {
return result[key] = obj[key], result;
}, {});
}
/**
* Encapsulates the tracking information provided by all
* tracking sources into a single consistent schema for
* consumption by collectors.
* @class TrackingInfo
* @param {Object} params An object whose properties will
* be examined to set properties on the TrackingInfo
* instance.
* @example
* var info = new TrackingInfo({
* type: 'event',
* action: 'click',
* label: 'custom label'
* });
*/
return function TrackingInfo(params) {
if (!(this instanceof TrackingInfo)) {
return new TrackingInfo(params);
}
/**
* @member {Object} [TrackingInfo#data] Any optional data
* associated with the current instance.
*/
this.data = clone(params.data || {});
/**
* @member {Array} [TrackingInfo#tags] Any optional strings
* to associate with the current instance.
*/
this.tags = getValue(params, 'tags', []);
/**
* @member {Number} [TrackingInfo#count=1] Typically used to
* indicate the number of times an event,
* mark, or measure has been collected.
*/
this.count = getValue(params, 'count', 1);
/**
* @member {String} [TrackingInfo#type="unknown"] The type
* of this instance. Built-in types include
* 'event', 'timer', 'mark', 'measure',
* 'network', 'context', 'metric', and
* 'dimension'
*/
this.type = getValue(params, 'type', 'unknown');
/**
* @member {String} [TrackingInfo#id] The unique id to associate
* with this instance. If not provided, a universally
* unique identifier will be generated automatically.
*/
this.id = getValue(params, 'id', undefined) || generateUUID();
/**
* @member {Number} [TrackingInfo#start] The number of milliseconds since
* 1/1/1970 before this instance was started. If not
* provided, defaults to the current date and time.
*/
this.start = getValue(params, 'start', 'startTime', Stopwatch.now());
/**
* @member {Number} [TrackingInfo#stop] The number of milliseconds since
* 1/1/1970 before this instance was stopped. If not
* provided, defaults to the start time.
*/
this.stop = getValue(params, 'stop', 'stopTime', 'end', 'endTime', this.start);
/**
* @member {Number} [TrackingInfo#duration] The number of milliseconds
* between this instance's stop time and start time.
*/
this.duration = this.stop - this.start;
/**
* @member {String} [TrackingInfo#label] The label to associate with
* this instance.
*/
this.label = getValue(params, 'label', 'description', undefined);
/**
* @member {String} [TrackingInfo#action] The action to associate with
* this instance.
*/
this.action = getValue(params, 'action', undefined);
/**
* @member {String} [TrackingInfo#category] The category to associate
* with this instance.
*/
this.category = getValue(params, 'category', undefined);
/**
* @member {*} [TrackingInfo#variable] Some custom value to associate
* with this instance. Typically used in conjunction with
* data events to register some conditional state of the
* application (such as the number of options available
* to the user at a given decision point).
*/
this.variable = getValue(params, 'variable', undefined);
/**
* @member {Array} [TrackingInfo#children] Contains any nested TrackingInfo
* instances. Timers and measures can both have children, but
* technically any custom data object with a `children` array
* will be attempted to be converted into TrackingInfo instances.
*/
this.children = getValue(params, 'children', []).map(TrackingInfo);
Object.keys(this).forEach(function(key) {
delete this.data[key];
}.bind(this));
this.toString = function toString() {
return 'TrackingInfo: ' + JSON.stringify(this, null, 2);
};
Object.freeze(this);
};
});