Personalized checkout and custom promotions with Shopify Scripts
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
Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024The Hydrogen Visual Editor is now available to merchants in Shopify Editions | Summer '...
By JasonH Sep 2, 2024Note: Customizing your CSS requires some familiarity with CSS and HTML. Before you cust...
By JasonH Aug 12, 2024