snowplow-tracker-core
Advanced tools
Comparing version 0.1.0 to 0.2.0
688
lib/core.js
@@ -17,4 +17,12 @@ /* | ||
var payload = require('./payload.js'); | ||
var uuid = require('uuid'); | ||
module.exports = function trackerCore(base64, callback) { | ||
/** | ||
* Create a tracker core object | ||
* | ||
* @param boolean base64 Whether to base 64 encode contexts and unstructured event JSONs | ||
* @param callback function Function applied to every payload dictionary object | ||
* @return object Tracker core | ||
*/ | ||
function trackerCore(base64, callback) { | ||
@@ -30,11 +38,2 @@ // base 64 encoding should default to true | ||
/** | ||
* Turn base 64 encoding on or off | ||
* | ||
* @param boolean encode key Field name | ||
*/ | ||
function setBase64Encoding(encode) { | ||
base64 = encode; | ||
} | ||
/** | ||
* Set a persistent key-value pair to be added to every payload | ||
@@ -50,24 +49,2 @@ * | ||
/** | ||
* Merges a dictionary into payloadPairs | ||
* | ||
* @param object dict Dictionary to add | ||
*/ | ||
function addPayloadDict(dict) { | ||
for (var key in dict) { | ||
if (dict.hasOwnProperty(key)) { | ||
payloadPairs[key] = dict[key]; | ||
} | ||
} | ||
} | ||
/** | ||
* Replace payloadPairs with a new dictionary | ||
* | ||
* @param object dict New dictionary | ||
*/ | ||
function resetPayloadPairs(dict) { | ||
payloadPairs = payload.isJson(dict) ? dict : {}; | ||
} | ||
/** | ||
* Returns a copy of a JSON with undefined and null properties removed | ||
@@ -110,5 +87,8 @@ * | ||
* @param array contexts Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
*/ | ||
function track(sb, context) { | ||
function track(sb, context, tstamp) { | ||
sb.addDict(payloadPairs); | ||
sb.add('eid', uuid.v4()); | ||
sb.add('dtm', tstamp || new Date().getTime()); | ||
if (context) { | ||
@@ -131,5 +111,6 @@ sb.addJson('cx', 'co', completeContexts(context)); | ||
* @param array context Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
function trackUnstructEvent(properties, context) { | ||
function trackUnstructEvent(properties, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
@@ -144,280 +125,409 @@ var ueJson = { | ||
return track(sb, context); | ||
return track(sb, context, tstamp); | ||
} | ||
/** | ||
* Track a structured event | ||
* | ||
* @param string category The name you supply for the group of objects you want to track | ||
* @param string action A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object | ||
* @param string label (optional) An optional string to provide additional dimensions to the event data | ||
* @param string property (optional) Describes the object or the action performed on it, e.g. quantity of item added to basket | ||
* @param int|float|string value (optional) An integer that you can use to provide numerical data about the user event | ||
* @param array Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackStructEvent(category, action, label, property, value, context) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'se'); // 'se' for Structured Event | ||
sb.add('se_ca', category); | ||
sb.add('se_ac', action); | ||
sb.add('se_la', label); | ||
sb.add('se_pr', property); | ||
sb.add('se_va', value); | ||
return { | ||
return track(sb, context); | ||
} | ||
/** | ||
* Turn base 64 encoding on or off | ||
* | ||
* @param boolean encode key Field name | ||
*/ | ||
setBase64Encoding: function (encode) { | ||
base64 = encode; | ||
}, | ||
/** | ||
* Track a screen view unstructured event | ||
* | ||
* @param string name The name of the screen | ||
* @param string id The ID of the screen | ||
* @return object Payload | ||
*/ | ||
function trackScreenView(name, id, context) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
name: name, | ||
id: id | ||
}) | ||
}); | ||
} | ||
addPayloadPair: addPayloadPair, | ||
/** | ||
* Merges a dictionary into payloadPairs | ||
* | ||
* @param object dict Dictionary to add | ||
*/ | ||
addPayloadDict: function (dict) { | ||
for (var key in dict) { | ||
if (dict.hasOwnProperty(key)) { | ||
payloadPairs[key] = dict[key]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Log the page view / visit | ||
* | ||
* @param string customTitle The user-defined page title to attach to this page view | ||
* @param array context Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackPageView(pageUrl, pageTitle, referrer, context) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'pv'); // 'pv' for Page View | ||
sb.add('url', pageUrl); | ||
sb.add('page', pageTitle); | ||
sb.add('refr', referrer); | ||
/** | ||
* Replace payloadPairs with a new dictionary | ||
* | ||
* @param object dict New dictionary | ||
*/ | ||
resetPayloadPairs: function (dict) { | ||
payloadPairs = payload.isJson(dict) ? dict : {}; | ||
}, | ||
return track(sb, context); | ||
} | ||
/** | ||
* Set the tracker version | ||
* | ||
* @param string version | ||
*/ | ||
setTrackerVersion: function (version) { | ||
addPayloadPair('tv', version) | ||
}, | ||
/** | ||
* Log that a user is still viewing a given page | ||
* by sending a page ping. | ||
* | ||
* @param string pageTitle The page title to attach to this page ping | ||
* @param array context Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackPagePing(pageUrl, pageTitle, referrer, context) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'pp'); // 'pv' for Page View | ||
sb.add('url', pageUrl); | ||
sb.add('page', pageTitle); | ||
sb.add('refr', referrer); | ||
/** | ||
* Set the tracker namespace | ||
* | ||
* @param string name | ||
*/ | ||
setTrackerNamespace: function (name) { | ||
addPayloadPair('tna', name); | ||
}, | ||
return track(sb, context); | ||
} | ||
/** | ||
* Set the application ID | ||
* | ||
* @param string appId | ||
*/ | ||
setAppId: function (appId) { | ||
addPayloadPair('aid', appId) | ||
}, | ||
/** | ||
* Track an ecommerce transaction | ||
* | ||
* @param string orderId Required. Internal unique order id number for this transaction. | ||
* @param string affiliation Optional. Partner or store affiliation. | ||
* @param string total Required. Total amount of the transaction. | ||
* @param string tax Optional. Tax amount of the transaction. | ||
* @param string shipping Optional. Shipping charge for the transaction. | ||
* @param string city Optional. City to associate with transaction. | ||
* @param string state Optional. State to associate with transaction. | ||
* @param string country Optional. Country to associate with transaction. | ||
* @param string currency Optional. Currency to associate with this transaction. | ||
* @param array context Optional. Context relating to the event. | ||
* @return object Payload | ||
*/ | ||
function trackEcommerceTransaction(orderId, affiliation, totalValue, taxValue, shipping, city, | ||
state, country, currency, context) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'tr'); // 'tr' for Transaction | ||
sb.add("tr_id", orderId); | ||
sb.add("tr_af", affiliation); | ||
sb.add("tr_tt", totalValue); | ||
sb.add("tr_tx", taxValue); | ||
sb.add("tr_sh", shipping); | ||
sb.add("tr_ci", city); | ||
sb.add("tr_st", state); | ||
sb.add("tr_co", country); | ||
sb.add("tr_cu", currency); | ||
/** | ||
* Set the platform | ||
* | ||
* @param string value | ||
*/ | ||
setPlatform: function (value) { | ||
addPayloadPair('p', value); | ||
}, | ||
return track(sb, context); | ||
} | ||
/** | ||
* Set the user ID | ||
* | ||
* @param string userId | ||
*/ | ||
setUserId: function (userId) { | ||
addPayloadPair('uid', userId); | ||
}, | ||
/** | ||
* Track an ecommerce transaction item | ||
* | ||
* @param string orderId Required Order ID of the transaction to associate with item. | ||
* @param string sku Required. Item's SKU code. | ||
* @param string name Optional. Product name. | ||
* @param string category Optional. Product category. | ||
* @param string price Required. Product price. | ||
* @param string quantity Required. Purchase quantity. | ||
* @param string currency Optional. Product price currency. | ||
* @param array context Optional. Context relating to the event. | ||
* @return object Payload | ||
*/ | ||
function trackEcommerceTransactionItem(orderId, sku, name, category, price, quantity, currency, context) { | ||
var sb = payload.payloadBuilder(base64) | ||
sb.add("e", "ti"); // 'tr' for Transaction Item | ||
sb.add("ti_id", orderId); | ||
sb.add("ti_sk", sku); | ||
sb.add("ti_nm", name); | ||
sb.add("ti_ca", category); | ||
sb.add("ti_pr", price); | ||
sb.add("ti_qu", quantity); | ||
sb.add("ti_cu", currency); | ||
/** | ||
* Set the screen resolution | ||
* | ||
* @param number width | ||
* @param number height | ||
*/ | ||
setScreenResolution: function (width, height) { | ||
addPayloadPair('res', width + 'x' + height); | ||
}, | ||
return track(sb, context); | ||
} | ||
/** | ||
* Set the viewport dimensions | ||
* | ||
* @param number width | ||
* @param number height | ||
*/ | ||
setViewport: function (width, height) { | ||
addPayloadPair('vp', width + 'x' + height); | ||
}, | ||
/** | ||
* Set the color depth | ||
* | ||
* @param number depth | ||
*/ | ||
setColorDepth: function (depth) { | ||
addPayloadPair('cd', depth); | ||
}, | ||
/** | ||
* Log the link or click with the server | ||
* | ||
* @param string elementId | ||
* @param array elementClasses | ||
* @param string elementTarget | ||
* @param string targetUrl | ||
* @param array context Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackLinkClick(targetUrl, elementId, elementClasses, elementTarget, context) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
targetUrl: targetUrl, | ||
elementId: elementId, | ||
elementClasses: elementClasses, | ||
elementTarget: elementTarget | ||
}), | ||
}; | ||
/** | ||
* Set the timezone | ||
* | ||
* @param string timezone | ||
*/ | ||
setTimezone: function (timezone) { | ||
addPayloadPair('tz', timezone); | ||
}, | ||
return trackUnstructEvent(eventJson, context); | ||
} | ||
/** | ||
* Set the language | ||
* | ||
* @param string lang | ||
*/ | ||
setLang: function (lang) { | ||
addPayloadPair('lang', lang); | ||
}, | ||
/** | ||
* Track an ad being served | ||
* | ||
* @param string impressionId Identifier for a particular ad impression | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param number cost Cost | ||
* @param string bannerId Identifier for the ad banner displayed | ||
* @param string zoneId Identifier for the ad zone | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackAdImpression(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId, context) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
impressionId: impressionId, | ||
costModel: costModel, | ||
cost: cost, | ||
targetUrl: targetUrl, | ||
bannerId: bannerId, | ||
zoneId: zoneId, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
/** | ||
* Set the IP address | ||
* | ||
* @param string appId | ||
*/ | ||
setIpAddress: function (ip) { | ||
addPayloadPair('ip', ip) | ||
}, | ||
return trackUnstructEvent(eventJson, context); | ||
} | ||
trackUnstructEvent: trackUnstructEvent, | ||
/** | ||
* Track an ad being clicked | ||
* | ||
* @param string clickId Identifier for the ad click | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param number cost Cost | ||
* @param string targetUrl (required) The link's target URL | ||
* @param string bannerId Identifier for the ad banner displayed | ||
* @param string zoneId Identifier for the ad zone | ||
* @param string impressionId Identifier for a particular ad impression | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackAdClick(targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId, context) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_click/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
targetUrl: targetUrl, | ||
clickId: clickId, | ||
costModel: costModel, | ||
cost: cost, | ||
bannerId: bannerId, | ||
zoneId: zoneId, | ||
impressionId: impressionId, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
/** | ||
* Log the page view / visit | ||
* | ||
* @param string customTitle The user-defined page title to attach to this page view | ||
* @param array context Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackPageView: function (pageUrl, pageTitle, referrer, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'pv'); // 'pv' for Page View | ||
sb.add('url', pageUrl); | ||
sb.add('page', pageTitle); | ||
sb.add('refr', referrer); | ||
return trackUnstructEvent(eventJson, context); | ||
} | ||
return track(sb, context, tstamp); | ||
}, | ||
/** | ||
* Log that a user is still viewing a given page | ||
* by sending a page ping. | ||
* | ||
* @param string pageTitle The page title to attach to this page ping | ||
* @param array context Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackPagePing: function (pageUrl, pageTitle, referrer, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'pp'); // 'pv' for Page View | ||
sb.add('url', pageUrl); | ||
sb.add('page', pageTitle); | ||
sb.add('refr', referrer); | ||
/** | ||
* Track an ad conversion event | ||
* | ||
* @param string conversionId Identifier for the ad conversion event | ||
* @param number cost Cost | ||
* @param string category The name you supply for the group of objects you want to track | ||
* @param string action A string that is uniquely paired with each category | ||
* @param string property Describes the object of the conversion or the action performed on it | ||
* @param number initialValue Revenue attributable to the conversion at time of conversion | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @return object Payload | ||
*/ | ||
function trackAdConversion(conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId, context) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_conversion/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
conversionId: conversionId, | ||
costModel: costModel, | ||
cost: cost, | ||
category: category, | ||
action: action, | ||
property: property, | ||
initialValue: initialValue, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
return track(sb, context, tstamp); | ||
}, | ||
/** | ||
* Track a structured event | ||
* | ||
* @param string category The name you supply for the group of objects you want to track | ||
* @param string action A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object | ||
* @param string label (optional) An optional string to provide additional dimensions to the event data | ||
* @param string property (optional) Describes the object or the action performed on it, e.g. quantity of item added to basket | ||
* @param int|float|string value (optional) An integer that you can use to provide numerical data about the user event | ||
* @param array Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackStructEvent: function (category, action, label, property, value, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'se'); // 'se' for Structured Event | ||
sb.add('se_ca', category); | ||
sb.add('se_ac', action); | ||
sb.add('se_la', label); | ||
sb.add('se_pr', property); | ||
sb.add('se_va', value); | ||
return trackUnstructEvent(eventJson, context); | ||
} | ||
return track(sb, context, tstamp); | ||
}, | ||
return { | ||
setBase64Encoding: setBase64Encoding, | ||
addPayloadPair: addPayloadPair, | ||
addPayloadDict: addPayloadDict, | ||
resetPayloadPairs: resetPayloadPairs, | ||
trackUnstructEvent: trackUnstructEvent, | ||
trackStructEvent: trackStructEvent, | ||
trackPageView: trackPageView, | ||
trackPagePing: trackPagePing, | ||
trackEcommerceTransaction: trackEcommerceTransaction, | ||
trackEcommerceTransactionItem: trackEcommerceTransactionItem, | ||
trackScreenView: trackScreenView, | ||
trackLinkClick: trackLinkClick, | ||
trackAdImpression: trackAdImpression, | ||
trackAdClick: trackAdClick, | ||
trackAdConversion: trackAdConversion | ||
/** | ||
* Track an ecommerce transaction | ||
* | ||
* @param string orderId Required. Internal unique order id number for this transaction. | ||
* @param string affiliation Optional. Partner or store affiliation. | ||
* @param string total Required. Total amount of the transaction. | ||
* @param string tax Optional. Tax amount of the transaction. | ||
* @param string shipping Optional. Shipping charge for the transaction. | ||
* @param string city Optional. City to associate with transaction. | ||
* @param string state Optional. State to associate with transaction. | ||
* @param string country Optional. Country to associate with transaction. | ||
* @param string currency Optional. Currency to associate with this transaction. | ||
* @param array context Optional. Context relating to the event. | ||
* @param number tstamp Optional. Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackEcommerceTransaction: function (orderId, affiliation, totalValue, taxValue, shipping, city, state, country, currency, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64); | ||
sb.add('e', 'tr'); // 'tr' for Transaction | ||
sb.add("tr_id", orderId); | ||
sb.add("tr_af", affiliation); | ||
sb.add("tr_tt", totalValue); | ||
sb.add("tr_tx", taxValue); | ||
sb.add("tr_sh", shipping); | ||
sb.add("tr_ci", city); | ||
sb.add("tr_st", state); | ||
sb.add("tr_co", country); | ||
sb.add("tr_cu", currency); | ||
return track(sb, context, tstamp); | ||
}, | ||
/** | ||
* Track an ecommerce transaction item | ||
* | ||
* @param string orderId Required Order ID of the transaction to associate with item. | ||
* @param string sku Required. Item's SKU code. | ||
* @param string name Optional. Product name. | ||
* @param string category Optional. Product category. | ||
* @param string price Required. Product price. | ||
* @param string quantity Required. Purchase quantity. | ||
* @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 | ||
*/ | ||
trackEcommerceTransactionItem: function (orderId, sku, name, category, price, quantity, currency, context, tstamp) { | ||
var sb = payload.payloadBuilder(base64) | ||
sb.add("e", "ti"); // 'tr' for Transaction Item | ||
sb.add("ti_id", orderId); | ||
sb.add("ti_sk", sku); | ||
sb.add("ti_nm", name); | ||
sb.add("ti_ca", category); | ||
sb.add("ti_pr", price); | ||
sb.add("ti_qu", quantity); | ||
sb.add("ti_cu", currency); | ||
return track(sb, context, tstamp); | ||
}, | ||
/** | ||
* Track a screen view unstructured event | ||
* | ||
* @param string name The name of the screen | ||
* @param string id The ID of the screen | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackScreenView: function (name, id, context, tstamp) { | ||
return trackUnstructEvent({ | ||
schema: 'iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
name: name, | ||
id: id | ||
}) | ||
}, context, tstamp); | ||
}, | ||
/** | ||
* Log the link or click with the server | ||
* | ||
* @param string elementId | ||
* @param array elementClasses | ||
* @param string elementTarget | ||
* @param string targetUrl | ||
* @param array context Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackLinkClick: function (targetUrl, elementId, elementClasses, elementTarget, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
targetUrl: targetUrl, | ||
elementId: elementId, | ||
elementClasses: elementClasses, | ||
elementTarget: elementTarget | ||
}), | ||
}; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
}, | ||
/** | ||
* Track an ad being served | ||
* | ||
* @param string impressionId Identifier for a particular ad impression | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param number cost Cost | ||
* @param string bannerId Identifier for the ad banner displayed | ||
* @param string zoneId Identifier for the ad zone | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackAdImpression: function(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
impressionId: impressionId, | ||
costModel: costModel, | ||
cost: cost, | ||
targetUrl: targetUrl, | ||
bannerId: bannerId, | ||
zoneId: zoneId, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
}, | ||
/** | ||
* Track an ad being clicked | ||
* | ||
* @param string clickId Identifier for the ad click | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param number cost Cost | ||
* @param string targetUrl (required) The link's target URL | ||
* @param string bannerId Identifier for the ad banner displayed | ||
* @param string zoneId Identifier for the ad zone | ||
* @param string impressionId Identifier for a particular ad impression | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackAdClick: function (targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_click/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
targetUrl: targetUrl, | ||
clickId: clickId, | ||
costModel: costModel, | ||
cost: cost, | ||
bannerId: bannerId, | ||
zoneId: zoneId, | ||
impressionId: impressionId, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
}, | ||
/** | ||
* Track an ad conversion event | ||
* | ||
* @param string conversionId Identifier for the ad conversion event | ||
* @param number cost Cost | ||
* @param string category The name you supply for the group of objects you want to track | ||
* @param string action A string that is uniquely paired with each category | ||
* @param string property Describes the object of the conversion or the action performed on it | ||
* @param number initialValue Revenue attributable to the conversion at time of conversion | ||
* @param string advertiserId Identifier for the advertiser | ||
* @param string costModel The cost model. 'cpa', 'cpc', or 'cpm' | ||
* @param string campaignId Identifier for the campaign which the banner belongs to | ||
* @param array Custom contexts relating to the event | ||
* @param number tstamp Timestamp of the event | ||
* @return object Payload | ||
*/ | ||
trackAdConversion: function (conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId, context, tstamp) { | ||
var eventJson = { | ||
schema: 'iglu:com.snowplowanalytics.snowplow/ad_conversion/jsonschema/1-0-0', | ||
data: removeEmptyProperties({ | ||
conversionId: conversionId, | ||
costModel: costModel, | ||
cost: cost, | ||
category: category, | ||
action: action, | ||
property: property, | ||
initialValue: initialValue, | ||
advertiserId: advertiserId, | ||
campaignId: campaignId | ||
}) | ||
}; | ||
return trackUnstructEvent(eventJson, context, tstamp); | ||
} | ||
}; | ||
} | ||
module.exports = trackerCore; |
{ | ||
"name": "snowplow-tracker-core", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"devDependencies": { | ||
"JSON": "~1.0.0", | ||
"grunt": "^0.4.5", | ||
"intern": "^2.0.1" | ||
}, | ||
"contributors": [ | ||
"contributors": [ | ||
"Alex Dean", | ||
@@ -13,3 +13,3 @@ "Simon Andersson", | ||
], | ||
"description": "Snowplow tracker core", | ||
"description": "Core functionality for the Snowplow JavaScript trackers (browser JavaScript; Node.js; Segment.io server-side)", | ||
"repository": { | ||
@@ -26,3 +26,7 @@ "type": "git", | ||
], | ||
"license": "Apache-2.0" | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"JSON": "^1.0.0", | ||
"uuid": "^1.4.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# Snowplow Tracker Core | ||
# Snowplow JavaScript Tracker Core [![npm version][npm-image]][npm-url] | ||
@@ -9,5 +9,5 @@ Core module to be used by all Snowplow JavaScript trackers. | ||
```bash | ||
npm install snowplow-tracker-core | ||
``` | ||
npm install intern --save-dev | ||
``` | ||
@@ -22,11 +22,18 @@ ## Example | ||
// Add this name-value pair to all payloads | ||
coreInstance.addPayloadPair('dtm', new Date().getTime()); | ||
// Add a name-value pair to all payloads | ||
coreInstance.addPayloadPair('vid', 2); | ||
// Add each name-value pair in this dictionary to all payloads | ||
// Add each name-value pair in a dictionary to all payloads | ||
coreInstance.addPayloadDict({ | ||
'p': 'web', // platform | ||
'tv': 'js-3.0.0' // tracker version | ||
'ds': '1160x620', | ||
'fp': 4070134789 | ||
}); | ||
// Add name-value pairs to all payloads using convenience methods | ||
coreInstance.setTrackerVersion('js-3.0.0'); | ||
coreInstance.setPlatform('web'); | ||
coreInstance.setUserId('user-321'); | ||
coreInstance.setColorDepth(24); | ||
coreInstance.setViewport(600, 400); | ||
// Track a page view with URL and title | ||
@@ -38,8 +45,15 @@ var pageViewPayload = coreInstance.trackPageView('http://www.example.com', 'landing page'); | ||
{ | ||
'tv': 'js-2.0.0', | ||
'p': 'web', | ||
'dtm': 1406879959702, | ||
'e': 'pv', | ||
'url': 'http://www.example.com', | ||
'page': 'landing page' | ||
'page': 'landing page', | ||
'uid': 'user-321', | ||
'vd': 2, | ||
'ds': '1160x620', | ||
'fp': 4070134789 | ||
'tv': 'js-3.0.0', | ||
'p': 'web', | ||
'cd': 24, | ||
'vp': '600x400', | ||
'dtm': 1406879959702, // timestamp | ||
'eid': '0718a85a-45dc-4f71-a949-27870442ed7d' // UUID | ||
} | ||
@@ -73,3 +87,5 @@ */ | ||
} | ||
} | ||
}, | ||
'dtm': 1406879973439, | ||
'eid': '956c6670-cbf6-460b-9f96-143e0320fdf6' | ||
} | ||
@@ -97,6 +113,11 @@ */ | ||
coreInstance.setBase64Encoding(false); // Base 64 encoding is now off | ||
``` | ||
## Documentation | ||
For more information on the Snowplow JavaScript Tracker Core's API, view its [wiki page][wiki]. | ||
## Copyright and license | ||
The Snowplow Tracker Core is copyright 2014 Snowplow Analytics Ltd. | ||
The Snowplow JavaScript Tracker Core is copyright 2014 Snowplow Analytics Ltd. | ||
@@ -113,1 +134,5 @@ Licensed under the [Apache License, Version 2.0][apache-license] (the "License"); | ||
[apache-license]: http://www.apache.org/licenses/LICENSE-2.0 | ||
[npm-url]: http://badge.fury.io/js/snowplow-tracker-core | ||
[npm-image]: https://badge.fury.io/js/snowplow-tracker-core.svg | ||
[wiki]: https://github.com/snowplow/snowplow/wiki/Javascript-Tracker-Core |
Sorry, the diff of this file is not supported yet
38873
657
133
2
+ AddedJSON@^1.0.0
+ Addeduuid@^1.4.1
+ AddedJSON@1.0.0(transitive)
+ Addeduuid@1.4.2(transitive)