forked from aleross/angular-segment-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.js
371 lines (298 loc) · 12.2 KB
/
segment.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
angular.module('ngSegment', []);
angular.module('ngSegment').constant('segmentDefaultConfig', {
// API key: The https://segment.com API key to be used when loading analytics.js.
// Must be set before loading the analytics.js script.
apiKey: null,
// Autoload: if true, analytics.js will be asynchronously
// loaded after the app's .config() cycle has ended
autoload: true,
// Load delay: number of milliseconds to defer loading
// analytics.js.
loadDelay: 0,
// Condition: callback function to be checked before making an API call
// against segment. Can be dependency-injected, and is passed the method name
// and arguments of the analytics.js call being made. Useful for disabling
// certain or all analytics.js functionality for certain users or in certain
// application states.
condition: null,
// Debug: turns debug statements on/off. Useful during development.
debug: false,
// Methods: the analytics.js methods that the service creates queueing stubs for.
methods: [
'trackSubmit',
'trackClick',
'trackLink',
'trackForm',
'pageview',
'identify',
'reset',
'group',
'track',
'ready',
'alias',
'page',
'once',
'off',
'on',
],
// Tag: the tag used in debug log statements
tag: '[ngSegment] ',
});
(function (module) {
function SegmentLoader(hasLoaded) {
this.hasLoaded = hasLoaded || false;
this.load = function (apiKey, delayMs) {
if (window.analytics.initialized) {
console.warn('Warning: Segment analytics has already been initialized. Did you already load the library?');
}
if (this.hasLoaded) {
throw new Error('Attempting to load Segment twice.');
} else {
// Only load if we've been given or have set an API key
if (apiKey) {
// Prevent double .load() calls
this.hasLoaded = true;
window.setTimeout(function () {
// Create an async script element based on your key.
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = (document.location.protocol === 'https:'
? 'https://' : 'http://')
+ 'cdn.segment.com/analytics.js/v1/'
+ apiKey + '/analytics.min.js';
script.onerror = function () {
console.error('Error loading Segment library.');
};
// Insert our script next to the first script element.
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(script, first);
}, delayMs);
} else {
throw new Error('Cannot load Analytics.js without an API key.');
}
}
};
}
function SegmentLoaderProvider() {
// Inherit .load()
SegmentLoader.call(this);
this.$get = function () {
return new SegmentLoader(this.hasLoaded);
};
}
// Register with Angular
module.provider('segmentLoader', SegmentLoaderProvider);
})(angular.module('ngSegment'));
(function (module) {
var analytics = window.analytics = window.analytics || [];
// Invoked flag, to make sure the snippet
// is never invoked twice.
if (analytics.invoked) {
console.error('Segment or ngSegment included twice.');
} else {
analytics.invoked = true;
}
// Define a factory to create stubs. These are placeholders
// for methods in Analytics.js so that you never have to wait
// for it to load to actually record data. The `method` is
// stored as the first argument, so we can replay the data.
analytics.factory = function (method) {
return function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(method);
analytics.push(args);
return analytics;
};
};
/**
* Segment service
* @param config
* @constructor
*/
function Segment(config) {
this.config = config;
// Checks condition before calling Segment method
this.factory = function (method) {
var _this = this;
return function () {
// If a condition has been set, only call the Segment method if it returns true
if (_this.config.condition && !_this.config.condition(method, arguments)) {
_this.debug('Not calling method, condition returned false.', {
method: method,
arguments: arguments,
});
return;
}
// No condition set, call the Segment method
_this.debug('Calling method ' + method + ' with arguments:', arguments);
return window.analytics[method].apply(analytics, arguments);
};
};
}
/**
* Methods available on both segment service and segmentProvider
* @type {{init: Function, debug: Function}}
*/
Segment.prototype = {
// Creates analytics.js method stubs
init: function () {
for (var i = 0; i < this.config.methods.length; i++) {
var key = this.config.methods[i];
// Only create analytics stub if it doesn't already exist
if (!analytics[key]) {
analytics[key] = analytics.factory(key);
}
this[key] = this.factory(key);
}
},
debug: function () {
if (this.config.debug) {
arguments[0] = this.config.tag + arguments[0];
console.log.apply(console, arguments);
return true;
}
},
};
/**
* Segment provider available during .config() Angular app phase. Inherits from Segment prototype.
* @param segmentDefaultConfig
* @constructor
* @extends Segment
*/
function SegmentProvider(segmentDefaultConfig) {
this.config = angular.copy(segmentDefaultConfig);
// Stores any analytics.js method calls
this.queue = [];
// Overwrite Segment factory to queue up calls if condition has been set
this.factory = function (method) {
var queue = this.queue;
return function () {
// Defer calling analytics.js methods until the service is instantiated
queue.push({ method: method, arguments: arguments });
};
};
// Create method stubs using overridden factory
this.init();
this.setKey = function (apiKey) {
this.config.apiKey = apiKey;
this.validate('apiKey');
return this;
};
this.setLoadDelay = function (milliseconds) {
this.config.loadDelay = milliseconds;
this.validate('loadDelay');
return this;
};
this.setCondition = function (callback) {
this.config.condition = callback;
this.validate('condition');
return this;
};
this.setEvents = function (events) {
this.events = events;
return this;
};
this.setConfig = function (config) {
if (!angular.isObject(config)) {
throw new Error(this.config.tag + 'Config must be an object.');
}
angular.extend(this.config, config);
// Validate new settings
var _this = this;
Object.keys(config).forEach(function (key) {
_this.validate(key);
});
return this;
};
this.setAutoload = function (bool) {
this.config.autoload = !!bool;
return this;
};
this.setDebug = function (bool) {
this.config.debug = !!bool;
return this;
};
var validations = {
apiKey: function (config) {
if (!angular.isString(config.apiKey) || !config.apiKey) {
throw new Error(config.tag + 'API key must be a valid string.');
}
},
loadDelay: function (config) {
if (!angular.isNumber(config.loadDelay)) {
throw new Error(config.tag + 'Load delay must be a number.');
}
},
condition: function (config) {
if (!angular.isFunction(config.condition) &&
!(angular.isArray(config.condition) &&
angular.isFunction(config.condition[config.condition.length - 1]))
) {
throw new Error(config.tag + 'Condition callback must be a function or array.');
}
},
};
// Allows validating a specific property after set[Prop]
// or all config after provider/constant config
this.validate = function (property) {
if (typeof validations[property] === 'function') {
validations[property](this.config);
}
};
this.createService = function ($injector, segmentLoader) {
// Apply user-provided config constant if it exists
if ($injector.has('segmentConfig')) {
var constant = $injector.get('segmentConfig');
if (!angular.isObject(constant)) {
throw new Error(this.config.tag + 'Config constant must be an object.');
}
angular.extend(this.config, constant);
this.debug('Found segment config constant');
// Validate settings passed in by constant
var _this = this;
Object.keys(constant).forEach(function (key) {
_this.validate(key);
});
}
// Autoload Segment on service instantiation if an API key has been set via the provider
if (this.config.autoload) {
this.debug('Autoloading Analytics.js');
if (this.config.apiKey) {
segmentLoader.load(this.config.apiKey, this.config.loadDelay);
} else {
this.debug(this.config.tag + ' Warning: API key is not set and autoload is not disabled.');
}
}
// Create dependency-injected condition
if (typeof this.config.condition === 'function' ||
(angular.isArray(this.config.condition) &&
typeof this.config.condition[this.config.condition.length - 1] === 'function')
) {
var condition = this.config.condition;
this.config.condition = function (method, params) {
return $injector.invoke(condition, condition, { method: method, params: params });
};
}
// Pass any provider-set configuration down to the service
var segment = new Segment(angular.copy(this.config));
// Transfer events if set
if (this.events) {
segment.events = angular.copy(this.events);
}
// Set up service method stubs
segment.init();
// Play back any segment calls that were made against the provider now that the
// condition callback has been injected with dependencies
this.queue.forEach(function (item) {
segment[item.method].apply(segment, item.arguments);
});
return segment;
};
// Returns segment service and creates dependency-injected condition callback, if provided
this.$get = ['$injector', 'segmentLoader', this.createService];
}
SegmentProvider.prototype = Object.create(Segment.prototype);
// Register with Angular
module.provider('segment', ['segmentDefaultConfig', SegmentProvider]);
})(angular.module('ngSegment'));