0

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"
  },
3
  • I don't know what is the mapping for those variables {{product_id - add to cart js}} but have you tried using the index to control what you want to pass in? Commented Nov 14, 2023 at 23:40
  • Actually I have no knowledge about JavaScript and just used codes that was given by a YouTube instructor. I do not know what mapping is and how to create or use indexing. Can you please explain if you have the time to? Commented Nov 15, 2023 at 5:20
  • Can you tell me that whether it is required to create add to cart products array in GTM as when the data is sent to GA4 and it is counted as accumulation of total events, so do I really required to do this? if not then I think I am stressing on this too much! Commented Nov 16, 2023 at 3:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.