mightyitguy
GTM

How to Track Purchase Events after Shopify Checkout Extensibility Update – GTM

How to Track Purchase Events after Shopify Checkout Extensibility Update – GTM

Updated on 23 November 2026: the script below is revised against Shopify's current Web Pixels API. It fixes the state, tax and coupon fields, removes two fields Shopify never sends (customer tags and total spent), and adds a GA4 ecommerce object and an eventId for Meta and TikTok deduplication. Existing field names are unchanged, so current GTM setups keep working.

On 13th of August 2024 Shopify deprecated checkout.liquid and the method to add “Additional Scripts” to the checkout page directly. Since then a lot of Shopify shop owners were struggling to track checkout related events without using an app. And there are not many guides out there and Shopify hasn’t provided a proper guide at all. They have only given a standard event parameter breakdown. Not only that this removed Google Tag Manager or any code snippet which was added to the checkout pages. So nothing was being tracked in the checkout pages.

This guide will help you to send purchase events data with all the available data in Shopify using Google Tag Manager to any platform you desire.

Step 1 – Find where to add?

Go to Shopify Admin panel > Settings > Customer Events

Shopify admin Settings menu with Customer events

Step 2 – Add custom pixel

Press “Add custom pixel”, give it a recognizable name (your GTM container ID works well), and paste the code below. Change GTM-XXXXXXX on the first line to your own GTM ID. Set DEBUG to true while you test, and back to false when you're done.

// ==== Settings ====
const GTM_ID = 'GTM-XXXXXXX'; // <-- change to your GTM container ID
const DEBUG = false;          // true = log every step to the console while testing

// Load Google Tag Manager inside the custom pixel sandbox
(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_ID);

window.dataLayer = window.dataLayer || [];
const log = (...args) => DEBUG && console.log('[cgtag]', ...args);
const num = (money) => Number(money?.amount ?? 0); // Shopify money fields are { amount, currencyCode }

// Fires once per checkout: on the Thank you page, or on the first upsell page if you use post-purchase offers
analytics.subscribe('checkout_completed', (event) => {
  const c = event?.data?.checkout || {};
  const bill = c.billingAddress || {};
  const lines = c.lineItems || [];
  const codes = (c.discountApplications || [])
    .filter((d) => d.type === 'DISCOUNT_CODE')
    .map((d) => d.title)
    .join(',');
  log('checkout_completed', event);

  const items = lines.map((line) => ({
    item_id: line?.variant?.product?.id || '',
    item_name: line?.variant?.product?.title || line?.title || '',
    item_variant: line?.variant?.title || '',
    item_sku: line?.variant?.sku || '',
    item_brand: line?.variant?.product?.vendor || '',
    item_category: line?.variant?.product?.type || '',
    price: num(line?.variant?.price),
    quantity: line?.quantity || 0,
    discount: (line?.discountAllocations || []).reduce((sum, a) => sum + num(a.amount), 0),
  }));

  const dataLayerObject = {
    event: 'orderPlaced',
    eventId: event?.id || '', // Shopify's ID for this event: use it as event_id for Meta/TikTok deduplication

    // Flat fields (same names as the original version of this script)
    transactionId: c.order?.id || '',
    transactionCurrency: c.currencyCode || '',
    transactionDate: event?.timestamp || '',
    transactionOrigin: bill.country || 'Online',
    transactionEmail: c.email || '',
    transactionPhone: c.phone || bill.phone || '',
    transactionUserName: `${bill.firstName || ''} ${bill.lastName || ''}`.trim(),
    transactionUserId: c.order?.customer?.id || '',
    transactionUserFirstOrder: c.order?.customer?.isFirstOrder ?? '',
    transactionPaymentType: c.transactions?.[0]?.paymentMethod?.name || c.transactions?.[0]?.gateway || '',
    transactionCoupon: codes,
    transactionShippingMethods: (c.delivery?.selectedDeliveryOptions || []).map((o) => o.title).filter(Boolean).join(', '),
    transactionCity: bill.city || '',
    transactionState: bill.province || '',
    transactionStateCode: bill.provinceCode || '',
    transactionPostalCode: bill.zip || '',
    transactionCountry: bill.country || '',
    transactionCountryCode: bill.countryCode || '',
    transactionSubTotal: num(c.subtotalPrice),
    transactionTotal: num(c.totalPrice),
    transactionTotalTax: num(c.totalTax),
    transactionShipping: num(c.shippingLine?.price),
    transactionDiscount: num(c.discountsAmount),
    transactionItems: items,

    // GA4 format: lets a GA4 "purchase" tag use "Send Ecommerce data" with no extra variables
    ecommerce: {
      transaction_id: c.order?.id || '',
      value: num(c.totalPrice),
      tax: num(c.totalTax),
      shipping: num(c.shippingLine?.price),
      currency: c.currencyCode || '',
      coupon: codes,
      items,
    },
  };

  try {
    window.dataLayer.push({ ecommerce: null }); // clear any previous ecommerce object first
    window.dataLayer.push(dataLayerObject);
    log('orderPlaced pushed', dataLayerObject);
  } catch (error) {
    console.error('[cgtag] dataLayer push failed:', error);
  }
});

