I am creating a GTM variable that amends a custom metric in our data layer (standard enhanced e-commerce), that is then passed through to a Google Analytics Event. The full code looks like the below
function() {
var ecom = JSON.parse(JSON.stringify({{dlv_ecommerce}}));
var orderItems = {{dlv_purchase_ecommerce.purchase.products}};
var convertFrom = {{dlv_purchase_ecommerce.currencyCode}};
var rates = {{dlv_rates}};
var eventItem = orderItems[i].category;
var marginPrice;
if(eventItem=="Red Hoodie"){
marginPrice=orderItems[i].price * 0.10;
}
if(eventItem=="Blue T-Shirt"){
marginPrice=orderItems[i].price * 0.05;
}
else{
marginPrice=orderItems[i].metric5;
}
var n = orderItems.length;
lineItems = [];
for (i = 0; i < n; i++) {
lineItems.push({
"name": orderItems[i].name,
"id": orderItems[i].id,
"price": orderItems[i].price,
"brand": orderItems[i].brand,
"category": orderItems[i].category,
"quantity": orderItems[i].quantity,
"dimension2": orderItems[i].dimension2,
"dimension8": orderItems[i].dimension8,
"dimension9": orderItems[i].dimension9,
"dimension10": orderItems[i].dimension10,
"dimension11": orderItems[i].dimension11,
"metric1": orderItems[i].metric1,
"metric2": orderItems[i].metric2,
"metric3": orderItems[i].metric3,
"metric4": orderItems[i].metric4,
"metric5": marginPrice / rates[convertFrom],
});
};
var ecomAddProducts = {
"products": lineItems,
};
return {'ecommerce' : {'purchase' : {'products' : ecomAddProducts}}};
}
Everything here works, apart from metric 5, and so i assume also this section (If I do "metric5": orderItems[i].metric5 / rates[convertFrom] It works fine, which is why i'm guessing it's this section)
var eventItem = orderItems[i].category;
var marginPrice;
if(eventItem=="Red Hoodie"){
marginPrice=orderItems[i].price * 0.10;
}
if(eventItem=="Blue T-Shirt"){
marginPrice=orderItems[i].price * 0.05;
}
else{
marginPrice=orderItems[i].metric5;
}
As soon as I add these sections in I get an undefined result in GTM preview mode. Can anyone spot what is causing the issue?