Skip to content

Commit bb88faf

Browse files
committed
Merge branch 'core/0.3.0'
2 parents edb3295 + cb89e04 commit bb88faf

File tree

6 files changed

+388
-32
lines changed

6 files changed

+388
-32
lines changed

core/CHANGELOG

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Version 0.3.0 (2014-11-04)
2+
--------------------------
3+
Applied callback to the Payload for an event rather than the event dictionary (#259)
4+
Added page scroll parameters to trackPagePing (#257)
5+
Added social tracking (#258)
6+
Added trackAddToCart and trackRemoveFromCart methods (#260)
7+
Added trackFormChange and trackFormSubmission methods (#261)
8+
Added trackSiteSearch method (#263)
9+
Added content field to trackLinkClick (#262)
10+
111
Version 0.2.0 (2014-08-07)
212
--------------------------
313
Added UUID to payload (#244)

core/lib/core.js

+176-16
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ function trackerCore(base64, callback) {
4747
* Returns a copy of a JSON with undefined and null properties removed
4848
*
4949
* @param object eventJson JSON to clean
50+
* @param object exemptFields Set of fields which should not be removed even if empty
5051
* @return object A cleaned copy of eventJson
5152
*/
52-
function removeEmptyProperties(eventJson) {
53+
function removeEmptyProperties(eventJson, exemptFields) {
5354
var ret = {};
55+
exemptFields = exemptFields || {};
5456
for (var k in eventJson) {
55-
if (eventJson[k] !== null && typeof eventJson[k] !== 'undefined') {
57+
if (exemptFields[k] || (eventJson[k] !== null && typeof eventJson[k] !== 'undefined')) {
5658
ret[k] = eventJson[k];
5759
}
5860
}
@@ -82,6 +84,7 @@ function trackerCore(base64, callback) {
8284
* @param sb object Payload
8385
* @param array contexts Custom contexts relating to the event
8486
* @param number tstamp Timestamp of the event
87+
* @return object Payload after the callback is applied
8588
*/
8689
function track(sb, context, tstamp) {
8790
sb.addDict(payloadPairs);
@@ -91,12 +94,11 @@ function trackerCore(base64, callback) {
9194
sb.addJson('cx', 'co', completeContexts(context));
9295
}
9396

94-
var payload = sb.build();
9597
if (typeof callback === 'function') {
96-
callback(payload);
98+
callback(sb);
9799
}
98100

99-
return payload;
101+
return sb;
100102
}
101103

102104
/**
@@ -161,7 +163,7 @@ function trackerCore(base64, callback) {
161163
* @param string version
162164
*/
163165
setTrackerVersion: function (version) {
164-
addPayloadPair('tv', version)
166+
addPayloadPair('tv', version);
165167
},
166168

167169
/**
@@ -179,7 +181,7 @@ function trackerCore(base64, callback) {
179181
* @param string appId
180182
*/
181183
setAppId: function (appId) {
182-
addPayloadPair('aid', appId)
184+
addPayloadPair('aid', appId);
183185
},
184186

185187
/**
@@ -253,7 +255,7 @@ function trackerCore(base64, callback) {
253255
* @param string appId
254256
*/
255257
setIpAddress: function (ip) {
256-
addPayloadPair('ip', ip)
258+
addPayloadPair('ip', ip);
257259
},
258260

259261
trackUnstructEvent: trackUnstructEvent,
@@ -280,16 +282,24 @@ function trackerCore(base64, callback) {
280282
* by sending a page ping.
281283
*
282284
* @param string pageTitle The page title to attach to this page ping
285+
* @param minxoffset Minimum page x offset seen in the last ping period
286+
* @param maxXOffset Maximum page x offset seen in the last ping period
287+
* @param minYOffset Minimum page y offset seen in the last ping period
288+
* @param maxYOffset Maximum page y offset seen in the last ping period
283289
* @param array context Custom contexts relating to the event
284290
* @param number tstamp Timestamp of the event
285291
* @return object Payload
286292
*/
287-
trackPagePing: function (pageUrl, pageTitle, referrer, context, tstamp) {
293+
trackPagePing: function (pageUrl, pageTitle, referrer, minXOffset, maxXOffset, minYOffset, maxYOffset, context, tstamp) {
288294
var sb = payload.payloadBuilder(base64);
289-
sb.add('e', 'pp'); // 'pv' for Page View
295+
sb.add('e', 'pp'); // 'pp' for Page Ping
290296
sb.add('url', pageUrl);
291297
sb.add('page', pageTitle);
292298
sb.add('refr', referrer);
299+
sb.add('pp_mix', minXOffset);
300+
sb.add('pp_max', maxXOffset);
301+
sb.add('pp_miy', minYOffset);
302+
sb.add('pp_may', maxYOffset);
293303

294304
return track(sb, context, tstamp);
295305
},
@@ -364,7 +374,7 @@ function trackerCore(base64, callback) {
364374
* @return object Payload
365375
*/
366376
trackEcommerceTransactionItem: function (orderId, sku, name, category, price, quantity, currency, context, tstamp) {
367-
var sb = payload.payloadBuilder(base64)
377+
var sb = payload.payloadBuilder(base64);
368378
sb.add("e", "ti"); // 'tr' for Transaction Item
369379
sb.add("ti_id", orderId);
370380
sb.add("ti_sk", sku);
@@ -398,22 +408,24 @@ function trackerCore(base64, callback) {
398408
/**
399409
* Log the link or click with the server
400410
*
411+
* @param string targetUrl
401412
* @param string elementId
402413
* @param array elementClasses
403414
* @param string elementTarget
404-
* @param string targetUrl
415+
* @param string elementContent innerHTML of the link
405416
* @param array context Custom contexts relating to the event
406417
* @param number tstamp Timestamp of the event
407418
* @return object Payload
408419
*/
409-
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, context, tstamp) {
420+
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, elementContent, context, tstamp) {
410421
var eventJson = {
411-
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0',
422+
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1',
412423
data: removeEmptyProperties({
413424
targetUrl: targetUrl,
414425
elementId: elementId,
415426
elementClasses: elementClasses,
416-
elementTarget: elementTarget
427+
elementTarget: elementTarget,
428+
elementContent: elementContent
417429
}),
418430
};
419431

@@ -520,8 +532,156 @@ function trackerCore(base64, callback) {
520532
};
521533

522534
return trackUnstructEvent(eventJson, context, tstamp);
535+
},
536+
537+
/**
538+
* Track a social event
539+
*
540+
* @param string action Social action performed
541+
* @param string network Social network
542+
* @param string target Object of the social action e.g. the video liked, the tweet retweeted
543+
* @param array Custom contexts relating to the event
544+
* @param number tstamp Timestamp of the event
545+
* @return object Payload
546+
*/
547+
trackSocialInteraction: function (action, network, target, context, tstamp) {
548+
var eventJson = {
549+
schema: 'iglu:com.snowplowanalytics.snowplow/social_interaction/jsonschema/1-0-0',
550+
data: removeEmptyProperties({
551+
action: action,
552+
network: network,
553+
target: target
554+
})
555+
};
556+
557+
return trackUnstructEvent(eventJson, context, tstamp);
558+
},
559+
560+
/**
561+
* Track an add-to-cart event
562+
*
563+
* @param string sku Required. Item's SKU code.
564+
* @param string name Optional. Product name.
565+
* @param string category Optional. Product category.
566+
* @param string unitPrice Optional. Product price.
567+
* @param string quantity Required. Quantity added.
568+
* @param string currency Optional. Product price currency.
569+
* @param array context Optional. Context relating to the event.
570+
* @param number tstamp Optional. Timestamp of the event
571+
* @return object Payload
572+
*/
573+
trackAddToCart: function (sku, name, category, unitPrice, quantity, currency, context, tstamp) {
574+
return trackUnstructEvent({
575+
schema: 'iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0',
576+
data: removeEmptyProperties({
577+
sku: sku,
578+
name: name,
579+
category: category,
580+
unitPrice: unitPrice,
581+
quantity: quantity,
582+
currency: currency
583+
})
584+
}, context, tstamp);
585+
},
586+
587+
/**
588+
* Track a remove-from-cart event
589+
*
590+
* @param string sku Required. Item's SKU code.
591+
* @param string name Optional. Product name.
592+
* @param string category Optional. Product category.
593+
* @param string unitPrice Optional. Product price.
594+
* @param string quantity Required. Quantity removed.
595+
* @param string currency Optional. Product price currency.
596+
* @param array context Optional. Context relating to the event.
597+
* @param number tstamp Optional. Timestamp of the event
598+
* @return object Payload
599+
*/
600+
trackRemoveFromCart: function (sku, name, category, unitPrice, quantity, currency, context, tstamp) {
601+
return trackUnstructEvent({
602+
schema: 'iglu:com.snowplowanalytics.snowplow/remove_from_cart/jsonschema/1-0-0',
603+
data: removeEmptyProperties({
604+
sku: sku,
605+
name: name,
606+
category: category,
607+
unitPrice: unitPrice,
608+
quantity: quantity,
609+
currency: currency
610+
})
611+
}, context, tstamp);
612+
},
613+
614+
/**
615+
* Track the value of a form field changing
616+
*
617+
* @param string formId The parent form ID
618+
* @param string elementId ID of the changed element
619+
* @param string nodeName "INPUT", "TEXTAREA", or "SELECT"
620+
* @param string type Type of the changed element if its type is "INPUT"
621+
* @param array elementClasses List of classes of the changed element
622+
* @param string value The new value of the changed element
623+
* @param array context Optional. Context relating to the event.
624+
* @param number tstamp Optional. Timestamp of the event
625+
* @return object Payload
626+
*/
627+
trackFormChange: function(formId, elementId, nodeName, type, elementClasses, value, context, tstamp) {
628+
return trackUnstructEvent({
629+
schema: 'iglu:com.snowplowanalytics.snowplow/change_form/jsonschema/1-0-0',
630+
data: removeEmptyProperties({
631+
formId: formId,
632+
elementId: elementId,
633+
nodeName: nodeName,
634+
type: type,
635+
elementClasses: elementClasses,
636+
value: value
637+
}, {'value': true})
638+
}, context, tstamp);
639+
},
640+
641+
/**
642+
* Track a form submission event
643+
*
644+
* @param string formId ID of the form
645+
* @param array formClasses Classes of the form
646+
* @param array elements Mutable elements within the form
647+
* @param array context Optional. Context relating to the event.
648+
* @param number tstamp Optional. Timestamp of the event
649+
* @return object Payload
650+
*/
651+
trackFormSubmission: function(formId, formClasses, elements, context, tstamp) {
652+
return trackUnstructEvent({
653+
schema: 'iglu:com.snowplowanalytics.snowplow/submit_form/jsonschema/1-0-0',
654+
data: removeEmptyProperties({
655+
formId: formId,
656+
formClasses: formClasses,
657+
elements: elements
658+
})
659+
}, context, tstamp);
660+
},
661+
662+
/**
663+
* Track an internal search event
664+
*
665+
* @param array terms Search terms
666+
* @param object filters Search filters
667+
* @param totalResults Number of results
668+
* @param pageResults Number of results displayed on page
669+
* @param array context Optional. Context relating to the event.
670+
* @param number tstamp Optional. Timestamp of the event
671+
* @return object Payload
672+
*/
673+
trackSiteSearch: function(terms, filters, totalResults, pageResults, context, tstamp) {
674+
return trackUnstructEvent({
675+
schema: 'iglu:com.snowplowanalytics.snowplow/site_search/jsonschema/1-0-0',
676+
data: removeEmptyProperties({
677+
terms: terms,
678+
filters: filters,
679+
totalResults: totalResults,
680+
pageResults: pageResults
681+
})
682+
}, context, tstamp);
523683
}
524684
};
525-
}
685+
};
526686

527687
module.exports = trackerCore;

core/lib/payload.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
* See: http://tools.ietf.org/html/rfc4648#page-7
2828
*/
2929
function base64urlencode(data) {
30-
if (!data) return data;
30+
if (!data) {
31+
return data;
32+
}
3133

3234
var enc = base64.base64encode(data);
33-
return enc.replace(/=/g, '')
34-
.replace(/\+/g, '-')
35-
.replace(/\//g, '_');
35+
return enc.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
3636
}
3737

3838
/*
@@ -73,7 +73,7 @@
7373

7474
var add = function (key, value) {
7575
if (value !== undefined && value !== null && value !== '') {
76-
dict[key] = value
76+
dict[key] = value;
7777
}
7878
};
7979

@@ -83,7 +83,7 @@
8383
add(key, dict[key]);
8484
}
8585
}
86-
}
86+
};
8787

8888
var addJson = function (keyIfEncoded, keyIfNotEncoded, json) {
8989

@@ -105,6 +105,6 @@
105105
return dict;
106106
}
107107
};
108-
}
108+
};
109109

110110
}());

core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "snowplow-tracker-core",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"devDependencies": {
55
"grunt": "^0.4.5",
66
"intern": "^2.0.1"

0 commit comments

Comments
 (0)