Skip to content

Instantly share code, notes, and snippets.

@philcon93
Last active March 8, 2018 07:23
Show Gist options
  • Save philcon93/625a99c44efa7cd280fcc8223290ef75 to your computer and use it in GitHub Desktop.
Save philcon93/625a99c44efa7cd280fcc8223290ef75 to your computer and use it in GitHub Desktop.
GA add/remove from cart functions

GA enhanced eCommerce

Similar to my FB Pixel functions, this will setup the product data and send to Google Analytics, measuring an Addition or Removal from Cart.

Please note, to reduce the size of the analytics.js library, enhanced ecommerce tracking is not provided in the default library. Instead it is provided as a plugin module that must be loaded before being used.

To load the enhanced ecommerce plugin, use the following command:

ga('require', 'ec');

This command must occur after you create your tracker object and before you use any of the enhanced ecommerce specific functionality.

<!-- Handle Neto data for GA -->
<script>
ga('require', 'ec');
var gaEE = {
functions: {
gaProductKitItems: function(kit_items){
var val = "";
for (var i = 0; i < kit_items.length; i++) { val += kit_items[i].assemble_qty + "x" + kit_items[i].name + "; ";}
return val;
},
gaProductSpecifics: function(specifics){
var val = "";
for (var i = 0; i < specifics.length; i++) { val += specifics[i].name + ": " + specifics[i].value + "; "; }
return val;
},
gaProductType: function(product){
if(product.kit_items.length > 0){
return gaEE.functions.gaProductKitItems(product.kit_items)
}else if(product.specifics.length > 0){
return gaEE.functions.gaProductSpecifics(product.specifics)
}
return "";
},
gaProductSetup: function(product, type) {
var productVariant = "";
if((product.kit_items.length > 0) || (product.specifics.length > 0)){
productVariant = gaEE.functions.gaProductType(product);
}
return {
'id': product.SKU ? product.SKU : product.parent_sku,
'name': product.name,
'category': product.category_fullname ? product.category_fullname : "",
'brand': product.brand ? product.brand : "",
'variant': productVariant,
'price': product.price,
'quantity': product.qty
}
},
gaSend: function(product, action, description){
console.log('GA - ' + action);
ga('ec:addProduct', product);
ga('ec:setAction', action);
ga('send', 'event', 'UX', 'click', description);
},
gaAddToCart: function() {
var nProduct = $.getLastItemAdded();
var gaProduct = gaEE.functions.gaProductSetup(nProduct);
gaEE.functions.gaSend(gaProduct, 'add', 'add to cart');
},
gaRemoveFromCart: function() {
var nProduct = $.getLastItemRemoved();
var gaProduct = gaEE.functions.gaProductSetup(nProduct);
gaEE.functions.gaSend(gaProduct, 'remove', 'removed from cart');
},
gaAddMultiToCart: function() {
var nProducts = $.getLastItemsAdded();
console.log('Sent multi add GA');
for (var i = 0; i < nProducts.length; i++) {
var gaProduct = gaEE.functions.gaProductSetup(nProducts[i]);
gaEE.functions.gaSend(gaProduct, 'add', 'add to cart');
}
},
init: function(){
nAddItemCallbacks.push(gaEE.functions.gaAddToCart);
nRemoveItemCallbacks.push(gaEE.functions.gaRemoveFromCart);
nAddMultiItemsCallbacks.push(gaEE.functions.gaAddMultiToCart);
}
}
}
gaEE.functions.init();
</script>
<!-- End Handle Neto data for GA -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment