DataLayer throwing undefined error on checkout pages even after adding gtm code in custom pixel

Hi, I need some help.

I’m unable to track my website pages after Add to Cart. From Checkout pages, GTM is not able to track any page, even purchase. To solve this, I pushed the GTM code in Custom Pixel with Data Layers for relevant events that I want to track but the code is still not firing. Please help.

Here is the website: https://yourcleanlab.com/

I added this code:

window.dataLayer = window.dataLayer || ;

function gtag(){dataLayer.push(arguments);}

(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’);

analytics.subscribe(“checkout_started”, (event) => {

dataLayer.push({ ecommerce: null });

const items = event.data?.checkout?.lineItems?.map((item) => {

return {

item_id: item.variant.product.id,

item_name: item.variant.product.title,

price: item.variant.price.amount,

quantity: item.quantity

}

});

dataLayer.push({

event: “begin_checkout”,

url: event.context.document.location.href,

ecommerce: {

currency: event.data?.checkout?.currencyCode,

value: event.data?.checkout?.subtotalPrice?.amount,

items: items

}

});

});


Still not working. Sharing screenshots below for better understanding.

While checking through console, values are passed, but Data.Layer is still showing error. Check the screenshot below:

Hi,

Thanks for your query.

your code which is not appropriate for shopify custom pixel to push all ecommerce events like view item, add to cart, checkout or purchase event with user provided data like email, phone, first name, last name, country, city and so on

The code you’ve written is correct in structure, but it’s likely being injected into the wrong execution context Needs to add more javascript elements to push datalayer event with GA4 schema datalayer.

Your current GTM code is the standard web code. For Shopify Checkout, we need to be more explicit.

we have developed a strong datalayer to track events for any platform with GA4 schema.