Miss data send from custom pixel to GTM

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.

Hello minhleos,

Thanks for your query.

To avoid data discrepancies, you have to follow some steps:

  1. GTM server side tracking with first party domain/subdomain for data accuracy. The reason I am talking about that i have hands on experience that sometimes browser/client side fails to track the conversions due to data protection policy. in that case you may not get the real conversions. If someone uses ads blocker, 3rd party cookie blocker and such browser that block the 3rd party cookies/marketing cookies. If browser is unable to track the purchase, you can still get the event from the server. Facebook named in Conversion API (CAPI). Sending data from both browser and server end. I attached a picture where you can see the purchase events did not track from browser/client but from server end.

https://developers.facebook.com/docs/marketing-api/conversions-api/

  1. Purchase event tag is not configured properly. send all event parameters that are recommended by Facebook documentations.

  2. Lack of first party cookies like FBP and FBC to store the cookies in the browser coming from the facebook organic or ads.

The images i attached is from my clients ads platform that i configured with google tag Manager . the result is pretty pleasing.

Feel free to ask if you have any questions.

or

if you want to discuss more, don’t hesitate to send me a PM.

Thanks

@Sam_Mahmud Thanks for replying
You are using Facebook API to send tracking data to server. Can you share your config in GTM, GA4

I already shared the tag configuration. the GA4 purchase tag is the tag for server event and create a browser event tag with facebook pixel template.

1 Like

Thanks @Sam_Mahmud

If you face difficulties, feel free to share.

I would love to see you mark my answer recommended if you get something.

Thanks

Hi @Sam_Mahmud
I try to test with 3rd app, because I checked the configuration on GA4, GTM but there are still cases of missing data, specifically, the number of orders on Shopify is about 1k, there are about 60-70 orders missed on GA4

https://www.littledata.io/shopify/google-analytics
https://analyzify.com/shopify-google-analytics

These are technical process to go through.

To Get purchase event in the GTM you must have an datalayer from where datlayer variables will pick the value to pass with the event.

I have been doing this for my clients for a very long time and tracking the accurate data every day.

I attached a image where we had 10 sales on 7th of october. all the purchase tracked perfectly both from browser and server. event are deduplicated by an event ID.

The way you configured has lack of perfection as these are technical process.

Thanks

@Sam_Mahmud
I don’t think it’s a misconfiguration. Because when I checked about 100 orders, I got the data. But actually, when my order count reached about 1000, I saw data loss.

@Sam_Mahmud

Can you share the code in your customer event for my reference, thanks