I’m trying to create a dataLayer variable so I can push the product name into the event label.
The dataLayer push when a user adds a product to cart is:
JavaScript
x
30
30
1
{
2
event: "addToCart",
3
gtm: {
4
uniqueEventId: 107,
5
start: 1666871102469,
6
scrollThreshold: 75,
7
scrollUnits: "percent",
8
scrollDirection: "vertical",
9
triggers: "30151582_46"
10
},
11
customer_id: "4cfa14bf-1749-47a3-9063-e19c0bc6f3a8",
12
ecommerce: {
13
currencyCode: "NZD",
14
add: {
15
products: [
16
{
17
name: "Panadol Rapid Soluble Tablets - Packet 20",
18
id: "FAH262",
19
price: 12.28,
20
brand: "Panadol",
21
category: "Web / Dental / Consumables / Anaesthetics / Pain" +
22
" Relief",
23
variant: "",
24
quantity: 1
25
}
26
]
27
}
28
}
29
}
30
I’ve tried to create a dataLayer variable with the following
JavaScript
1
2
1
ecommerce.add.products[0].name
2
This is returning undefined in preview mode when I do test add to carts.
Where am I going wrong?
Advertisement
Answer
You might need to change the DataLayer variable to
I can’t really remember where I saw about this.
But when fetching data through here.
It is not regular get products[0].name
but products.0.name