How to Add a Tracking Script to Your Shopify Store (theme.liquid, Step by Step)
Most analytics, heatmap and pop-up tools ask for the same thing: paste a short script into the <head> of every page. On Shopify, that means one file, theme.liquid. This guide walks through the edit step by step, shows how to back up and undo it, and explains the part many tutorials skip: what a theme script can and cannot reach on Shopify's checkout, thank-you and order status pages today.
Updated

Before you start: what you need
You need three things: access to your Shopify admin with permission to edit themes, the script your tool gives you, and about ten minutes. You do not need to know Liquid (Shopify's template language) to follow this guide, but you will be looking at a code file, so it helps to go slowly and change only what is described here.
Shopify itself is cautious about code edits. Its help center says you should only edit your theme's code if you can't make the change with an app or the theme editor, and that it's meant for people who know HTML and CSS and have a basic understanding of Liquid. Pasting a vendor's script before a closing tag is about the simplest code edit there is, but the warning is a good reminder to back up first, which we cover below.
Two account details are worth checking before you begin:
- Paid themes must be purchased before you can edit their code. Shopify notes that this applies to trial versions of paid themes too. If you are trying out a theme you haven't bought, the Edit code option won't help you yet.
- Know which theme is live. Your Online Store can hold several themes, but only the current (published) theme is what shoppers see. A script added to an unpublished theme does nothing until that theme goes live.
Finally, have the script ready to copy. In marketingLABO, for example, you register your site with its name and URL, and the dashboard then shows the tracking tag in a box with a Copy tag button, along with the instruction to paste it right before </head> on every page. Other tools look different, but the principle is the same: copy the snippet exactly as given, without retyping or "tidying" it.
<script src="https://...your tag..."></script>. Always paste the exact snippet from your own tool's dashboard, not this example.Why theme.liquid is the right file
Shopify themes are built from layouts, templates and sections. According to Shopify's developer documentation, layouts are "the base of the theme, through which all templates are rendered," and the default layout file, which every theme must include, is theme.liquid. Your home page, collection pages, product pages, cart page, blog posts and regular pages are all rendered inside that layout.
That is why one edit is enough. You don't add the script to each template; you add it once to the layout that wraps them. Shopify also states that theme.liquid must contain two Liquid objects: content_for_header, which "must be placed inside the HTML <head> tag" and loads scripts Shopify needs, and content_for_layout, which outputs each template's content between <body> and </body>. You will see content_for_header in the head of the file; leave it exactly where it is.
One more thing from the same documentation shapes what this guide can promise: general layouts apply to all non-checkout pages. Checkout does not use theme.liquid at all. We return to this in detail further down, because it decides what a theme script can measure.
Step 1: Make a backup copy of your theme
Shopify recommends duplicating your theme before customizing it, so you have an untouched copy to go back to. It takes a few seconds and costs nothing.
- In your Shopify admin, go to Online Store > Themes.
- Next to your current theme, click the ⋯ (more actions) button and choose Duplicate.
- Shopify creates a copy in your theme library. It isn't published, so shoppers never see it. Rename it if you like, for example "Backup before tracking tag."
You can also rely on file history for small edits (see "How to undo the change" below), but a duplicate is the safer net, especially if you plan more code changes later.
Step 2: Open the code editor
Shopify documents two ways in, depending on your device.
| Device | Path to the code editor |
|---|---|
| Desktop browser | From your Shopify admin, go to Online Store. Click the ⋯ menu next to your current theme, then select Edit code. |
| Shopify mobile app | Tap the menu icon. Under Sales channels, tap Online Store, then Manage themes. Tap the ⋯ icon, then Edit code. |
We recommend doing this on a desktop. You will be scrolling through a long file and pasting into a precise spot, which is far easier with a full keyboard and a wide screen.
Step 3: Find theme.liquid and the closing </head> tag
- In the file list on the left, open the Layout folder.
- Click theme.liquid. The file opens in the editor.
- Click inside the editor and use your browser's or the editor's find shortcut (Ctrl+F on Windows, Cmd+F on Mac) to search for
</head>. - You should land on a single line containing the closing head tag. Everything above it, back up to
<head>, is the head of every storefront page.
If the search finds more than one match, look for the one that comes before the opening <body> tag. A theme can mention the string elsewhere (inside a comment, for instance), but there is only one real end of the head section in the layout.
Shopify themes can contain more than one file in the Layout folder. theme.liquid is the default, and it is the one that wraps your normal storefront pages. If you know your theme assigns a different layout to specific templates and you want the script there too, those layout files need the same edit.
Step 4: Paste the script right before </head>
Put your cursor at the start of the </head> line, press Enter to make an empty line above it, and paste your snippet on that new line. The result should look like this, with your own snippet in place of the placeholder:
{{ content_for_header }}
...
<!-- Tracking tag: added 2026-10-12 -->
<script src="https://...your tag..."></script>
</head>A few rules keep this edit safe:
- Paste, don't retype. A single missing quote or bracket can stop a script from running. Copy from your tool's dashboard and paste as-is.
- Don't paste inside another tag. The snippet goes on its own line, not inside a
{% ... %}Liquid block, a<style>block or another<script>. - Leave content_for_header alone. Don't move, delete or wrap it. Shopify requires it in the head.
- Add a short HTML comment. A note like the one in the example tells your future self (or a developer) what the snippet is and when it was added. This matters when themes get updated, as explained below.
- Paste it once. If the tool is already installed some other way, a second copy can count every page view twice.
Step 5: Save, then check that it works
Click Save in the top-right of the code editor. Because you edited the live theme, the change applies to your storefront right away. Now confirm it actually loads:
- Open your store in a new tab (a private window works well, so you see it the way a new visitor does).
- View the page source (Ctrl+U on Windows, Option+Cmd+U in Chrome on Mac) and search for a recognizable part of your snippet, such as its domain. If it appears just before
</head>, the edit is live. - Check a few page types: the home page, a collection, a product page and the cart. They all come from theme.liquid, so the snippet should appear on each.
- Use your tool's own check. In marketingLABO, open any page of your store, then press Check tracking in the dashboard; it tells you whether data has arrived. If nothing comes in, the dashboard suggests confirming the tag is right before </head> on every page and that your changes are saved, then reloading your site and trying again.
If the source shows the snippet but your tool still reports nothing, try again from a browser without extensions that block scripts, and check that the site URL registered in your tool matches the domain you are visiting.
How to undo the change
If something looks wrong after saving, you have two ways back.
| Method | How it works | Good to know |
|---|---|---|
| Delete the lines you added | Open theme.liquid again, remove the comment and the snippet, and save. | Fastest fix when you know exactly what you added, which is why the comment helps. |
| Restore an earlier version (Timeline) | Open the file and use the Timeline view to compare versions. Right-click an entry, select Restore contents, then click Restore. | Restoring replaces the whole file, works one file at a time, and history is finite. Files in the assets folder have no Timeline history. |
| Publish your backup copy | Go back to Online Store > Themes and publish the duplicate you made in Step 1. | Brings back the whole theme as it was, including anything else you changed since. |
Shopify's advice before restoring is worth repeating: copy the current file contents somewhere safe first, because restoring replaces everything in the file, including changes you might want to keep.
What happens when you update or switch themes
Your snippet lives inside one theme's code, so it travels with that theme and nowhere else. Two situations catch store owners out:
- Theme updates. Shopify warns that if your code changes are incompatible with a theme update, all your code changes are removed in the updated copy. After updating a theme, view the page source again and confirm the snippet is still there. The comment you added makes it easy to find.
- New themes. If you switch to a different theme, the new theme's theme.liquid doesn't contain your snippet. Repeat Steps 2 to 5 on the new theme before you publish it, so you don't lose days of data.
A simple habit helps: keep a short note (in your password manager, a shared doc or a sticky note) listing every script you have added to theme.liquid and why. When you redesign or update, that list becomes your checklist.
Checkout, thank-you and order status pages: what a theme script can't reach
This is where Shopify differs from most other platforms, and where older tutorials go out of date. Shopify's documentation says general layouts apply to all non-checkout pages. In practice, a script you paste into theme.liquid runs on your storefront pages but not on the checkout, the Thank you page or the Order status page.
For years, there were workarounds. Shopify Plus stores could edit checkout.liquid, and many stores pasted conversion code into an "Additional scripts" box for the order status page. Those routes are closed or closing:
| Old approach | Status as documented by Shopify |
|---|---|
| checkout.liquid for the Information, Shipping and Payment steps | No longer supported. checkout.liquid was only ever available to Shopify Plus merchants. |
| Customizations on the Thank you and Order status pages (apps, scripts or pixels that aren't compatible) | Shopify's help center states that August 26, 2026 was the deadline to upgrade and replace these pages, and that stores that weren't upgraded by then were upgraded automatically. |
So as of today, if you were relying on an old "Additional scripts" snippet to record purchases, check whether it still fires. Shopify's upgrade guide says stores with incompatible apps, scripts or pixels on those pages are required to upgrade and replace them.
The supported route for checkout events: customer events and custom pixels
Shopify's current answer for tracking beyond the storefront is customer events, delivered through pixels. Shopify describes custom pixels as a way to collect customer events on more of your store, including checkout and post-purchase pages, without modifying your theme code. Apps can also provide their own pixels (app pixels); Shopify positions custom pixels for cases where no app pixel meets your needs.
Adding a custom pixel follows these steps, per Shopify's help center:
- From your Shopify admin, go to Settings > Customer events.
- Give your pixel a unique name, then click Add pixel to open the event editor.
- Optionally, adjust the Permission and Data sale settings in the Customer privacy section.
- Paste your custom JavaScript pixel code into the Code window.
- Click Save, then Connect pixel when you're ready to start tracking.
Before you go down this road, understand the trade-offs Shopify documents:
- Pixels run in a sandbox. Shopify's developer docs say custom pixels load in a "lax" sandbox, an iframe, and that traditional JavaScript pixels placed there cannot access the top frame. For example,
window.hrefreturns the sandbox URL instead of the page's URL. App pixels run in a stricter sandbox where features that scrape or write to the page won't work. - "Not all pixel functionality works in the sandbox." Shopify says you may need help from your third-party provider.
- Support and responsibility. Shopify states that adding and using custom pixels is unsupported by Shopify, and that compliance with applicable laws, consents, code security, troubleshooting and updates are your responsibility.
- Avoid double counting. Shopify advises removing existing pixel code from places like theme.liquid before adding the same pixel as a custom pixel, so events aren't counted twice.
The practical takeaway: a custom pixel is a data-collection mechanism. It is designed to send events (such as a completed checkout) to a provider that accepts them in that format. It is not a way to run a normal page script on checkout. A tag that needs to draw on the page, such as a pop-up, a heatmap overlay or anything that reads the visible page, should not be expected to work from inside the sandbox. If you need purchase tracking in checkout, ask your provider whether they offer a Shopify app pixel or official custom-pixel code.
What this means if you use marketingLABO on Shopify
We want to be precise here, because it affects how you set goals. marketingLABO is installed with one tag in the <head> of every page; it works on any site where you can do that. On Shopify, that is theme.liquid, and it covers the pages shoppers browse before checkout.
| Store area | Covered by a tag in theme.liquid? | Notes |
|---|---|---|
| Home, collection, product, cart, blog and regular pages | Yes | Analytics, heatmaps, visitor history and on-page pop-up banners work here. |
| Checkout steps | No | Shopify renders checkout outside your theme. |
| Thank you and Order status pages | No | Also outside your theme; Shopify points to pixels for event tracking there. |
marketingLABO has no Shopify app and doesn't connect to your store's order data, so it doesn't read cart contents, cart value, products or orders. We don't provide custom-pixel code, so please don't paste the marketingLABO tag into Customer events. It was built for regular pages, not the sandbox.
Within those limits, there is still plenty to measure and act on with storefront pages alone:
- Micro-conversions you can see on the storefront. marketingLABO can count micro-conversions by pages viewed, a specific URL, time on page or return visits. Reaching
/cartis a reasonable signal of intent on Shopify. - Banners by page behavior. Pop-up banners can target visitors by page URL and by pages viewed or not viewed, by visit count, days since last visit and traffic source, and can trigger immediately, after a delay, at a scroll depth or on exit intent.
- Heatmaps on product pages. See how far people scroll and where they click on the pages that lead to checkout.
For revenue and order counts, keep using Shopify's own reports, since checkout happens inside Shopify. Use page-level tools for what happens before checkout: where people come from, which pages they read and where they drop off.
Common mistakes and how to avoid them
| Mistake | What happens | Fix |
|---|---|---|
| Editing an unpublished theme | Nothing changes on the live store. | Check which theme is current in Online Store > Themes. |
| Pasting after </head> or inside <body> by accident | Usually still loads, but the tool may start later than intended or not as documented. | Move it to the line just before </head>. |
| Pasting inside a Liquid tag or another script | The snippet may not run, or the page may break. | Give the snippet its own line. |
| Retyping or reformatting the snippet | A small typo stops the script. | Copy and paste from the tool's dashboard. |
| Installing the same tool twice (theme + app or pixel) | Duplicate page views or events. | Keep exactly one installation per tool. |
| Forgetting after a theme update or redesign | Tracking silently stops. | Re-check page source after every theme update or switch. |
Quick checklist
- Theme duplicated as a backup.
- Snippet copied directly from your tool's dashboard.
- Pasted on its own line, right before </head> in Layout > theme.liquid of the current theme.
- A comment added with the tool name and date.
- Saved, then verified in the page source on home, collection, product and cart pages.
- Tool's own tracking check passed.
- Expectations set: checkout, thank-you and order status pages are outside theme.liquid.
FAQ
Do I need to add the script to every template? No. Templates are rendered through the layout, so one copy in theme.liquid covers your normal storefront pages. Only add it elsewhere if your theme uses a different layout file for some templates.
Can I still add scripts to the Thank you page through settings? Shopify's help center says August 26, 2026 was the deadline to upgrade and replace the Thank you and Order status pages, with automatic upgrades after that. Use apps, app pixels or custom pixels as Shopify documents them, and confirm with your provider what they support.
Does this guide apply to headless Shopify storefronts? No. It covers Online Store themes, where theme.liquid exists. Headless storefronts are built differently and need their own setup.
I run WooCommerce on another site. Is it similar? The idea is the same (get the tag into the head of every page), but the method differs. See How to add a script to the head of a WooCommerce store.
marketingLABO's Free plan includes one site with analytics, heatmaps and visitor history, plus one form and one pop-up banner, with no credit card required. Compare plans on the pricing section, or sign up free to get your tag.
Sources
All checked on October 12, 2026.
- Shopify Help Center: Editing theme code
- Shopify.dev: Layouts (theme.liquid, content_for_header)
- Shopify Help Center: Upgrading the Thank you and Order status pages
- Shopify Help Center: Custom pixels
- Shopify Help Center: Managing custom pixels
- Shopify.dev: About web pixels (sandboxes)
- Shopify.dev: Web Pixels API
Start free — no credit card needed
Sign up freeMore articles

How to Add a Script to the <head> of a WooCommerce Store (Without Breaking Your Theme)
Add a tracking script to the head of a WooCommerce store with the wp_head hook: a tiny plugin or a child theme, for classic and block themes, plus testing.
Read the article →
Website Heatmaps Explained: Click, Scroll and Mouse-Movement Maps for Small Sites
What click, scroll and mouse-movement heatmaps show, how to read scroll depth on product pages, and the common misreads that lead small sites to the wrong fix.
Read the article →
Cart Abandonment: Why Shoppers Leave and What You Can Do On-Site
Why shoppers abandon carts, based on Baymard Institute research, and the on-site fixes and returning-visitor reminders that address each reason.
Read the article →
How to Build a Welcome Email Series for a Small E-commerce Store
A step-by-step plan for a 4-email welcome series: timing, wait steps, branches on opens and return visits, sample copy, and the CAN-SPAM basics.
Read the article →
Free Shipping Banners: Where to Show Them and How to Word the Threshold
Where to place a free shipping banner (bar or modal), when to show it, and how to word the threshold so it matches your checkout.
Read the article →
Form Abandonment: How to Find the Field Where People Give Up
How to measure form abandonment field by field, read a drop-off report, and decide what to fix first: field count, order, labels or validation.
Read the article →