Skip to content

Commit c5f12d1

Browse files
committed
Added custom contexts to all events
Added test for trackEcommerceTransactionItem
1 parent 6d9907c commit c5f12d1

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

core/lib/core.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = function trackerCore(base64) {
6262
* Returns a copy of a JSON with undefined and null properties removed
6363
*
6464
* @param object eventJson JSON to clean
65-
* @return A cleaned copy of eventJson
65+
* @return object A cleaned copy of eventJson
6666
*/
6767
function removeEmptyProperties(eventJson) {
6868
var ret = {};
@@ -74,6 +74,12 @@ module.exports = function trackerCore(base64) {
7474
return ret;
7575
}
7676

77+
/**
78+
* Wraps an array of custom contexts in a self-describing JSON
79+
*
80+
* @param array contexts Array of custom context self-describing JSONs
81+
* @return object Outer JSON
82+
*/
7783
function completeContexts(contexts) {
7884
if (contexts && contexts.length) {
7985
return {
@@ -88,10 +94,10 @@ module.exports = function trackerCore(base64) {
8894
*
8995
* @param object eventJson Contains the properties and schema location for the event
9096
* @param object context Custom context relating to the event
97+
* @return object Payload
9198
*/
9299
function trackUnstructEvent(properties, context) {
93100
var sb = payload.payloadBuilder(base64);
94-
95101
var ueJson = {
96102
schema: 'iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0',
97103
data: properties
@@ -100,6 +106,7 @@ module.exports = function trackerCore(base64) {
100106
sb.add('e', 'ue');
101107
sb.addJson('ue_px', 'ue_pr', ueJson);
102108
sb.addDict(environment);
109+
sb.addJson('cx', 'co', completeContexts(context));
103110

104111
return sb.build();
105112
}
@@ -113,6 +120,7 @@ module.exports = function trackerCore(base64) {
113120
* @param string property (optional) Describes the object or the action performed on it, e.g. quantity of item added to basket
114121
* @param int|float|string value (optional) An integer that you can use to provide numerical data about the user event
115122
* @param object Custom context relating to the event
123+
* @return object Payload
116124
*/
117125
function trackStructEvent(category, action, label, property, value, context) {
118126
var sb = payload.payloadBuilder(base64);
@@ -122,8 +130,8 @@ module.exports = function trackerCore(base64) {
122130
sb.add('se_la', label);
123131
sb.add('se_pr', property);
124132
sb.add('se_va', value);
125-
126133
sb.addDict(environment);
134+
sb.addJson('cx', 'co', completeContexts(context));
127135

128136
return sb.build();
129137
}
@@ -133,6 +141,7 @@ module.exports = function trackerCore(base64) {
133141
*
134142
* @param string name The name of the screen
135143
* @param string id The ID of the screen
144+
* @return object Payload
136145
*/
137146
function trackScreenView(name, id, context) {
138147
return trackUnstructEvent({
@@ -149,6 +158,7 @@ module.exports = function trackerCore(base64) {
149158
*
150159
* @param string customTitle The user-defined page title to attach to this page view
151160
* @param object context Custom context relating to the event
161+
* @return object Payload
152162
*/
153163
function trackPageView(pageUrl, pageTitle, referrer, context) {
154164
var sb = payload.payloadBuilder(base64);
@@ -168,6 +178,7 @@ module.exports = function trackerCore(base64) {
168178
*
169179
* @param string pageTitle The page title to attach to this page ping
170180
* @param object context Custom context relating to the event
181+
* @return object Payload
171182
*/
172183
function trackPagePing(pageUrl, pageTitle, referrer, context) {
173184
var sb = payload.payloadBuilder(base64);
@@ -176,6 +187,7 @@ module.exports = function trackerCore(base64) {
176187
sb.add('page', pageTitle);
177188
sb.add('refr', referrer);
178189
sb.addDict(environment);
190+
sb.addJson('cx', 'co', completeContexts(context));
179191

180192
return sb.build();
181193
}
@@ -193,8 +205,9 @@ module.exports = function trackerCore(base64) {
193205
* @param string country Optional. Country to associate with transaction.
194206
* @param string currency Optional. Currency to associate with this transaction.
195207
* @param object context Optional. Context relating to the event.
208+
* @return object Payload
196209
*/
197-
function trackEcommerceTransaction(orderId, totalValue, affiliation, taxValue, shipping, city, state, country, currency, items, context) {
210+
function trackEcommerceTransaction(orderId, totalValue, affiliation, taxValue, shipping, city, state, country, currency, context) {
198211
var sb = payload.payloadBuilder(base64);
199212
sb.add('e', 'tr'); // 'tr' for Transaction
200213
sb.add("tr_id", orderId);
@@ -207,6 +220,7 @@ module.exports = function trackerCore(base64) {
207220
sb.add("tr_co", country);
208221
sb.add("tr_cu", currency);
209222
sb.addDict(environment);
223+
sb.addJson('cx', 'co', completeContexts(context));
210224

211225
return sb.build();
212226
}
@@ -222,17 +236,20 @@ module.exports = function trackerCore(base64) {
222236
* @param string quantity Required. Purchase quantity.
223237
* @param string currency Optional. Product price currency.
224238
* @param object context Optional. Context relating to the event.
239+
* @return object Payload
225240
*/
226241
function trackEcommerceTransactionItem(orderId, sku, price, quantity, name, category, currency, context) {
227242
var sb = payload.payloadBuilder(base64)
228243
sb.add("e", "ti"); // 'tr' for Transaction Item
229244
sb.add("ti_id", orderId);
230245
sb.add("ti_sk", sku);
231-
sb.add("ti_nm", name);
232-
sb.add("ti_ca", category);
233246
sb.add("ti_pr", price);
234247
sb.add("ti_qu", quantity);
248+
sb.add("ti_nm", name);
249+
sb.add("ti_ca", category);
235250
sb.add("ti_cu", currency);
251+
sb.addDict(environment);
252+
sb.addJson('cx', 'co', completeContexts(context));
236253

237254
return sb.build();
238255
}
@@ -246,6 +263,7 @@ module.exports = function trackerCore(base64) {
246263
* @param string elementTarget
247264
* @param string targetUrl
248265
* @param object context Custom context relating to the event
266+
* @return object Payload
249267
*/
250268
function trackLinkClick(targetUrl, elementId, elementClasses, elementTarget, context) {
251269
var eventJson = {
@@ -272,6 +290,7 @@ module.exports = function trackerCore(base64) {
272290
* @param string advertiserId Identifier for the advertiser
273291
* @param string campaignId Identifier for the campaign which the banner belongs to
274292
* @param object Custom context relating to the event
293+
* @return object Payload
275294
*/
276295
function trackAdImpression(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId, context) {
277296
var eventJson = {
@@ -304,6 +323,7 @@ module.exports = function trackerCore(base64) {
304323
* @param string advertiserId Identifier for the advertiser
305324
* @param string campaignId Identifier for the campaign which the banner belongs to
306325
* @param object Custom context relating to the event
326+
* @return object Payload
307327
*/
308328
function trackAdClick(targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId, context) {
309329
var eventJson = {
@@ -337,6 +357,7 @@ module.exports = function trackerCore(base64) {
337357
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm'
338358
* @param string campaignId Identifier for the campaign which the banner belongs to
339359
* @param object Custom context relating to the event
360+
* @return object Payload
340361
*/
341362
function trackAdConversion(conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId, context) {
342363
var eventJson = {

core/tests/core.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ define([
104104
assert.deepEqual(tracker.trackEcommerceTransaction(orderId, totalValue, null, taxValue, shipping, city, state, country, currency), expected, 'A transaction event should be tracked correctly');
105105
},
106106

107+
"Track an ecommerce transaction item event": function() {
108+
var orderId = 'ak0008';
109+
var sku = '4q345';
110+
var price = 17;
111+
var quantity = 2;
112+
var name = 'red shoes';
113+
var category = 'clothing';
114+
var currency = 'USD';
115+
var expected = {
116+
e: 'ti',
117+
ti_id: orderId,
118+
ti_sk: sku,
119+
ti_pr: price,
120+
ti_qu: quantity,
121+
ti_nm: name,
122+
ti_ca: category,
123+
ti_cu: currency
124+
};
125+
126+
assert.deepEqual(tracker.trackEcommerceTransactionItem(orderId, sku, price, quantity, name, category, currency), expected, 'A transaction item event should be tracked correctly');
127+
},
128+
107129
"Track an unstructured event": function() {
108130
var inputJson = {
109131
schema: 'iglu:com.acme/user/jsonschema/1-0-1',

0 commit comments

Comments
 (0)