Skip to content

Instantly share code, notes, and snippets.

@SearchStar
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SearchStar/0daf857ba5ed0cc20d49 to your computer and use it in GitHub Desktop.
Save SearchStar/0daf857ba5ed0cc20d49 to your computer and use it in GitHub Desktop.
Push ecommerce data into a data layer array using JavaScript
<script type="text/javascript">
var cartProducts = [];
var dataLayer = [];
for(var i=0; i<x; i++){
cartProducts.push({
'id': transactionId,
'sku': sku[i], // string; product ID
'name': name[i], // string; product name
'category': category[i], // string; category name
'price': price[i], // string; product price without tax and shipping
'quantity': quantity[i] // string: item count
});}
dataLayer.push({
'transactionId': transactionId,
'transactionAffiliation' : transactionAffiliation,
'transactionTotal': transactionTotal, // total price,
'transactionTax': transactionTax, // float; tax; number rounded to 2 decimal places
'transactionShipping': transactionShipping, // float; shipping price; number rounded to 2 decimal places
'transactionProducts': cartProducts,
'event': 'trackTrans'});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment