Hi,
I am using custom pixels to track checkout-related GTM events. This is the code for one of the events.
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');
window.dataLayer = window.dataLayer || [];
analytics.subscribe("checkout_shipping_info_submitted", event => {
window.dataLayer.push({ ecommerce: null })
window.dataLayer.push({
event: 'add_shipping_info',
currency: event.data.checkout.currencyCode,
value: event.data.checkout.subtotalPrice.amount,
items: event.data.checkout.lineItems.map((product) => {
// details of each product in cart
return {
item_id: product.id,
item_name: product.title,
price: product.variant.price.amount,
quantity: product.quantity
}
})
});
});
As you can see I have added the GTM installation code, however when I am on the checkout page and I enter dataLayer into the console, it says the dataLayer is undefined. Does anyone know why this is happening? Am I supposed to add the GTM installation code somewhere else?
Thank you!