Skip to content

Instantly share code, notes, and snippets.

@marcusRB
Created June 30, 2018 14:31
Show Gist options
  • Save marcusRB/903020836466c681867c6dbfbec0219e to your computer and use it in GitHub Desktop.
Save marcusRB/903020836466c681867c6dbfbec0219e to your computer and use it in GitHub Desktop.
Measuring Product Clicks EEC dataLayer
<script>
/**
* Call this function when a user clicks on a product link. This function uses the event
* callback datalayer variable to handle navigation after the ecommerce data has been sent
* to Google Analytics.
* @param {Object} productObj An object representing a product.
*/
function(productObj) {
dataLayer.push({
'event': 'productClick',
'ecommerce': {
'click': {
'actionField': {'list': 'Search Results'}, // Optional list property.
'products': [{
'name': productObj.name, // Name or ID is required.
'id': productObj.id,
'price': productObj.price,
'brand': productObj.brand,
'category': productObj.cat,
'variant': productObj.variant,
'position': productObj.position
}]
}
},
'eventCallback': function() {
document.location = productObj.url
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment