Skip to content
ShopPilotStart free trial

How to install Google Tag Manager on Shopify

By 22 min read

A custom pixel in Shopify's Customer events settings, with the Google Tag Manager code pasted into the code editor.

Most tutorials for this still tell you to paste Google Tag Manager into checkout.liquid or into the "Additional scripts" box on the order status page. Both are gone. Shopify made the Additional scripts box view-only on August 28, 2025, and the last stores had to move to the new Thank you and Order status pages by August 26, 2026 (Shopify's upgrade guide).

So if you installed Tag Manager the old way, your theme still loads it on product pages, but it never sees a checkout or a purchase. Your GA4 and Google Ads purchase numbers quietly drop, and nothing in Shopify tells you.

This guide shows where Tag Manager lives on Shopify today, gives you a custom pixel, built on Shopify's own GTM tutorial, that sends page views, product views, add to cart, checkout and purchase into the data layer in GA4's format, and shows the Tag Manager setup that reads it. It also answers the question nobody asks first: whether you need Tag Manager at all. The whole job takes about 30 minutes.

First: do you need Google Tag Manager at all?

Google Tag Manager (GTM) is a free Google tool that loads your tracking codes (called tags) on your site and decides when each one fires. You manage the tags in GTM instead of editing your store's code every time.

The honest answer for most small stores: no.

My own store runs on Shopify's Google & YouTube app. When I open Settings, then Customer events, I see three pixels: Facebook & Instagram, Google & YouTube, and Klaviyo. All three come from apps. There is no Tag Manager. GA4, Google Ads conversions and Merchant Center all work through the app, including checkout and purchases, and I never wrote a line of code.

Google itself says the app is the recommended way, and that running Google tags inside a Shopify custom pixel "is not a supported implementation". If something breaks there, Google support will not help you (Google's page on custom pixel limits).

Your situationUse thisWhy
You want GA4, Google Ads conversions and Shopping adsThe Google & YouTube appIt sends checkout and purchase events for you, and it is the setup Google supports.
You also run Meta, TikTok or Pinterest adsEach platform's own Shopify appEach app installs its own pixel in Customer events. No code.
You need a tag that has no Shopify app, or custom GA4 events and parametersGTM in a custom pixel (this guide)It is the only place your own code can see checkout and purchase events.
An agency or freelancer already manages everything in GTMGTM in a custom pixel (this guide)Keep one system, but move the checkout part out of the theme.
You need heatmaps, chat widgets or A/B testsThat tool's Shopify app, or GTM in the themeThese tools need the real page, which a custom pixel cannot touch.

If the first two rows describe you, install the app and stop reading. Seriously. If not, keep going.

The two places GTM can live on Shopify

There are only two places your own code can run, and they see different things:

GTM in the theme (theme.liquid)GTM in a custom pixel (Customer events)
Product, collection and cart pagesYesYes
Checkout pagesNoYes
Thank you page (purchase)NoYes
Can read and change the real pageYesNo, it runs in a sandbox
GTM preview mode worksYesNo
Respects the visitor's cookie choice automaticallyNo, you build it yourselfYes, through the pixel's Permission setting

The custom pixel is the only one that sees checkout, so that is where your GA4 and ads tags belong. The theme is only for tags that need the actual page, like a heatmap, and those should never send purchase data.

Never send the same GA4 events from both places, and never send purchases from GTM if the Google & YouTube app already sends them. You will count every order twice.

What the sandbox means in practice

A custom pixel is a piece of JavaScript you add in Settings, then Customer events. Shopify runs it on every page, including checkout and the Thank you page (Shopify's pixel overview).

The catch is the sandbox. Shopify runs your pixel inside an isolated frame, separate from the real page. When I created my pixel, the first thing on the screen was a blue notice: "For enhanced security and stability, pixel access is sandboxed."

Here is what that means for you:

  • Your code cannot see the page. It cannot read buttons, forms or text, and it cannot add anything visible. You only get the events Shopify hands you.
  • GTM preview mode does not work. Google lists Preview mode and Tag Assistant among the features that may not work in the sandbox (Google's list), and Shopify says Tag Assistant's Troubleshoot tag feature doesn't detect Google tags in a custom pixel at all (Shopify's GTM tutorial). This is the number one complaint on the Shopify Community forum: people install GTM in a pixel, open preview, nothing connects, and they assume it's broken. The usual answer there is that the tags still fire. Preview just can't see inside the frame.
  • Tags and triggers that need the page will not work. Click triggers, scroll tracking, element visibility triggers, heatmaps, chat widgets, cookie banners and anything that reads or changes the page's HTML. Google says many trigger types, many built-in variables and tags that modify the page may not work (Google's list). Shopify lists outbound link clicks, scrolling and heatmaps as things pixels cannot capture (the sandbox limits).
  • Page addresses can be wrong. The address the sandbox reports may not be the real page address, and GA4 can end up with sandbox addresses in its page reports. Shopify's fix is to take the address from the page_viewed event (the sandbox limits) and to turn off GA4's automatic page views (Shopify's GTM tutorial). The code below sends the real address with every event.
  • Some Google features may break. For GA4, Google lists automatically collected events, enhanced measurement (scrolls, outbound clicks, site search), creating or modifying events in GA4, enhanced conversions, cross-domain measurement and consent mode URL passthrough. For Google Ads, it adds conversions set up with a URL, conversion troubleshooting and phone call conversions (Google's list).

What does work, according to Google's own page: core signals like page views, purchase conversions and their values. In practice that means page views, product views, add to cart, checkout and purchases, sent by the GA4 and Google Ads tags in your container.

Step 1: Find your GTM container ID

You need a container, which is GTM's word for the box that holds all the tags for one website. Its ID looks like GTM-XXXXXXX.

  1. Open Tag Manager and open your container.
  2. Click Admin, then Install Google Tag Manager (Shopify's GTM tutorial). Clicking your container ID at the top of the workspace opens the same dialog (Google's install steps).
  3. Note your container ID, the GTM- code shown in the dialog. That is all you need from here.

You don't need to copy Google's code block. Shopify's pixels only accept JavaScript, not HTML (Shopify's code rules), and Google now shows a newer snippet that loads Tag Manager through a <script src="..."> tag (Google's install steps). Strip the tags from that one and the part that loads your container is gone. The code below uses Google's previous snippet instead, the one Shopify's tutorial is built on, which is plain JavaScript. Google says containers installed with it keep working (same page). You only replace GTM-XXXXXXX with your own container ID.

Step 2: Create the custom pixel

  1. In Shopify, click Settings (bottom left), then Customer events.
  2. Click Add custom pixel (top right).
  3. A box warns you that apps are the safer option. Under Pixel name, type a name, for example "Google Tag Manager". The limit is 30 characters.
  4. Click Add pixel.

You land on the pixel's page. It has two parts: Customer privacy and Code.

The new custom pixel with its sandbox notice and the Permission settings.

Customer privacy has two settings. These decide when Shopify lets your pixel run, based on what the visitor chose in your cookie banner.

Permission:

Option on screenWhat it doesMy choice
Required, with Marketing, Analytics and Preferences checkboxes"The pixel will collect data when permission is granted for selected purposes." Marketing and Analytics are ticked by default.Keep this, with Marketing and Analytics ticked. GA4 is analytics and Google Ads is marketing.
Not required"The pixel will always run."Don't. You would track people who said no to tracking.

Data sale:

Option on screenWhat it doesMy choice
Data collected qualifies as data sale (the default)"The pixel will not collect data when the customer opts out of their data being sold."Keep this if you run ad tags, which is the usual case.
Data collected qualifies as data sale and supports limited data use"The pixel will collect data when the customer opts out of their data being sold."Only if every tag in your container supports limited data use. Most small stores can ignore it.
Data collected does not qualify as data sale"The pixel will collect data when the customers opts out of their data being sold."Only for pure analytics with no ad tags.

I'm not a lawyer, and privacy rules differ by country. The defaults are the cautious choice, and the cautious choice is what I'd pick.

Step 3: Paste the code and save

The Code box starts with a short placeholder. Delete it all and paste the code below. Paste, don't type: the editor adds closing brackets automatically while you type, and you end up with extra ones.

// 1. Consent: Shopify only runs this pixel once the visitor allows it
window.dataLayer = window.dataLayer || [];
function gtag() { window.dataLayer.push(arguments); }
gtag('consent', 'update', {
  ad_storage: 'granted',
  analytics_storage: 'granted',
  ad_user_data: 'granted',
  ad_personalization: 'granted'
});

// 2. Load Google Tag Manager (your container ID goes at the end)
(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');

// 3. Helpers
function pageData(event) {
  return {
    page_location: event.context.window.location.href,
    page_referrer: event.context.document.referrer,
    page_title: event.context.document.title
  };
}

function pushEcommerce(eventName, event, ecommerce) {
  window.dataLayer.push({ ecommerce: null });
  window.dataLayer.push({ event: eventName, ...pageData(event), ecommerce: ecommerce });
}

function unitPrice(total, quantity) {
  return Math.round((total / quantity) * 100) / 100;
}

function toItem(variant, quantity, price) {
  return {
    item_id: variant?.sku || variant?.id,
    item_name: variant?.product?.title,
    item_variant: variant?.title,
    item_brand: variant?.product?.vendor,
    price: price,
    quantity: quantity
  };
}

function checkoutData(checkout) {
  return {
    currency: checkout.currencyCode || checkout.subtotalPrice?.currencyCode,
    value: checkout.subtotalPrice?.amount,
    items: checkout.lineItems.map((line) =>
      toItem(line.variant, line.quantity, unitPrice(line.finalLinePrice.amount, line.quantity))
    )
  };
}

// 4. Page views, with the real page address instead of the sandbox's
analytics.subscribe('page_viewed', (event) => {
  window.dataLayer.push({ event: 'page_viewed', ...pageData(event) });
});

// 5. Product page
analytics.subscribe('product_viewed', (event) => {
  const variant = event.data.productVariant;
  pushEcommerce('view_item', event, {
    currency: variant.price.currencyCode,
    value: variant.price.amount,
    items: [toItem(variant, 1, variant.price.amount)]
  });
});

// 6. Add to cart
analytics.subscribe('product_added_to_cart', (event) => {
  const line = event.data.cartLine;
  if (!line) return;
  const total = line.cost.totalAmount;
  pushEcommerce('add_to_cart', event, {
    currency: total.currencyCode,
    value: total.amount,
    items: [toItem(line.merchandise, line.quantity, unitPrice(total.amount, line.quantity))]
  });
});

// 7. Checkout started
analytics.subscribe('checkout_started', (event) => {
  pushEcommerce('begin_checkout', event, checkoutData(event.data.checkout));
});

// 8. Purchase
analytics.subscribe('checkout_completed', (event) => {
  const checkout = event.data.checkout;
  const code = checkout.discountApplications.find((d) => d.type === 'DISCOUNT_CODE');
  pushEcommerce('purchase', event, {
    ...checkoutData(checkout),
    transaction_id: checkout.order?.id,
    tax: checkout.totalTax?.amount,
    shipping: checkout.shippingLine?.price?.amount,
    coupon: code?.title
  });
});

Replace GTM-XXXXXXX with your container ID, then click Save in the bar at the top. You get a "Changes saved" message.

Don't click Connect yet. Until you connect the pixel, it doesn't run anywhere, so you can set up Tag Manager first without sending half-finished data. Notice also that Connect only becomes clickable after you save code: before that, the page says "To connect this pixel, edit the code below and save your changes."

What each part of the code does

This is the setup from Shopify's GTM tutorial, with one change: Shopify's example pushes its own event names and flat fields, and this version pushes them in Google's GA4 ecommerce format for Tag Manager, so one GA4 tag can send them all. Every field it reads comes from Shopify's Web Pixels API. Inside a custom pixel, Shopify gives you a ready-made object called analytics. analytics.subscribe('event name', ...) means "when this happens, run this code".

PartWhat it doesWhy it's written this way
1. ConsentCreates the data layer (a list GTM reads events from) and tells Google's tags that the visitor agreed to analytics and ads.Shopify's tutorial does the same with gtag('consent', 'update', ...), and Shopify's code rules name it as the way to pass consent to Google (Shopify's code rules). It is safe because, with the Permission setting from Step 2, Shopify only runs the pixel once the visitor allows marketing and analytics. If you untick Marketing there, set the three ad_ lines to 'denied'.
2. Load Google Tag ManagerLoads your container inside the sandbox.It is Google's previous container snippet without the <script> tags, because pixels only accept JavaScript.
3. pageDataReads the real page address, the previous page and the title.The sandbox may report its own address, so the code takes them from the event, as Shopify recommends.
3. pushEcommerceClears the previous ecommerce data, then sends the new event with the page data.Google's examples push { ecommerce: null } first, so one event's products never leak into the next.
3. unitPriceDivides a line's total by its quantity.GA4 wants price per unit, and the discounted price when a discount applies (GA4 event reference). Shopify gives the cart and checkout lines as totals after discounts.
3. toItemTurns a Shopify variant into one GA4 item: ID, name, variant, brand, price, quantity.GA4 needs item_id or item_name for every item. I use the SKU as the ID and fall back to Shopify's variant ID when a product has no SKU.
3. checkoutDataBuilds currency, value and the item list from a checkout.The checkout start and the purchase use the same data, so it's written once. The value is Shopify's subtotalPrice, the price before shipping and taxes, because GA4 wants the sum of the items without shipping and tax. If your prices include tax, as in most of Europe, the subtotal includes it too.
4. page_viewedPushes page_viewed with the real page address and title.This is Shopify's own "cleaner page URLs" code from the tutorial. Tag Manager turns it into GA4's page_view in Step 4.
5. product_viewedSends view_item when someone opens a product page.Shopify's product_viewed becomes GA4's view_item.
6. product_added_to_cartSends add_to_cart with the product and quantity.Shopify's documentation allows the cart line to be empty, so the code stops if there is none.
7. checkout_startedSends begin_checkout with everything in the cart.This is the first event the theme never sees. Shopify fires it every time a customer enters checkout, so one shopper can send it more than once.
8. checkout_completedSends purchase with the order ID, value, tax, shipping and discount code.The order ID becomes transaction_id, which GA4 uses to drop duplicate purchases (Google's help on transaction IDs). Shopify fires this event once per checkout, usually on the Thank you page, or on the first upsell page if you use post-purchase offers (Shopify's reference).

Why rename Shopify's events to GA4's names? Because GA4 recognizes view_item, add_to_cart, begin_checkout and purchase and fills its ecommerce reports with them. If you send checkout_completed, GA4 treats it as some random custom event and your revenue reports stay empty. The names below are the same pairs Shopify lists in its tutorial.

Shopify's event names, and the fields each one carries, are in its standard events reference. Here is the mapping in one place:

Shopify eventWhen it firesGA4 event that reaches GA4
page_viewedEvery storefront page, checkout page, Thank you page and Order status pagepage_view (the pixel pushes page_viewed, and the tag in Step 4 sends it as page_view)
product_viewedA product page opensview_item
product_added_to_cartA product goes into the cartadd_to_cart
checkout_startedThe customer enters checkoutbegin_checkout
checkout_completedThe order is placedpurchase

Step 4: Set up GA4 in Tag Manager

Now GTM receives the events. It needs tags that send them on to GA4. You need your GA4 Measurement ID, which looks like G-XXXXXXXXXX (in GA4: Admin, Data streams, then your web stream).

First, the page data. Every event the pixel pushes carries the real page address, the previous page and the title. GA4 should use those instead of the sandbox's own address.

  1. Click Variables. Under User-Defined Variables, click New, choose Data Layer Variable, and set Data Layer Variable Name to page_location. Save it as "DLV page_location". Do the same for page_referrer and page_title.
  2. Click New again and choose Google Tag: Event Settings. Add three rows: page_location = {{DLV page_location}}, page_referrer = {{DLV page_referrer}} and page_title = {{DLV page_title}}. Save it as "Page data". You will attach it to both GA4 tags below.

Then the page views. Shopify recommends turning off GA4's automatic page views in a custom pixel, because they can carry the sandbox address (Shopify's GTM tutorial), and sending your own from the page_viewed event.

  1. Click Triggers, then New. Choose Custom Event and set Event name to page_viewed (Google's trigger help). Save it as "CE page_viewed".
  2. Click Tags, then New, then Tag Configuration. Choose Google Analytics, then Google Tag. Enter your Measurement ID. Under Configuration settings, add the parameter send_page_view with the value false, which is Google's switch for automatic page views (Google's page view guide). Use the Initialization - All Pages trigger.
  3. Add another tag: Google Analytics, then Google Analytics: GA4 Event. Enter your Measurement ID, set Event Name to page_view, and under Event Parameters choose "Page data" as the Event Settings Variable. Trigger: "CE page_viewed".

Then the ecommerce events. One tag handles all four.

  1. Click Triggers, then New, choose Custom Event. Set Event name to view_item|add_to_cart|begin_checkout|purchase and tick use regex matching. Save it as "CE ecommerce".
  2. Click Tags, then New. Choose Google Analytics: GA4 Event.
  3. Enter your Measurement ID. Set Event Name to {{Event}}. That built-in variable copies whatever event name the pixel sent.
  4. Under Event Parameters, choose "Page data" as the Event Settings Variable, so checkout and purchase events also carry the real page address.
  5. Open More Settings, then Ecommerce. Tick Send Ecommerce data and set Data Source to Data Layer (Google's GTM ecommerce steps).
  6. Trigger: "CE ecommerce". Save.

That's it for GA4. The tag reads the currency, value, items and transaction ID straight from the ecommerce object the pixel pushed, so you don't need a variable for each field.

For Google Ads conversions, add a Google Ads, then Google Ads Conversion Tracking tag that fires on a Custom Event trigger for purchase. Fill in the Conversion ID and Conversion Label from your Google Ads conversion action, and use Data Layer Variables for ecommerce.value, ecommerce.transaction_id and ecommerce.currency in the value, transaction ID and currency fields (Google's steps). If Google Ads already imports purchases from GA4 or from the Google & YouTube app, skip this, or you will count each sale twice.

When everything is ready, Submit and publish the container. Then go back to Shopify and click Connect on your pixel.

Step 5: Test it without preview mode

Since GTM's preview mode can't see inside the sandbox, test with Shopify's tool and GA4's live reports instead.

  1. In Customer events, check that your pixel shows Connected in the Status column. Shopify's test only works on a connected pixel.
  2. Open the pixel and click Test. Shopify opens your store in a new tab with the Pixel helper (Shopify's testing steps).
  3. If it says Pixel is awaiting consent, accept your cookie banner or click Give consent to continue test.
  4. View a product, add it to the cart, and go to checkout. On each page, the event should appear under Events received with a green dot. A red dot means your code threw an error for that event: expand it to see the error. The list clears on every new page, so check it before you click on.
  5. In GA4, open Reports, then Realtime. Within a minute or so you should see page_view, view_item, add_to_cart and begin_checkout.
  6. For purchase, place a test order (Shopify's test order options), or a real order with a cheap product or a 100% discount code that you cancel afterwards. Check that it appears in GA4 with the right value.

When you change the pixel's code, save it, close the Pixel helper tab and click Test again: Shopify doesn't let you edit while the helper runs.

If the Pixel helper shows events but GA4 shows nothing, the problem is in GTM: usually an unpublished container, a typo in the trigger's event name, or the wrong Measurement ID. Container changes only reach the pixel once you publish them, which store owners on the Shopify Community forum point out again and again. Advice from the same forum for seeing what the pixel pushes, since preview mode can't: the DataLayer Checker Plus Chrome extension, or your browser's Network tab filtered for collect?v=2, which shows each hit GA4 receives.

When GTM in the theme still makes sense

The pixel handles all your Google and ads tracking. You only need GTM in theme.liquid for tools that need the real page: heatmaps, session recordings, chat widgets, some A/B testing tools. Most of these have a Shopify app now, and I'd use the app.

If you do put GTM in the theme, open Online Store, click the three dots next to your theme, choose Edit code (Shopify's steps), open theme.liquid, and paste the code from Google's Install Google Tag Manager dialog as high in the <head> as possible. If your dialog still shows the older two-part version, the second <noscript> part goes right after <body> (Google's install guide). Use a separate container from the one in your pixel. Then nobody can accidentally fire a purchase tag from the theme, where it would never see a purchase anyway.

Two things the theme version does not do: it doesn't respect the visitor's cookie choice by itself, and it runs on storefront pages only.

Frequently asked questions

Why doesn't GTM preview mode connect to my Shopify store?

Because the custom pixel runs inside an isolated frame, and Tag Assistant can't see into it. Google lists Preview mode among the features that may not work there. Store owners on the Shopify Community forum report that the tags still fire anyway, so test with Shopify's Pixel helper and GA4's Realtime report instead.

Can I still use checkout.liquid or the Additional scripts box?

No. The Additional scripts box has been view-only since August 28, 2025, and all stores had to move to the new Thank you and Order status pages by August 26, 2026 (Shopify's upgrade guide). Anything that tracked checkout or purchases from there has stopped. A custom pixel or an app is the replacement.

Do I need Shopify Plus for GTM in a custom pixel?

No. Shopify says Tag Manager works on every store with custom pixels, and custom pixels live in Settings, then Customer events, not in a Plus-only checkout file. That is the whole point of the new setup: the old checkout.liquid route was the Plus-only one.

Why does GA4 show every purchase twice?

Almost always because two things send it: the Google & YouTube app and your GTM pixel, or GTM in the theme and in the pixel. Pick one source for GA4 purchases and turn the other off. Sending the order ID as transaction_id also helps, because GA4 drops repeated purchases that carry the same transaction ID.

Can I use the GTM data layer from my theme in the pixel?

No. The pixel has its own separate data layer inside the sandbox and can't read the page's. If you need a custom event from the theme, Shopify's tutorial shows how to publish it with Shopify.analytics.publish() in the theme and subscribe to it in the pixel.

Your first audit is five minutes away.

Start free trial

14 days free$19.95/monthCancel anytime