Purchase event => product data => GTM datalayer => PUSH

Purchase event => product data => GTM datalayer => PUSH

Srdjan123
Shopify Partner
1 0 0

Hello,

I am trying to push product data in the GTM data layer using this script in settings =>customer events => pixels

Shopify Help Center only provides a solution for pushing the value of transactions but does not offer a solution for pushing the individual products(name, price, amount) to be added as an array of objects. 

See Shopify solution:

 

 

 

analytics.subscribe("checkout_completed", (event) => {
  window.dataLayer.push({
    event: "checkout_completed",
    order_id: event.data.checkout.order.id,
    currency: event.data.checkout.currencyCode,
    subtotal: event.data.checkout.subtotalPrice.amount,
    shipping: event.data.checkout.shippingLine.price.amount,
    value: event.data.checkout.totalPrice.amount,
    tax: event.data.checkout.totalTax.amount,
  });
});

 

 

 


So, based on the Shopify solution I have added a loop which goes through lineItems, pushes each item data into array of objects and then pushes everything to datalayer

 

 

 

analytics.subscribe("checkout_completed", (event) => {
  // Define an array to store product data
  var products = [];

  // Iterate through each line item in the checkout
  event.data.checkout.lineItems.forEach((lineItem) => {
    // Push product data into the products array
    products.push({
      id: lineItem.id,
      name: lineItem.title,
      category: lineItem.productType,
      variant: lineItem.variantTitle,
      price: lineItem.price.amount,
      quantity: lineItem.quantity
    });
  });

  // Push data into the data layer
  window.dataLayer.push({
    event: "checkout_completed",
    order_id: event.data.checkout.order.id,
    currency: event.data.checkout.currencyCode,
    subtotal: event.data.checkout.subtotalPrice.amount,
    shipping: event.data.checkout.shippingLine.price.amount,
    value: event.data.checkout.totalPrice.amount,
    tax: event.data.checkout.totalTax.amount,
    items: products // Add the products array
  });
});

 

 

 


Does anyone know what I am doing wrong?

Thanks


Replies 0 (0)