Each purchase pushes one orderPlaced event to the dataLayer with:

  • transaction… fields (transactionId, transactionTotal, transactionTotalTax, transactionCoupon and so on) for any tag you build with dataLayer variables.
  • ecommerce in GA4's format, so a GA4 Event tag named purchase can simply tick “Send Ecommerce data”.
  • eventId, Shopify's ID for the event. Send it as event_id from both your browser and server tags so Meta and TikTok can deduplicate them.

Customer tags and lifetime spend are not available to pixels at all, so they are not in the event. transactionUserFirstOrder tells you whether it was the customer's first order instead.

Step 3 – Save and connect

Set the Permission. “Not required” runs the pixel for every visitor, without waiting for cookie consent. That's simplest, but if you sell to the EU, UK or other regions that need consent, choose “Required” and tick Analytics and Marketing instead, so the pixel only runs once the visitor agrees.

Custom pixel customer privacy permission setting

Press “Save” and “Connect”. Thats it! 

Custom pixel code editor with Save and Connect buttons

This code snippet will start sending datalayer events for purchases again. However, please note the Google Tag Manager assistant does not get connected in checkout pages at the moment, even though the datalayer events are being sent to the live site, it’s being sent to a sandbox environment in the front end. As of now Google Tag Manager does not have the capability to access the code snippets in the checkout pages. 

So don’t assume Google Tag Assistant breaking on checkout pages, is related to GTM code not being present on the checkout pages. Add the code as I mentioned and do a test purchase to see it working and you receiving data in GA4, Meta etc.

How to test if it’s working

With DEBUG = true, a test purchase logs [cgtag] orderPlaced pushed in your browser console. The event fires once per checkout on the Thank you page, or on the first upsell page if you use post-purchase offers.

Browser console showing the orderPlaced dataLayer push

Now you can use the purchase event data, under the datalayer event name “orderPlaced” to send data to any platform you want using Google Tag Manager.

If you get stuck, reach out to me on LinkedIn and I'm happy to help troubleshoot.

If you are so eager to see the actually datalayer events in the new shopify checkout pages, here’s how to do that.

After doing a purchase, in the console you should see something called “top” as a dropdown. Open the dropdown and find an option called “web-pixel-sandbox-CUSTOM-xxxxxxxxxxxxx” and click on that(if you have multiple other custom pixels added to Shopify backend, you will be see one for each of those as well).

Browser console context menu with the web-pixel-sandbox entry

After that if you type “dataLayer” as usual in the console, it will show you the datalayer. 

dataLayer contents in the custom pixel sandbox console

#custom events#gtm#shopify

More guides

All guides →