Google Tag Manager

How to Install Google Tag Manager on Shopify

Install Google Tag Manager on Shopify by editing theme.liquid, and understand the checkout limitation: storefront pages are easy, checkout needs a custom pixel.

9 min read
Quick answer

Add GTM to your Shopify storefront by editing theme code: go to Online Store > Themes > three-dot menu > Edit code, open theme.liquid, paste GTM's first snippet right after the opening head tag and the second snippet right after the opening body tag, then save. That covers every storefront page but NOT checkout. Checkout pages only run tracking through a custom pixel (Settings > Customer events), which is a more advanced setup. If your main goal is GA4 e-commerce data, the Google & YouTube app is the simpler route; GTM earns its place for extra storefront tracking.

Installing Google Tag Manager on Shopify is a 10-minute job with one honest complication: your install covers the storefront, not checkout. Understanding that boundary up front will save you from the classic Shopify tracking surprise, where everything works until the moment money changes hands.

This is the Shopify-specific version of our general GTM installation guide. New to GTM? Start with the Tag Manager beginner’s guide to learn what tags, triggers, and variables do.

Before you start

You’ll need a GTM container ID (the GTM-XXXXXXX code from the top bar at tagmanager.google.com; the GTM setup guide covers creating a container) and access to your Shopify admin with permission to edit themes.

First, decide if GTM is the right tool for the job

One question sorts this out: what do you want GTM for?

  • “I want GA4 tracking my store, including purchases.” Use Shopify’s Google & YouTube app instead; it installs GA4 everywhere including checkout and sends full e-commerce events with zero code. Here’s the GA4 on Shopify guide. You don’t need GTM for this.
  • “I want extra tracking on storefront pages” (scroll depth, outbound clicks, third-party tags, testing tools). GTM is the right tool, and this guide is for you.
  • “I want full e-commerce tracking through GTM, checkout included.” Possible, but it’s an advanced project involving a custom pixel and data layer work. The last section explains the landscape honestly before you commit.

Install GTM in your theme

1

Copy the two GTM snippets

At tagmanager.google.com, click your container ID in the top bar (or go to Admin > Install Google Tag Manager). You’ll see two snippets: the first belongs in the <head>, the second right after the opening <body> tag.

They look like this, with GTM-XXXXXXX standing in for your real ID (copy yours from GTM, not from here):

