snowplow-tracker-core
Advanced tools
Comparing version 0.2.0 to 0.3.0
192
lib/core.js
@@ -50,8 +50,10 @@ /* | ||
* @param object eventJson JSON to clean | ||
* @param object exemptFields Set of fields which should not be removed even if empty | ||
* @return object A cleaned copy of eventJson | ||
*/ | ||
function removeEmptyProperties(eventJson) { | ||
function removeEmptyProperties(eventJson, exemptFields) { | ||
var ret = {}; | ||
exemptFields = exemptFields || {}; | ||
for (var k in eventJson) { | ||
if (eventJson[k] !== null && typeof eventJson[k] !== 'undefined') { | ||
if (exemptFields[k] || (eventJson[k] !== null && typeof eventJson[k] !== 'undefined')) { | ||
ret[k] = eventJson[k]; | ||
@@ -86,2 +88,3 @@ } | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload after the callback is applied | ||
*/ | ||
@@ -96,8 +99,7 @@ function track(sb, context, tstamp) { | ||
var payload = sb.build(); | ||
if (typeof callback === 'function') { | ||
callback(payload); | ||
callback(sb); | ||
} | ||
return payload; | ||
return sb; | ||
} | ||
@@ -167,3 +169,3 @@ | ||
setTrackerVersion: function (version) { | ||
addPayloadPair('tv', version) | ||
addPayloadPair('tv', version); | ||
}, | ||
@@ -186,3 +188,3 @@ | ||
setAppId: function (appId) { | ||
addPayloadPair('aid', appId) | ||
addPayloadPair('aid', appId); | ||
}, | ||
@@ -261,3 +263,3 @@ | ||
setIpAddress: function (ip) { | ||
addPayloadPair('ip', ip) | ||
addPayloadPair('ip', ip); | ||
}, | ||
@@ -289,2 +291,6 @@ | ||
* @param string pageTitle The page title to attach to this page ping | ||
* @param minxoffset Minimum page x offset seen in the last ping period | ||
* @param maxXOffset Maximum page x offset seen in the last ping period | ||
* @param minYOffset Minimum page y offset seen in the last ping period | ||
* @param maxYOffset Maximum page y offset seen in the last ping period | ||
* @param array context Custom contexts relating to the event | ||
@@ -294,8 +300,12 @@ * @param number tstamp Timestamp of the event | ||
*/ | ||
trackPagePing: function (pageUrl, pageTitle, referrer, context, tstamp) { | ||
trackPagePing: function (pageUrl, pageTitle, referrer, minXOffset, maxXOffset, minYOffset, maxYOffset, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'pp'); // 'pv' for Page View | ||
sb.add('e', 'pp'); // 'pp' for Page Ping | ||
sb.add('url', pageUrl); | ||
sb.add('page', pageTitle); | ||
sb.add('refr', referrer); | ||
sb.add('pp_mix', minXOffset); | ||
sb.add('pp_max', maxXOffset); | ||
sb.add('pp_miy', minYOffset); | ||
sb.add('pp_may', maxYOffset); | ||
@@ -375,3 +385,3 @@ return track(sb, context, tstamp); | ||
trackEcommerceTransactionItem: function (orderId, sku, name, category, price, quantity, currency, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64) | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add("e", "ti"); // 'tr' for Transaction Item | ||
@@ -410,6 +420,7 @@ sb.add("ti_id", orderId); | ||
* | ||
* @param string targetUrl | ||
* @param string elementId | ||
* @param array elementClasses | ||
* @param string elementTarget | ||
* @param string targetUrl | ||
* @param string elementContent innerHTML of the link | ||
* @param array context Custom contexts relating to the event | ||
@@ -419,5 +430,5 @@ * @param number tstamp Timestamp of the event | ||
*/ | ||
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, context, tstamp) { | ||
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, elementContent, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0', | ||
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1', | ||
data: removeEmptyProperties({ | ||
@@ -427,3 +438,4 @@ targetUrl: targetUrl, | ||
elementClasses: elementClasses, | ||
elementTarget: elementTarget | ||
elementTarget: elementTarget, | ||
elementContent: elementContent | ||
}), | ||
@@ -535,6 +547,154 @@ }; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
}, | ||
/** | ||
* Track a social event | ||
* | ||
* @param string action Social action performed | ||
* @param string network Social network | ||
* @param string target Object of the social action e.g. the video liked, the tweet retweeted | ||
* @param array Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackSocialInteraction: function (action, network, target, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/social_interaction/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
action: action, | ||
network: network, | ||
target: target | ||
}) | ||
}; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
}, | ||
/** | ||
* Track an add-to-cart event | ||
* | ||
* @param string sku Required. Item's SKU code. | ||
* @param string name Optional. Product name. | ||
* @param string category Optional. Product category. | ||
* @param string unitPrice Optional. Product price. | ||
* @param string quantity Required. Quantity added. | ||
* @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 | ||
*/ | ||
trackAddToCart: function (sku, name, category, unitPrice, quantity, currency, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
sku: sku, | ||
name: name, | ||
category: category, | ||
unitPrice: unitPrice, | ||
quantity: quantity, | ||
currency: currency | ||
}) | ||
}, context, tstamp); | ||
}, | ||
/** | ||
* Track a remove-from-cart event | ||
* | ||
* @param string sku Required. Item's SKU code. | ||
* @param string name Optional. Product name. | ||
* @param string category Optional. Product category. | ||
* @param string unitPrice Optional. Product price. | ||
* @param string quantity Required. Quantity removed. | ||
* @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 | ||
*/ | ||
trackRemoveFromCart: function (sku, name, category, unitPrice, quantity, currency, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/remove_from_cart/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
sku: sku, | ||
name: name, | ||
category: category, | ||
unitPrice: unitPrice, | ||
quantity: quantity, | ||
currency: currency | ||
}) | ||
}, context, tstamp); | ||
}, | ||
/** | ||
* Track the value of a form field changing | ||
* | ||
* @param string formId The parent form ID | ||
* @param string elementId ID of the changed element | ||
* @param string nodeName "INPUT", "TEXTAREA", or "SELECT" | ||
* @param string type Type of the changed element if its type is "INPUT" | ||
* @param array elementClasses List of classes of the changed element | ||
* @param string value The new value of the changed element | ||
* @param array context Optional. Context relating to the event. | ||
* @param number tstamp Optional. Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackFormChange: function(formId, elementId, nodeName, type, elementClasses, value, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/change_form/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
formId: formId, | ||
elementId: elementId, | ||
nodeName: nodeName, | ||
type: type, | ||
elementClasses: elementClasses, | ||
value: value | ||
}, {'value': true}) | ||
}, context, tstamp); | ||
}, | ||
/** | ||
* Track a form submission event | ||
* | ||
* @param string formId ID of the form | ||
* @param array formClasses Classes of the form | ||
* @param array elements Mutable elements within the form | ||
* @param array context Optional. Context relating to the event. | ||
* @param number tstamp Optional. Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackFormSubmission: function(formId, formClasses, elements, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/submit_form/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
formId: formId, | ||
formClasses: formClasses, | ||
elements: elements | ||
}) | ||
}, context, tstamp); | ||
}, | ||
/** | ||
* Track an internal search event | ||
* | ||
* @param array terms Search terms | ||
* @param object filters Search filters | ||
* @param totalResults Number of results | ||
* @param pageResults Number of results displayed on page | ||
* @param array context Optional. Context relating to the event. | ||
* @param number tstamp Optional. Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackSiteSearch: function(terms, filters, totalResults, pageResults, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/site_search/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
terms: terms, | ||
filters: filters, | ||
totalResults: totalResults, | ||
pageResults: pageResults | ||
}) | ||
}, context, tstamp); | ||
} | ||
}; | ||
} | ||
}; | ||
module.exports = trackerCore; |
@@ -30,8 +30,8 @@ /* | ||
function base64urlencode(data) { | ||
if (!data) return data; | ||
if (!data) { | ||
return data; | ||
} | ||
var enc = base64.base64encode(data); | ||
return enc.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_'); | ||
return enc.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_'); | ||
} | ||
@@ -77,3 +77,3 @@ | ||
if (value !== undefined && value !== null && value !== '') { | ||
dict[key] = value | ||
dict[key] = value; | ||
} | ||
@@ -88,3 +88,3 @@ }; | ||
} | ||
} | ||
}; | ||
@@ -111,4 +111,4 @@ var addJson = function (keyIfEncoded, keyIfNotEncoded, json) { | ||
}; | ||
} | ||
}; | ||
}()); |
{ | ||
"name": "snowplow-tracker-core", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "grunt": "^0.4.5", |
Sorry, the diff of this file is not supported yet
45174
810