I’m using an Online Store to sell products and I want to send data to GTM for e-commerce tracking purposes. I’m using a custom pixel to send the data as instructed by Shopify.
(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-xxxxxxxx');
// Check if analytics is defined
if (typeof analytics !== 'undefined' && analytics.subscribe) {
console.log('here 3', typeof dataLayer)
analytics.subscribe("page_viewed", (event) => {
console.log('page_viewed dataLayer', window.dataLayer)
const ecommerceEvent = {
event: "shopify_page_view",
page_url: event.context.document.location.href
};
window.dataLayer.push(ecommerceEvent);
});
analytics.subscribe("checkout_completed", (event) => {
console.log('checkout_completed 1')
console.log('analytics', analytics)
console.log('dataLayer', window.dataLayer)
const checkoutData = event.data?.checkout;
if (checkoutData) {
const items = checkoutData.lineItems.map(lineItem => ({
item_id: lineItem.variant.product.id,
item_name: lineItem.variant.product.title,
affiliation: lineItem.variant.product.vendor,
item_category: lineItem.variant.product.type,
price: lineItem.variant.price.amount,
quantity: lineItem.quantity
}));
const ecommerceEvent = {
event: "purchase",
ecommerce: {
currency: checkoutData.totalPrice.currencyCode,
value: checkoutData.totalPrice.amount,
tax: checkoutData.totalTax?.amount,
shipping: checkoutData.shippingLine?.price?.amount,
transaction_id: checkoutData.order?.id,
items: items,
customer: {
email: checkoutData.email,
phone_number: checkoutData.billingAddress.phone || null,
address: {
first_name: checkoutData.billingAddress.firstName,
last_name: checkoutData.billingAddress.lastName,
street: checkoutData.billingAddress.address1,
city: checkoutData.billingAddress.city,
region: checkoutData.billingAddress.provinceCode,
country: checkoutData.billingAddress.country,
postal_code: checkoutData.billingAddress.zip
}
}
},
page_url: event.context.document.location.href,
customer_type: checkoutData.order?.customer?.isFirstOrder,
user_id: checkoutData.order?.id,
localization_country: checkoutData.localization?.country?.isoCode,
localization_language: checkoutData.localization?.language?.isoCode,
gateway: checkoutData.transactions?.gateway,
payment_method_name: checkoutData.transactions?.paymentMethod?.name,
payment_method_type: checkoutData.transactions?.paymentMethod?.type
};
console.log('checkout_completed 2')
window.dataLayer.push(ecommerceEvent);
}
});
} else {
console.error("Analytics is not defined or does not support subscriptions.");
}
However, I’ve noticed a discrepancy between the number of orders and the number of purchase events sent to GTM. The number of events on GTM is consistently lower than the number of orders. Has anyone encountered a similar issue or know how to fix this? Could you please help me resolve this problem? Thank you.