Snippet 1: goes in <head>
<!-- Google Tag Manager -->
<script>(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');</script>
<!-- End Google Tag Manager -->
Snippet 2: goes after opening <body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
2

Open your theme's code editor

In your Shopify admin, go to Online Store > Themes. On your current theme, click the three-dot menu, then Edit code.

Online Store Themes ⋯ menu Edit code

You should see: the code editor with a file list on the left, organized into folders (Layout, Templates, Sections, and so on).

3

Paste both snippets into theme.liquid

In the Layout folder, open theme.liquid. This file wraps every storefront page, which is why it’s the right home for GTM.

  1. Find the opening <head> tag near the top. Paste snippet 1 on a new line right after it, so it sits as high in the head as possible.
  2. Find the opening <body> tag (it may include Liquid attributes; that’s fine). Paste snippet 2 on a new line right after it.
  3. Click Save.

You should see: both snippets saved in place. If your theme gets updated or replaced later, repeat this; theme edits don’t carry over to a new theme.

4

Verify with Preview mode

In your GTM workspace, click Preview in the top right. Tag Assistant opens; enter your store’s URL and click Connect. Your store opens with a Tag Assistant badge, and page-load events appear in the Summary as you browse.

You should see: Container Loaded firing on storefront pages, product pages, and the cart. A brand-new container shows no tags fired yet, which is correct; the container is installed and empty.

Then walk into checkout and notice what happens: nothing fires. That’s the boundary from the intro, working as designed.

Password-protected store?

If your store isn’t launched yet and has password protection on, Tag Assistant may fail to connect. Enter the password in the preview window, or temporarily disable the password page while you test.

The checkout problem, honestly

Code in theme.liquid does not run on checkout pages. Your GTM container sees the cart, then goes blind until the buyer lands back on a storefront page. The old workaround for Plus merchants, editing checkout.liquid, has been retired.

The supported route is a custom pixel: under Settings > Customer events, Shopify lets you add JavaScript that runs in a sandboxed environment across all pages including checkout, subscribe to Shopify’s customer events (like checkout_started and checkout_completed), and forward them to a GTM container as data layer events. Shopify publishes an official tutorial for exactly this setup.

Go in with clear eyes, though:

  • It’s labeled advanced and unsupported. Shopify’s own docs say tag managers in the pixel sandbox carry the same restrictions as any pixel, that the setup requires JavaScript comfort, and that troubleshooting is on you.
  • The sandbox limits what tags can do, and Tag Assistant’s normal debugging doesn’t work inside it. You verify with GA4 DebugView and test orders instead.
  • Double counting is the classic failure. If purchase tracking fires from a pixel and from an app or theme tag, every sale counts twice. Give each layer a clear job: theme container owns storefront events, pixel owns checkout events.

For most stores, the pragmatic architecture is: Google & YouTube app for GA4 e-commerce (it already covers checkout), GTM in the theme for everything else. Reach for the custom pixel route when you have a hard requirement the app can’t meet, like sending checkout events to platforms the app doesn’t serve, and budget real time for it.

What to set up next

With GTM live on your storefront:

  1. Add tags with clear jobs. If GA4 isn’t on the store yet, the Google & YouTube app route beats adding it via GTM here, for the checkout reasons above. For other tags, our GA4-via-GTM guide shows the general tag-building pattern.
  2. Track storefront interactions like clicks on size guides, trust badges, or newsletter signups: button click tracking with GTM.
  3. Name everything properly from the start so your container stays readable as it grows: GTM naming conventions.

Frequently asked questions

Does Google Tag Manager work on Shopify checkout pages?
Not from theme code. Code in theme.liquid doesn't run on checkout, so a standard GTM install goes dark the moment a buyer starts checking out. The supported route into checkout is a custom pixel under Settings > Customer events, where you can load GTM in a sandboxed environment and forward Shopify's checkout events to it. Shopify documents this but labels it an advanced, unsupported setup, so treat it as a project, not a checkbox.
Should I use GTM or the Google & YouTube app for GA4 on Shopify?
If the goal is GA4 with reliable e-commerce data (product views, add to carts, purchases), use the Google & YouTube app: it covers checkout and requires no code. Use GTM alongside it for the extras the app doesn't do, like scroll tracking, outbound click tracking, or firing other platforms' tags on storefront pages. Many stores run both, with each tool doing the job it's good at.
Will installing GTM twice cause double tracking?
Yes, and it's a common Shopify mistake. If a tag fires from theme.liquid and the same tag also fires from a custom pixel (or from a Shopify app doing the same job), every event counts twice. Decide which layer owns which events: storefront tags in the theme container, checkout events in the pixel, and audit whenever numbers look suspiciously good.
Do I need Shopify Plus to use Google Tag Manager?
No. Editing theme.liquid works on every Shopify plan, and custom pixels under Settings > Customer events are available on all plans too. The old Plus-only checkout.liquid customization route has been retired, so Plus stores use the same custom pixel approach as everyone else for checkout tracking.
Where do I find my GTM container ID?
In the top bar of your workspace at tagmanager.google.com, formatted GTM-XXXXXXX. Clicking it opens the install window with both code snippets, which is where you copy them for theme.liquid.
How do I know if GTM is working on my store?
Use GTM's Preview mode: click Preview in your workspace, enter your store URL, and click Connect. Browse a few pages and confirm page-load events appear in Tag Assistant. Note that Tag Assistant can't see inside a checkout custom pixel; for that, use the pixel's own testing plus GA4's DebugView or Realtime.

Keep learning