I have this json structure datalayer in GTM where I want to get specific json outcome. I´m trying to use the javascript map function with GTM variable for that, but it just return only the whole string with values. Below the initial json structure that I´m fetching:
dataLayer.push({
ecommerce: {
currencyCode: "USD",
purchase: {
actionField: {
id: "xxx",
revenue: xxx,
tax: 0,
shipping: xxx,
coupon: "",
discountAmount: 0,
affiliation: "xxx"
},
products: [
{
id: "1",
name: "product1",
price: xxx,
unitPriceLessTax: xxx,
unitPriceWithTax: xxx,
taxRate: 0,
quantity: 2,
category: "",
brand: "",
dimension10: "xxx"
},
{
id: "2",
name: "product2",
price: xxx,
unitPriceLessTax: xxx,
unitPriceWithTax: xxx,
taxRate: 0,
quantity: 1,
category: "",
brand: "",
dimension10: "xxx",
variant: "xxx"
}
]
}
}
})
As a first starting point I´m trying to retrieve first the product_quantity
and the product_name
key values using a mapping javascript function:
function() {
var products = {{DLV - Product Name}}; // this DLV is just the products array
return products.map(function(prod) { return prod.quantity; });
}
function() {
var products = {{DLV - Product Name}}; // this DLV is just the products array
return products.map(function(prod) { return prod.name; });
}
The problem is that I get as result 2,1 and product1,product2.
So when I´m wrapping up like below:
var result = {
lineitems : [{
quantity : "{{Product Quantity}}",
productName: "{{Product Name}}",
}]
}
I'm getting this outcome below:
{
"lineitems": [
{
"quantity": "1,2"
},
{
"productName": "product1, product2"
}
]
}
How can I fetch each single values to get this expected json structure?
{
"lineitems": [
{
"productName": "product1",
"quantity": "2,"
},
{
"productName":"product2",
"quantity": "1,"
}
]
}
Update
When I pass the returned object result from the custom variable function to the main GTM tag it shows me in debug mode not the object json result, but something like google_tag_manager[XXX].macro(187)
.
Not sure why it doesn´t show the data result calculated with the Javascript map function.