I'm trying to calculate the value of the shopping cart based on data contianed in the GTM datalayer ecommerce object. I'm bringing the entire ecommerce object into a datalayer variable so I can work with it in the custom js variable, but I'm unable to make the recursive calculation work.
Here's an example of code from the datalayer:
{
"event": "checkout",
"ecommerce": {
"checkout": {
"products": [
{
"name": "coat",
"id": "14M",
"quantity": 1,
"price": "100.00"
},
{
"name": "pants",
"id": "12L",
"quantity": 3,
"price": "50.00"
}
]
}
}
}`
My goal is to identifies the number of products in the product array and then perform the calculation of products price * product quantity as many times as is required to go through the whol product array. In this case, it would be something like this:
(1*100) + (3*50) = 250.
I've tried modifying the code from Simo Ahava's related blogpost without success. The differences between his blogpost and my intention are:
- I don't need to set the cart value as an ecommerce Custom Metric. I want to just return the value of the cart in the custom js.
- I don't want this to involve adding or deleting items from the cart. Instead, I just want to calculate the value when the GTM Event "checkout" is triggered and calculate the value of the contained products.
Thanks in advance.