You are here

gaEcommerceProducts

Used by the following Containers: 
Variable Type: 
Custom Javascript
Manages the product List and prepares the product List for Google Analytics Enhanced Ecommerce object.
	function() {
// grabs the productList from the santiano.io Ecommerce Events and prepares it for Google Enhanced Ecommerce
var eventCommand = {{eventCommand}};
var productList = {{productList}};
var productListLength = {{productListLength}};
var gaEcommerceProducts = [];
var i;

for ( i = 0; i < productListLength; i++ ) {

    // initializing a new GA product with its common vars
    var gaIndividualProduct = {
      // Product variables defined for all EE GA hits
      'id': productList[i].productID,
      'name': productList[i].productName,
      'price': productList[i].productUnitPrice,
      'variant': productList[i].productVariant,
      'brand': productList[i].productBrand
      //'dimensionXX': productList[i].productCustomDimension
      //'metricXX': productList[i].productCustomMetric
    };

    // 5 level category magic
    var eeCategoryMagic = [];
    if (productList[i].productCategory1 != undefined) {
      eeCategoryMagic.push(productList[i].productCategory1);
    }
    if (productList[i].productCategory2 != undefined) {
      eeCategoryMagic.push(productList[i].productCategory2);
    }
    if (productList[i].productCategory3 != undefined) {
      eeCategoryMagic.push(productList[i].productCategory3);
    }
    if (productList[i].productCategory4 != undefined) {
      eeCategoryMagic.push(productList[i].productCategory4);
    }
    if (productList[i].productCategory5 != undefined) {
    eeCategoryMagic.push(productList[i].productCategory5);
    }
  	// Reminder for later... include the dynamic replacement of '/' characters in categoryVars if any :)
    // From list to string
    eeCategoryMagic = eeCategoryMagic.join('/');
    // pushing to the gaIndividualProduct as category
    gaIndividualProduct.category = eeCategoryMagic;

    // product in cart and order specific variables
    if (eventCommand == 'cartProductAdded' ||
        eventCommand == 'cartProductRemoved' || 
        eventCommand == 'checkoutStep' ||
        eventCommand == 'productRefund' ||
        eventCommand == 'orderConfirmation') {
        // these events need a productQuantity associated with each product
          gaIndividualProduct.quantity = productList[i].productQuantity;
          // gaIndividualProduct.dimensionXX = productInCartSpecificDimension
          // gaIndividualProduct.metricXX = productInCartSpecificMetric
    }

    // productImpression and productClick specific 
    if (eventCommand == 'productImpression') {
          // productImpression always need :

          gaIndividualProduct.position = productList[i].productImpressionListPosition;
              // the position of that product on that list

          gaIndividualProduct.list = {{productImpressionListName}};
              // the name of that list

          // gaIndividualProduct.dimensionXX = productImpressionSpecificDimension
          // gaIndividualProduct.metricXX = productImpressionSpecificMetric
    }

    if (eventCommand == 'productClick') {
          // productClick always need :
          gaIndividualProduct.position = productList[i].productImpressionListPosition;
              // the position of that product on that list

          // gaIndividualProduct.dimensionXX = productImpressionSpecificDimension
          // gaIndividualProduct.metricXX = productImpressionSpecificMetric
    }

    gaEcommerceProducts.push(gaIndividualProduct);
  }
  return gaEcommerceProducts;
}