Last active
October 21, 2015 12:14
-
-
Save fblundun/6ef375ec777b4a729f7a to your computer and use it in GitHub Desktop.
Enhanced Ecommerce tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// If this tag fires more than once (e.g. page view followed by ecommerce action), | |
// we don't want to repeat the trackPageView here | |
if (!window.snowplow) { | |
;(function(p,l,o,w,i,n,g){if(!p[i]){p.GlobalSnowplowNamespace=p.GlobalSnowplowNamespace||[]; | |
p.GlobalSnowplowNamespace.push(i);p[i]=function(){(p[i].q=p[i].q||[]).push(arguments) | |
};p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1; | |
n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","//d1fc8wv8zag5ca.cloudfront.net/2.5.2/sp.js","snowplow")); | |
snowplow('newTracker', 'snplow1', 'd3rkrsqld9gmqf.cloudfront.net', { | |
'appId': 'snowplowweb', | |
'contexts': { | |
'webPage': true, | |
'performanceTiming': true | |
} | |
}); | |
// Track page views, enable link clicks, and so on here | |
} | |
var ecommerce = {{ecommerce}}; | |
var actions = [ | |
"click", | |
"detail", | |
"add", | |
"remove", | |
"checkout", | |
"checkout_option", | |
"purchase", | |
"refund", | |
"promo_click" | |
]; | |
if (ecommerce) { | |
sendEnhancedEcommerceEvent(ecommerce); | |
} | |
function sendEnhancedEcommerceEvent(ecommerce) { | |
var currencyCode = ecommerce.currencyCode; | |
var action; | |
var contexts = []; | |
for (var i=0;i<actions.length;i++) { | |
if (ecommerce[actions[i]]) { | |
action = actions[i]; | |
} | |
} | |
if (ecommerce.impressions) { | |
for (var j=0;j<ecommerce.impressions.length;j++) { | |
contexts.push(getImpressionContext(ecommerce.impressions[j])); | |
} | |
} | |
if (ecommerce.promoView) { | |
for (var l=0;l<ecommerce.promoView.promotions.length;l++) { | |
contexts.push(getPromoContext(ecommerce.promoView.promotions[l])); | |
} | |
} | |
if (! action) { | |
action = 'view'; | |
} else { | |
if (ecommerce[action].products) { | |
for (var k=0;k<ecommerce[action].products.length;k++) { | |
contexts.push(getProductContext(ecommerce[action].products[k])); | |
} | |
} | |
if (ecommerce[action].actionField) { | |
contexts.push(getActionContext(ecommerce[action].actionField)); | |
} | |
} | |
var unstructEvent = getActionJson(action); | |
finalizeJsons([unstructEvent]); | |
finalizeJsons(contexts, currencyCode); | |
console.log(unstructEvent, contexts); | |
snowplow('trackUnstructEvent', unstructEvent, contexts); | |
} | |
function finalizeJsons(jsons, currencyCode) { | |
for (var i=0; i<jsons.length; i++) { | |
var data = jsons[i].data; | |
if (currencyCode) { | |
data.currency = currencyCode; | |
} | |
for (var j in data) { | |
if (data[j] === null || data[j] === undefined || data[j] === NaN) { | |
delete data[j]; | |
} | |
} | |
} | |
} | |
function getActionJson(action) { | |
return { | |
schema: 'iglu:com.google.analytics.enhanced-ecommerce/action/jsonschema/1-0-0', | |
data: { | |
action: action | |
} | |
}; | |
} | |
function getImpressionContext(impression) { | |
var data = { | |
name: impression.name, | |
id: impression.id, | |
price: parseFloat(impression.price), | |
brand: impression.brand, | |
category: impression.category, | |
variant: impression.variant, | |
list: impression.list, | |
position: parseInt(impression.position) | |
}; | |
return { | |
schema: 'iglu:com.google.analytics.enhanced-ecommerce/impressionFieldObject/jsonschema/1-0-0', | |
data: data | |
}; | |
} | |
function getProductContext(product) { | |
var data = { | |
name: product.name, | |
id: product.id, | |
price: parseFloat(product.price), | |
quantity: parseInt(product.quantity), | |
coupon: product.coupon, | |
position: parseInt(product.position), | |
brand: product.brand, | |
category: product.category, | |
variant: product.variant | |
}; | |
return { | |
schema: 'iglu:com.google.analytics.enhanced-ecommerce/productFieldObject/jsonschema/1-0-0', | |
data: data | |
}; | |
} | |
function getPromoContext(promo) { | |
var data = { | |
name: promo.name, | |
id: promo.id, | |
creative: promo.creative, | |
position: promo.position | |
} | |
return { | |
schema: 'iglu:com.google.analytics.enhanced-ecommerce/promoFieldObject/jsonschema/1-0-0', | |
data: data | |
}; | |
} | |
function getActionContext(action) { | |
var data = { | |
id: action.id, | |
affiliation: action.affiliation, | |
revenue: parseFloat(action.revenue), | |
tax: parseFloat(action.tax), | |
shipping: parseFloat(action.shipping), | |
coupon: action.coupon, | |
list: action.list, | |
step: parseInt(action.step), | |
option: action.option | |
} | |
return { | |
schema: 'iglu:com.google.analytics.enhanced-ecommerce/actionFieldObject/jsonschema/1-0-0', | |
data: data | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment