forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
527 lines (479 loc) · 15.7 KB
/
Copy pathcore.js
File metadata and controls
527 lines (479 loc) · 15.7 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
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
/*
* JavaScript tracker core for Snowplow: core.js
*
* Copyright (c) 2014 Snowplow Analytics Ltd. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
var payload = require('./payload.js');
var uuid = require('uuid');
/**
* Create a tracker core object
*
* @param boolean base64 Whether to base 64 encode contexts and unstructured event JSONs
* @param callback function Function applied to every payload dictionary object
* @return object Tracker core
*/
function trackerCore(base64, callback) {
// base 64 encoding should default to true
if (typeof base64 === 'undefined') {
base64 = true;
}
// Dictionary of key-value pairs which get added to every payload, e.g. tracker version
var payloadPairs = {};
/**
* Set a persistent key-value pair to be added to every payload
*
* @param string key Field name
* @param string value Field value
*/
function addPayloadPair(key, value) {
payloadPairs[key] = value;
}
/**
* Returns a copy of a JSON with undefined and null properties removed
*
* @param object eventJson JSON to clean
* @return object A cleaned copy of eventJson
*/
function removeEmptyProperties(eventJson) {
var ret = {};
for (var k in eventJson) {
if (eventJson[k] !== null && typeof eventJson[k] !== 'undefined') {
ret[k] = eventJson[k];
}
}
return ret;
}
/**
* Wraps an array of custom contexts in a self-describing JSON
*
* @param array contexts Array of custom context self-describing JSONs
* @return object Outer JSON
*/
function completeContexts(contexts) {
if (contexts && contexts.length) {
return {
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0',
data: contexts
};
}
}
/**
* Gets called by every trackXXX method
* Adds context and payloadPairs name-value pairs to the payload
* Applies the callback to the built payload
*
* @param sb object Payload
* @param array contexts Custom contexts relating to the event
* @param number tstamp Timestamp of the event
*/
function track(sb, context, tstamp) {
sb.addDict(payloadPairs);
sb.add('eid', uuid.v4());
sb.add('dtm', tstamp || new Date().getTime());
if (context) {
sb.addJson('cx', 'co', completeContexts(context));
}
var payload = sb.build();
if (typeof callback === 'function') {
callback(payload);
}
return payload;
}
/**
* Log an unstructured event
*
* @param object eventJson Contains the properties and schema location for the event
* @param array context Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
function trackUnstructEvent(properties, context, tstamp) {
var sb = payload.payloadBuilder(base64);
var ueJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
data: properties
};
sb.add('e', 'ue');
sb.addJson('ue_px', 'ue_pr', ueJson);
return track(sb, context, tstamp);
}
return {
/**
* Turn base 64 encoding on or off
*
* @param boolean encode key Field name
*/
setBase64Encoding: function (encode) {
base64 = encode;
},
addPayloadPair: addPayloadPair,
/**
* Merges a dictionary into payloadPairs
*
* @param object dict Dictionary to add
*/
addPayloadDict: function (dict) {
for (var key in dict) {
if (dict.hasOwnProperty(key)) {
payloadPairs[key] = dict[key];
}
}
},
/**
* Replace payloadPairs with a new dictionary
*
* @param object dict New dictionary
*/
resetPayloadPairs: function (dict) {
payloadPairs = payload.isJson(dict) ? dict : {};
},
/**
* Set the tracker version
*
* @param string version
*/
setTrackerVersion: function (version) {
addPayloadPair('tv', version)
},
/**
* Set the tracker namespace
*
* @param string name
*/
setTrackerNamespace: function (name) {
addPayloadPair('tna', name);
},
/**
* Set the application ID
*
* @param string appId
*/
setAppId: function (appId) {
addPayloadPair('aid', appId)
},
/**
* Set the platform
*
* @param string value
*/
setPlatform: function (value) {
addPayloadPair('p', value);
},
/**
* Set the user ID
*
* @param string userId
*/
setUserId: function (userId) {
addPayloadPair('uid', userId);
},
/**
* Set the screen resolution
*
* @param number width
* @param number height
*/
setScreenResolution: function (width, height) {
addPayloadPair('res', width + 'x' + height);
},
/**
* Set the viewport dimensions
*
* @param number width
* @param number height
*/
setViewport: function (width, height) {
addPayloadPair('vp', width + 'x' + height);
},
/**
* Set the color depth
*
* @param number depth
*/
setColorDepth: function (depth) {
addPayloadPair('cd', depth);
},
/**
* Set the timezone
*
* @param string timezone
*/
setTimezone: function (timezone) {
addPayloadPair('tz', timezone);
},
/**
* Set the language
*
* @param string lang
*/
setLang: function (lang) {
addPayloadPair('lang', lang);
},
/**
* Set the IP address
*
* @param string appId
*/
setIpAddress: function (ip) {
addPayloadPair('ip', ip)
},
trackUnstructEvent: trackUnstructEvent,
/**
* Log the page view / visit
*
* @param string customTitle The user-defined page title to attach to this page view
* @param array context Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackPageView: function (pageUrl, pageTitle, referrer, context, tstamp) {
var sb = payload.payloadBuilder(base64);
sb.add('e', 'pv'); // 'pv' for Page View
sb.add('url', pageUrl);
sb.add('page', pageTitle);
sb.add('refr', referrer);
return track(sb, context, tstamp);
},
/**
* Log that a user is still viewing a given page
* by sending a page ping.
*
* @param string pageTitle The page title to attach to this page ping
* @param array context Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackPagePing: function (pageUrl, pageTitle, referrer, context, tstamp) {
var sb = payload.payloadBuilder(base64);
sb.add('e', 'pp'); // 'pv' for Page View
sb.add('url', pageUrl);
sb.add('page', pageTitle);
sb.add('refr', referrer);
return track(sb, context, tstamp);
},
/**
* Track a structured event
*
* @param string category The name you supply for the group of objects you want to track
* @param string action A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object
* @param string label (optional) An optional string to provide additional dimensions to the event data
* @param string property (optional) Describes the object or the action performed on it, e.g. quantity of item added to basket
* @param int|float|string value (optional) An integer that you can use to provide numerical data about the user event
* @param array Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackStructEvent: function (category, action, label, property, value, context, tstamp) {
var sb = payload.payloadBuilder(base64);
sb.add('e', 'se'); // 'se' for Structured Event
sb.add('se_ca', category);
sb.add('se_ac', action);
sb.add('se_la', label);
sb.add('se_pr', property);
sb.add('se_va', value);
return track(sb, context, tstamp);
},
/**
* Track an ecommerce transaction
*
* @param string orderId Required. Internal unique order id number for this transaction.
* @param string affiliation Optional. Partner or store affiliation.
* @param string total Required. Total amount of the transaction.
* @param string tax Optional. Tax amount of the transaction.
* @param string shipping Optional. Shipping charge for the transaction.
* @param string city Optional. City to associate with transaction.
* @param string state Optional. State to associate with transaction.
* @param string country Optional. Country to associate with transaction.
* @param string currency Optional. Currency to associate with this transaction.
* @param array context Optional. Context relating to the event.
* @param number tstamp Optional. Timestamp of the event
* @return object Payload
*/
trackEcommerceTransaction: function (orderId, affiliation, totalValue, taxValue, shipping, city, state, country, currency, context, tstamp) {
var sb = payload.payloadBuilder(base64);
sb.add('e', 'tr'); // 'tr' for Transaction
sb.add("tr_id", orderId);
sb.add("tr_af", affiliation);
sb.add("tr_tt", totalValue);
sb.add("tr_tx", taxValue);
sb.add("tr_sh", shipping);
sb.add("tr_ci", city);
sb.add("tr_st", state);
sb.add("tr_co", country);
sb.add("tr_cu", currency);
return track(sb, context, tstamp);
},
/**
* Track an ecommerce transaction item
*
* @param string orderId Required Order ID of the transaction to associate with item.
* @param string sku Required. Item's SKU code.
* @param string name Optional. Product name.
* @param string category Optional. Product category.
* @param string price Required. Product price.
* @param string quantity Required. Purchase quantity.
* @param string currency Optional. Product price currency.
* @param array context Optional. Context relating to the event.
* @param number tstamp Optional. Timestamp of the event
* @return object Payload
*/
trackEcommerceTransactionItem: function (orderId, sku, name, category, price, quantity, currency, context, tstamp) {
var sb = payload.payloadBuilder(base64)
sb.add("e", "ti"); // 'tr' for Transaction Item
sb.add("ti_id", orderId);
sb.add("ti_sk", sku);
sb.add("ti_nm", name);
sb.add("ti_ca", category);
sb.add("ti_pr", price);
sb.add("ti_qu", quantity);
sb.add("ti_cu", currency);
return track(sb, context, tstamp);
},
/**
* Track a screen view unstructured event
*
* @param string name The name of the screen
* @param string id The ID of the screen
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackScreenView: function (name, id, context, tstamp) {
return trackUnstructEvent({
schema: 'iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0',
data: removeEmptyProperties({
name: name,
id: id
})
}, context, tstamp);
},
/**
* Log the link or click with the server
*
* @param string elementId
* @param array elementClasses
* @param string elementTarget
* @param string targetUrl
* @param array context Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, context, tstamp) {
var eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0',
data: removeEmptyProperties({
targetUrl: targetUrl,
elementId: elementId,
elementClasses: elementClasses,
elementTarget: elementTarget
}),
};
return trackUnstructEvent(eventJson, context, tstamp);
},
/**
* Track an ad being served
*
* @param string impressionId Identifier for a particular ad impression
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm'
* @param number cost Cost
* @param string bannerId Identifier for the ad banner displayed
* @param string zoneId Identifier for the ad zone
* @param string advertiserId Identifier for the advertiser
* @param string campaignId Identifier for the campaign which the banner belongs to
* @param array Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackAdImpression: function(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId, context, tstamp) {
var eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0',
data: removeEmptyProperties({
impressionId: impressionId,
costModel: costModel,
cost: cost,
targetUrl: targetUrl,
bannerId: bannerId,
zoneId: zoneId,
advertiserId: advertiserId,
campaignId: campaignId
})
};
return trackUnstructEvent(eventJson, context, tstamp);
},
/**
* Track an ad being clicked
*
* @param string clickId Identifier for the ad click
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm'
* @param number cost Cost
* @param string targetUrl (required) The link's target URL
* @param string bannerId Identifier for the ad banner displayed
* @param string zoneId Identifier for the ad zone
* @param string impressionId Identifier for a particular ad impression
* @param string advertiserId Identifier for the advertiser
* @param string campaignId Identifier for the campaign which the banner belongs to
* @param array Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackAdClick: function (targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId, context, tstamp) {
var eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_click/jsonschema/1-0-0',
data: removeEmptyProperties({
targetUrl: targetUrl,
clickId: clickId,
costModel: costModel,
cost: cost,
bannerId: bannerId,
zoneId: zoneId,
impressionId: impressionId,
advertiserId: advertiserId,
campaignId: campaignId
})
};
return trackUnstructEvent(eventJson, context, tstamp);
},
/**
* Track an ad conversion event
*
* @param string conversionId Identifier for the ad conversion event
* @param number cost Cost
* @param string category The name you supply for the group of objects you want to track
* @param string action A string that is uniquely paired with each category
* @param string property Describes the object of the conversion or the action performed on it
* @param number initialValue Revenue attributable to the conversion at time of conversion
* @param string advertiserId Identifier for the advertiser
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm'
* @param string campaignId Identifier for the campaign which the banner belongs to
* @param array Custom contexts relating to the event
* @param number tstamp Timestamp of the event
* @return object Payload
*/
trackAdConversion: function (conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId, context, tstamp) {
var eventJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_conversion/jsonschema/1-0-0',
data: removeEmptyProperties({
conversionId: conversionId,
costModel: costModel,
cost: cost,
category: category,
action: action,
property: property,
initialValue: initialValue,
advertiserId: advertiserId,
campaignId: campaignId
})
};
return trackUnstructEvent(eventJson, context, tstamp);
}
};
}
module.exports = trackerCore;