I've trying to figure this one out since my dev is not able to pass subtotal value by product. Any help is greatly appreciated.
I added this script in GTM java script variable in order to create a product array based on data layer. But I need to add product subtotal , which should pass value of productPrice * productQuantity .
function()
{
var productName = {{dlv - productName}};
var productId = {{dlv - productId}};
var productPrice = {{dlv - productPrice}};
var productBrand = {{dlv - productBrand}};
var productCategory = {{dlv - productCategory}};
var productVariant = {{dlv - productVariantTitle}};
var productQty = {{dlv - productQuantity}};
var productSku = {{dlv - productSku}};
var productSubtotal = ;
var items = [];
for( i = 0; i < productId.length; i++ ){
items.push(
{
'item_name': productName[i],
'item_id': productId[i],
'item_brand': productBrand[i],
'item_category': productCategory[i],
'item_variant': productVariant[i],
'price': productPrice[i],
'quantity': productQty[i],
'sku': productSku[i],
'subtotal': productSubtotal[i],
}
);
}
return items;
}
I tried few options but either get total product value (all products summed up instead of itemised), or no value at all.
In this case, i tried adding a new GTM variable to pass the total amount, but i need totals by product.
function(){
var productList={{DL - Checkout - Products}};
var totalAmount=0;
for(var i=0;i<productList.length;i++)
{
totalAmount+=(productList[i].quantity)*(parseFloat(productList[i].price));
}
return totalAmount;
}
I think you already know the concept. But didn't put it together
function(){
var productName = {{dlv - productName}};
var productId = {{dlv - productId}};
var productPrice = {{dlv - productPrice}};
var productBrand = {{dlv - productBrand}};
var productCategory = {{dlv - productCategory}};
var productVariant = {{dlv - productVariantTitle}};
var productQty = {{dlv - productQuantity}};
var productSku = {{dlv - productSku}};
var productSubtotal = 0;
var items = [];
for( i = 0; i < productId.length; i++ ){
productSubtotal = productQty[i] * parseFloat(productPrice[i]);
items.push({
'item_name': productName[i],
'item_id': productId[i],
'item_brand': productBrand[i],
'item_category': productCategory[i],
'item_variant': productVariant[i],
'price': productPrice[i],
'quantity': productQty[i],
'sku': productSku[i],
'subtotal': productSubtotal,
});
}
return items;
}
Just make the subtotal in the for loop and before the items.push