Skip to content

Commit f99ca3e

Browse files
committed
Added more functionality and tests
1 parent e63aa52 commit f99ca3e

6 files changed

Lines changed: 190 additions & 51 deletions

File tree

core/Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = function(grunt) {
4545
suites: [
4646
'tests/base64.js',
4747
'tests/payload.js',
48-
'tests/integration.js'
48+
'tests/core.js'
4949
]
5050
}
5151
}

core/lib/core.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@
3434

3535
var payload = require('./payload.js');
3636

37-
function trackerCore(base64) {
37+
module.exports = function trackerCore(base64) {
3838

3939
// Dictionary of key-value pairs which get added to every payload, e.g. tracker version
40-
var state = {};
40+
var environment = {};
4141

4242
/**
4343
* Set a persistent key-value pair to be added to every payload
4444
*
4545
* @param string key Field name
4646
* @param string value Field value
4747
*/
48-
function setState(key, value) {
49-
state[key] = value;
48+
function setEnvironment(key, value) {
49+
environment[key] = value;
5050
}
5151

5252
/**
53-
* Replace state with a new dictionary
53+
* Replace environment with a new dictionary
5454
*
55-
* @param object newState New dictionary
55+
* @param object newenvironment New dictionary
5656
*/
57-
function resetState(newState) {
58-
state = payload.isJson(newState) ? newState : {};
57+
function resetEnvironment(newenvironment) {
58+
environment = payload.isJson(newenvironment) ? newenvironment : {};
5959
}
6060

6161
function completeContexts(contexts) {
@@ -81,10 +81,11 @@ function trackerCore(base64) {
8181
data: properties
8282
};
8383

84+
sb.add('e', 'ue');
8485
sb.addJson('ue_px', 'ue_pr', ueJson);
85-
sb.addDict(state);
86+
sb.addDict(environment);
8687

87-
return sb;
88+
return sb.build();
8889
}
8990

9091
/**
@@ -101,14 +102,14 @@ function trackerCore(base64) {
101102
var sb = payload.payloadBuilder(base64);
102103
sb.add('e', 'se'); // 'se' for Structured Event
103104
sb.add('se_ca', category);
104-
sb.add('se_ac', action)
105+
sb.add('se_ac', action);
105106
sb.add('se_la', label);
106107
sb.add('se_pr', property);
107108
sb.add('se_va', value);
108109

109-
sb.addDict(state);
110+
sb.addDict(environment);
110111

111-
return sb;
112+
return sb.build();
112113
}
113114

114115
/**
@@ -144,10 +145,10 @@ function trackerCore(base64) {
144145
sb.add('url', pageUrl);
145146
sb.add('page', pageTitle);
146147
sb.add('refr', referrer);
147-
sb.addDict(state);
148+
sb.addDict(environment);
148149
sb.addJson('cx', 'co', completeContexts(context));
149150

150-
return sb;
151+
return sb.build();
151152
}
152153

153154
/**
@@ -163,9 +164,9 @@ function trackerCore(base64) {
163164
sb.add('url', pageUrl);
164165
sb.add('page', pageTitle);
165166
sb.add('refr', referrer);
166-
sb.addDict(state);
167+
sb.addDict(environment);
167168

168-
return sb;
169+
return sb.build();
169170
}
170171

171172
/**
@@ -185,18 +186,18 @@ function trackerCore(base64) {
185186
function trackEcommerceTransaction(orderId, totalValue, affiliation, taxValue, shipping, city, state, country, currency, items, context) {
186187
var sb = payload.payloadBuilder(base64);
187188
sb.add('e', 'tr'); // 'tr' for Transaction
188-
sb.add("tr_id", orderId)
189-
sb.add("tr_tt", totalValue)
190-
sb.add("tr_af", affiliation)
191-
sb.add("tr_tx", taxValue)
192-
sb.add("tr_sh", shipping)
193-
sb.add("tr_ci", city)
194-
sb.add("tr_st", state)
195-
sb.add("tr_co", country)
196-
sb.add("tr_cu", currency)
197-
sb.addDict(state);
198-
199-
return sb;
189+
sb.add("tr_id", orderId);
190+
sb.add("tr_tt", totalValue);
191+
sb.add("tr_af", affiliation);
192+
sb.add("tr_tx", taxValue);
193+
sb.add("tr_sh", shipping);
194+
sb.add("tr_ci", city);
195+
sb.add("tr_st", state);
196+
sb.add("tr_co", country);
197+
sb.add("tr_cu", currency);
198+
sb.addDict(environment);
199+
200+
return sb.build();
200201
}
201202

202203
/**
@@ -213,16 +214,16 @@ function trackerCore(base64) {
213214
*/
214215
function trackEcommerceTransactionItem(orderId, sku, price, quantity, name, category, currency, context) {
215216
var sb = payload.payloadBuilder(base64)
216-
sb.add("e", "ti") // 'tr' for Transaction Item
217-
sb.add("ti_id", orderId)
218-
sb.add("ti_sk", sku)
219-
sb.add("ti_nm", name)
220-
sb.add("ti_ca", category)
221-
sb.add("ti_pr", price)
222-
sb.add("ti_qu", quantity)
223-
sb.add("ti_cu", currency)
224-
225-
return sb;
217+
sb.add("e", "ti"); // 'tr' for Transaction Item
218+
sb.add("ti_id", orderId);
219+
sb.add("ti_sk", sku);
220+
sb.add("ti_nm", name);
221+
sb.add("ti_ca", category);
222+
sb.add("ti_pr", price);
223+
sb.add("ti_qu", quantity);
224+
sb.add("ti_cu", currency);
225+
226+
return sb.build();
226227
}
227228

228229

@@ -309,7 +310,7 @@ function trackerCore(base64) {
309310
}
310311
};
311312

312-
trackUnstructEvent(eventJson, context);
313+
return trackUnstructEvent(eventJson, context);
313314
}
314315

315316
/**
@@ -342,12 +343,12 @@ function trackerCore(base64) {
342343
}
343344
};
344345

345-
trackUnstructEvent(eventJson, context);
346+
return trackUnstructEvent(eventJson, context);
346347
}
347348

348349
return {
349-
setState: setState,
350-
resetState: resetState,
350+
setEnvironment: setEnvironment,
351+
resetEnvironment: resetEnvironment,
351352
trackUnstructEvent: trackUnstructEvent,
352353
trackStructEvent: trackStructEvent,
353354
trackPageView: trackPageView,

core/tests/base64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ define([
3939
], function(registerSuite, assert, base64) {
4040

4141
registerSuite({
42-
name: "base64 encoding test",
42+
name: "Base 64 encoding test",
4343
"Encode a string": function() {
4444
assert.strictEqual(base64.base64encode('my_string'), 'bXlfc3RyaW5n', 'Base64-encode a string');
4545
},

core/tests/core.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* JavaScript tracker core for Snowplow: tests/integration.js
3+
*
4+
* Significant portions copyright 2010 Anthon Pang. Remainder copyright
5+
* 2012-2014 Snowplow Analytics Ltd. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are
9+
* met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* * Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in the
16+
* documentation and/or other materials provided with the distribution.
17+
*
18+
* * Neither the name of Anthon Pang nor Snowplow Analytics Ltd nor the
19+
* names of their contributors may be used to endorse or promote products
20+
* derived from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
define([
36+
"intern!object",
37+
"intern/chai!assert",
38+
"intern/dojo/node!../lib/core.js",
39+
"intern/dojo/node!JSON"
40+
], function(registerSuite, assert, core, JSON) {
41+
42+
var tracker = core(false);
43+
44+
registerSuite({
45+
name: "Tracking events",
46+
"Track a page view": function() {
47+
var url = 'http://www.example.com';
48+
var page = 'title page';
49+
var expected = {
50+
e: 'pv',
51+
url: url,
52+
page: page
53+
};
54+
assert.deepEqual(tracker.trackPageView(url, page), expected, 'A page view should be tracked correctly');
55+
},
56+
57+
"Track a page ping": function() {
58+
var url = 'http://www.example.com';
59+
var referer = 'http://www.google.com';
60+
var expected = {
61+
e: 'pp',
62+
url: url,
63+
refr: referer
64+
};
65+
66+
assert.deepEqual(tracker.trackPagePing(url, null, referer), expected, 'A page ping should be tracked correctly');
67+
},
68+
69+
"Track a structured event": function() {
70+
var expected = {
71+
e: 'se',
72+
se_ca: 'cat',
73+
se_ac: 'act',
74+
se_la: 'lab',
75+
se_pr: 'prop',
76+
se_va: 'val'
77+
};
78+
79+
assert.deepEqual(tracker.trackStructEvent('cat', 'act', 'lab', 'prop', 'val'), expected, 'A structured event should be tracked correctly');
80+
},
81+
82+
"Track an ecommerce transaction event": function() {
83+
var orderId = 'ak0008';
84+
var totalValue = 50;
85+
var taxValue = 6;
86+
var shipping = 0;
87+
var city = 'Phoenix';
88+
var state = 'Arizona';
89+
var country = 'USA';
90+
var currency = 'USD';
91+
var expected = {
92+
e: 'tr',
93+
tr_tt: totalValue,
94+
tr_tx: taxValue,
95+
tr_id: orderId,
96+
tr_sh: shipping,
97+
tr_ci: city,
98+
tr_st: state,
99+
tr_co: country,
100+
tr_cu: currency
101+
};
102+
103+
assert.deepEqual(tracker.trackEcommerceTransaction(orderId, totalValue, null, taxValue, shipping, city, state, country, currency), expected, 'A transaction event should be tracked correctly');
104+
},
105+
106+
"Track an unstructured event": function() {
107+
var inputJson = {
108+
schema: 'iglu:com.acme/user/jsonschema/1-0-1',
109+
data: {
110+
name: 'Eric'
111+
}
112+
}
113+
var expected = {
114+
e: 'ue',
115+
ue_pr: JSON.stringify({
116+
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
117+
data: inputJson
118+
})
119+
};
120+
121+
assert.deepEqual(tracker.trackUnstructEvent(inputJson), expected, 'An unstructured event should be tracked correctly');
122+
},
123+
});
124+
125+
});

core/tests/intern.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript tracker for Snowplow: tests/intern.js
2+
* JavaScript tracker core for Snowplow: tests/intern.js
33
*
44
* Significant portions copyright 2010 Anthon Pang. Remainder copyright
55
* 2012-2014 Snowplow Analytics Ltd. All rights reserved.
@@ -66,7 +66,7 @@ define({
6666
loader: {},
6767

6868
// Functional test suite(s) to run in each browser once non-functional tests are completed
69-
functionalSuites: ['tests/functional/helpers','tests/functional/detectors'],
69+
functionalSuites: [],
7070

7171
// A regular expression matching URLs to files that should not be included in code coverage analysis
7272
excludeInstrumentation: /^tests\//

core/tests/payload.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,29 @@ define([
8484
'Identify an empty JSON': function() {
8585
var emptyJson = {};
8686

87-
assert.strictEqual(payload.isNonEmptyJson(emptyJson), false, 'Identify {} as empty')
87+
assert.strictEqual(payload.isNonEmptyJson(emptyJson), false, '{} should be identified as empty')
8888
},
8989

9090
'Build a payload': function () {
91-
9291
var sb = payload.payloadBuilder(false);
9392
sb.add('e', 'pv');
9493
sb.add('tv', 'js-2.0.0');
9594

96-
assert.deepEqual(sb.build(), {e: 'pv', tv: 'js-2.0.0'});
95+
assert.deepEqual(sb.build(), {e: 'pv', tv: 'js-2.0.0'}, 'Individual name-value pairs should be added to the payload');
96+
},
97+
98+
'Do not add undefined values to a payload': function () {
99+
var sb = payload.payloadBuilder(false);
100+
sb.add('e', undefined);
101+
102+
assert.deepEqual(sb.build(), {}, 'Undefined values should not be added to the payload');
103+
},
104+
105+
'Do not add null values to a payload': function () {
106+
var sb = payload.payloadBuilder(false);
107+
sb.add('e', null);
108+
109+
assert.deepEqual(sb.build(), {}, 'Null values should not be added to the payload');
97110
},
98111

99112
'Add a dictionary of name-value pairs to the payload': function () {
@@ -103,7 +116,7 @@ define([
103116
'tv': 'js-2.0.0'
104117
})
105118

106-
assert.deepEqual(sb.build(), {e: 'pv', tv: 'js-2.0.0'});
119+
assert.deepEqual(sb.build(), {e: 'pv', tv: 'js-2.0.0'}, 'A dictionary of name-value pairs should be added to the payload');
107120
},
108121

109122
'Add a JSON to the payload': function() {

0 commit comments

Comments
 (0)