Google Tag Manager custom javascript variable based on ecommerce datalayer object

情到浓时终转凉″ 提交于 2019-12-06 04:39:13

Since you have already created {{ecommerce}} variable , then it is very easy to fetch total amount on the basis of product's quantity and product's price.

You have to create on custom js variable Order Subtotal then in that custom js box paste below code

function(){
  var productList={{ecommerce}}.checkout.products;
  var totalAmount=0;
  for(var i=0;i<productList.length;i++)
  {
    totalAmount+=(productList[i].quantity)*(parseFloat(productList[i].price));
  }
  return totalAmount;
}

I hope this will help you resolve you issue

This JS function will take latest datalayer ecommerce event and calculate product sum:

function productSum() {
    var result = 0;
    var ecommerceDataLayers = dataLayer.filter(function(e){return e.event == "checkout";});
    if (ecommerceDataLayers.length)
    {
        var lastEcommerce = ecommerceDataLayers[ecommerceDataLayers.length-1];
        for(i=0;i<lastEcommerce.ecommerce.checkout.products.length;i++) {
            var product = lastEcommerce.ecommerce.checkout.products[i];
            result += product.quantity*parseFloat(product.price);
        }
    }
    return result;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!