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
414 lines (370 loc) · 11.1 KB
/
Copy pathcore.js
File metadata and controls
414 lines (370 loc) · 11.1 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
/*
* JavaScript tracker core for Snowplow: tests/integration.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.
*/
define([
"intern!object",
"intern/chai!assert",
"intern/dojo/node!../lib/core.js",
"intern/dojo/node!JSON"
], function (registerSuite, assert, core, JSON) {
var unstructEventSchema = 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0';
var tracker = core(false);
function compare(result, expected, message) {
assert.ok(result['eid'], 'A UUID should be attached to all events');
delete result['eid'];
assert.ok(result['dtm'], 'A timestamp should be attached to all events');
delete result['dtm'];
assert.deepEqual(result, expected, message);
}
registerSuite({
name: "Tracking events",
"Track a page view": function () {
var url = 'http://www.example.com';
var page = 'title page';
var expected = {
e: 'pv',
url: url,
page: page
};
compare(tracker.trackPageView(url, page), expected, 'A page view should be tracked correctly');
},
"Track a page ping": function () {
var url = 'http://www.example.com';
var referer = 'http://www.google.com';
var expected = {
e: 'pp',
url: url,
refr: referer
};
compare(tracker.trackPagePing(url, null, referer), expected, 'A page ping should be tracked correctly');
},
"Track a structured event": function () {
var expected = {
e: 'se',
se_ca: 'cat',
se_ac: 'act',
se_la: 'lab',
se_pr: 'prop',
se_va: 'val'
};
compare(tracker.trackStructEvent('cat', 'act', 'lab', 'prop', 'val'), expected, 'A structured event should be tracked correctly');
},
"Track an ecommerce transaction event": function () {
var orderId = 'ak0008';
var totalValue = 50;
var taxValue = 6;
var shipping = 0;
var city = 'Phoenix';
var state = 'Arizona';
var country = 'USA';
var currency = 'USD';
var expected = {
e: 'tr',
tr_id: orderId,
tr_tt: totalValue,
tr_tx: taxValue,
tr_sh: shipping,
tr_ci: city,
tr_st: state,
tr_co: country,
tr_cu: currency
};
compare(tracker.trackEcommerceTransaction(orderId, null, totalValue, taxValue, shipping, city, state, country, currency), expected, 'A transaction event should be tracked correctly');
},
"Track an ecommerce transaction item event": function () {
var orderId = 'ak0008';
var sku = '4q345';
var price = 17;
var quantity = 2;
var name = 'red shoes';
var category = 'clothing';
var currency = 'USD';
var expected = {
e: 'ti',
ti_id: orderId,
ti_sk: sku,
ti_nm: name,
ti_ca: category,
ti_pr: price,
ti_qu: quantity,
ti_cu: currency
};
compare(tracker.trackEcommerceTransactionItem(orderId, sku, name, category, price, quantity, currency), expected, 'A transaction item event should be tracked correctly');
},
"Track an unstructured event": function () {
var inputJson = {
schema: 'iglu:com.acme/user/jsonschema/1-0-1',
data: {
name: 'Eric'
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackUnstructEvent(inputJson), expected, 'An unstructured event should be tracked correctly');
},
"Track a link click": function () {
var targetUrl = 'http://www.example.com';
var elementId = 'first header';
var elementClasses = ['header'];
var inputJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0',
data: {
targetUrl: targetUrl,
elementId: elementId,
elementClasses: elementClasses
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackLinkClick(targetUrl, elementId, elementClasses), expected, 'A link click should be tracked correctly');
},
"Track a screen view": function () {
var name = 'intro';
var id = '7398-4352-5345-1950';
var inputJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0',
data: {
name: name,
id: id
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackScreenView(name, id), expected, 'A screen view should be tracked correctly');
},
"Track an ad impression": function () {
var impressionId = 'a0e8f8780ab3';
var costModel = 'cpc';
var cost = 0.5;
var targetUrl = 'http://adsite.com';
var bannerId = '123';
var zoneId = 'zone-14';
var advertiserId = 'ad-company';
var campaignId = 'campaign-7592';
var inputJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0',
data: {
impressionId: impressionId,
costModel: costModel,
cost: cost,
targetUrl: targetUrl,
bannerId: bannerId,
zoneId: zoneId,
advertiserId: advertiserId,
campaignId: campaignId
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackAdImpression(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId), expected, 'An ad impression should be tracked correctly');
},
"Track an ad click": function () {
var targetUrl = 'http://adsite.com';
var clickId = 'click-321';
var costModel = 'cpc';
var cost = 0.5;
var bannerId = '123';
var zoneId = 'zone-14';
var impressionId = 'a0e8f8780ab3';
var advertiserId = 'ad-company';
var campaignId = 'campaign-7592';
var inputJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_click/jsonschema/1-0-0',
data: {
targetUrl: targetUrl,
clickId: clickId,
costModel: costModel,
cost: cost,
bannerId: bannerId,
zoneId: zoneId,
impressionId: impressionId,
advertiserId: advertiserId,
campaignId: campaignId
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackAdClick(targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId), expected, 'An ad click should be tracked correctly');
},
"Track an ad conversion": function () {
var conversionId = 'conversion-59';
var costModel = 'cpc';
var cost = 0.5;
var category = 'cat';
var action = 'act';
var property = 'prop';
var initialValue = 7;
var advertiserId = 'ad-company';
var campaignId = 'campaign-7592';
var inputJson = {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_conversion/jsonschema/1-0-0',
data: {
conversionId: conversionId,
costModel: costModel,
cost: cost,
category: category,
action: action,
property: property,
initialValue: initialValue,
advertiserId: advertiserId,
campaignId: campaignId
}
};
var expected = {
e: 'ue',
ue_pr: JSON.stringify({
schema: unstructEventSchema,
data: inputJson
})
};
compare(tracker.trackAdConversion(conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId), expected, 'An ad conversion should be tracked correctly');
},
"Track a page view with custom context": function () {
var url = 'http://www.example.com';
var page = 'title page';
var inputContext = [{
schema: 'iglu:com.acme/user/jsonschema/1-0-0',
data: {
userType: 'tester',
userName: 'Jon'
}
}];
var expected = {
e: 'pv',
url: url,
page: page,
co: JSON.stringify({
schema: 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0',
data: inputContext
})
};
compare(tracker.trackPageView(url, page, null, inputContext), expected, 'A custom context should be attached correctly');
},
"Track a page view with a timestamp": function () {
var tstamp = 1000000000000;
assert.strictEqual(tracker.trackPageView('http://www.example.com', null, null, null, tstamp)['dtm'], tstamp, 'A timestamp should be attached correctly');
},
"Add individual name-value pairs to the payload": function () {
var tracker = core(false);
var url = 'http://www.example.com';
var expected = {
e: 'pv',
url: url,
tna: 'cf',
tv: 'js-2.0.0'
};
tracker.addPayloadPair('tna', 'cf');
tracker.addPayloadPair('tv', 'js-2.0.0');
compare(tracker.trackPageView(url), expected, 'Payload name-value pairs should be set correctly');
},
"Add a dictionary of name-value pairs to the payload": function () {
var tracker = core(false);
var url = 'http://www.example.com';
var expected = {
e: 'pv',
url: url,
tv: 'js-2.0.0',
tna: 'cf',
aid: 'cf325'
};
tracker.addPayloadPair('tv', 'js-2.0.0');
tracker.addPayloadDict({
tna: 'cf',
aid: 'cf325'
});
compare(tracker.trackPageView(url), expected, 'Payload name-value pairs should be set correctly');
},
"Reset payload name-value pairs": function () {
var tracker = core(false);
var url = 'http://www.example.com';
var expected = {
e: 'pv',
url: url,
tna: 'cf'
};
tracker.addPayloadPair('tna', 'mistake');
tracker.resetPayloadPairs({'tna': 'cf'});
compare(tracker.trackPageView(url), expected, 'Payload name-value pairs should be reset correctly');
},
"Execute a callback": function () {
var callbackTarget;
var tracker = core(false, function (payload) {
callbackTarget = payload;
});
var url = 'http://www.example.com';
var expected = {
e: 'pv',
url: url,
};
tracker.trackPageView(url);
compare(callbackTarget, expected, 'The callback should be executed correctly');
},
"Use setter methods": function () {
var tracker = core(false);
tracker.setTrackerVersion('js-3.0.0');
tracker.setTrackerNamespace('cf1');
tracker.setAppId('my-app');
tracker.setPlatform('web');
tracker.setUserId('jacob');
tracker.setScreenResolution(400, 200);
tracker.setViewport(500, 800);
tracker.setColorDepth(24);
tracker.setTimezone('Europe London');
tracker.setIpAddress('37.151.33.154');
var url = 'http://www.example.com';
var page = 'title page';
var expected = {
e: 'pv',
url: url,
page: page,
tna: 'cf1',
tv: 'js-3.0.0',
aid: 'my-app',
p: 'web',
uid: 'jacob',
res: '400x200',
vp: '500x800',
cd: 24,
tz: 'Europe London',
ip: '37.151.33.154'
};
compare(tracker.trackPageView(url, page), expected, 'setXXX methods should work correctly');
},
});
});