I am facing a problem as I have no knowledge in Javascript. The thing is I am using this custom html in GTM to send the add to cart event data to dataLayer,
"event": "add_to_cart", 'ecommerce': { 'currency': {{currency_add_to_cart}}, // Replace 'USD' with your currency variable 'value': {{estimated_total_initiate_checkout_js}}, // Replace with your total cart value variable 'items': [ { "item_id": {{product_id - add to cart js}}, [] "item_name": "{{item_name-add_to_cart}}", [] "price": {{item_price-add_to_cart}}, [] "quantity": {{item_quantity1-add_to_cart}}, [] },
{
"item_id": {{product_id - add to cart js}}, [**********]
"item_name": "{{item_name-add_to_cart}}", [**********]
"price": {{item_price-add_to_cart}}, [**********]
"quantity": {{item_quantity1-add_to_cart}}, [**********]
},
{
"item_id": {{product_id - add to cart js}}, [**********]
"item_name": "{{item_name-add_to_cart}}", [**********]
"price": {{item_price-add_to_cart}}, [**********]
"quantity": {{item_quantity1-add_to_cart}}, [**********]
},
]
} });
As you can see in the marked section [**********] all the variables are same because all the information regarding the product, price, quantity is in the same class or id of the website and provides the same css selector. In cart the drawer number is different so I can use different drawer number to distinguish product names as per the drawer number but here product name for any product is in the h2 class or price is in class= price--item price-regular. As a result when someone adds one product I see the same product name 3 times in the dataLayer and if another product is added, I the new product information 3 times but the previous product information is not in the list. What should I do to encounter this problem?
The result is shown like this,
items: [
{
item_name: "Men's T-shirt for summer",
price: "$45.00",
quantity: "3"**
},
{
item_name: "Men's T-shirt for summer",
price: "$45.00",
quantity: "3"**
},
{
item_name: "Men's T-shirt for summer",
price: "$45.00",
quantity: "3"
},