← Back to Trustlytics Sign in
Support Center Settings Tracking code Add the tracking code in Shopify

Add the tracking code in Shopify

Shopify has no ready made box for custom code. So you put the Trustlytics code straight into your theme, into the theme.liquid file to be exact. Sounds technical, but it is a five minute job. You do not have to program anything, you just place one line in the right spot.

Two things up front so this stays relaxed. Make a copy of your theme first, then nothing can happen to you. And keep this in mind: the code lives in the theme. If you switch designs later, it has to go into the new one.

Two ways, same result:

The video is in the works. Until then, the guide will get you there.

Step-by-step guide

What you need for this

  • Your tracking code. You get it under Tracking code with one click on "Copy".
  • Access to your Shopify admin area with the right to edit themes. As a staff member with limited permissions, you will not see the code editor.
  • Five quiet minutes. You are working in a file that carries your whole store. No reason to panic, but not a job to squeeze in between two meetings either.

Creating a backup

This step is optional. We still recommend it to everyone, for one simple reason: it costs twenty seconds and takes all the nerves out of it.

  • In the admin area, go to "Online Store" on the left.
  • Click the three dots (•••) next to your current theme.
  • Choose "Duplicate".

Now an untouched copy sits in your theme list. If something goes wrong while editing, you simply make the copy your active theme again, and your store looks exactly like before.

Add the Tracking Code in Shopify: Creating a backup - Trustlytics
Add the Tracking Code in Shopify: Duplicate - Trustlytics
Add the Tracking Code in Shopify: The copy - Trustlytics

Opening the code editor

You are still in the theme overview. Click the three dots (•••) next to the active theme again and this time choose "Edit code".

Watch which theme you grab. The copy you just made is now in the list too. Edit the copy by mistake, and the code will be in there, but it will not run in your store.
Add the Tracking Code in Shopify: Opening the code editor - Trustlytics

Opening the theme.liquid file

On the left you now see a long list of every file in your theme, sorted into folders. Open the "layout" folder and click theme.liquid inside it.

Why this file of all files: theme.liquid is the framework of your store. Every single page is passed through it, the home page just like every product page. Whatever is in here applies everywhere. So you only have to place the code once.

The keyboard is faster. In the code editor, press Ctrl + P (on a Mac Cmd + P), type "theme.liquid" and pick the file from the list. That saves you the hunt through the folders.
Add the Tracking Code in Shopify: Opening theme.liquid - Trustlytics

Finding the right spot

There is a lot of code in the main window now. Do not let that impress you, you need exactly one line of it.

  • Click anywhere in the code.
  • Press Ctrl + F (on a Mac Cmd + F). A search field appears.
  • Type </head> and press Enter or click "Next".

The editor jumps to the line with the closing </head> and highlights it. That is your target. You will usually find this line fairly far up in the file.

What this tag means: everything between the opening and the closing head tag belongs to the head section of a page. This section loads before the visitor sees anything at all. That is exactly where your tracking code should sit, so that even very short visits get counted.

Add the Tracking Code in Shopify: Searching for head - Trustlytics
Add the Tracking Code in Shopify: head found - Trustlytics

Pasting in the tracking code

Put your cursor at the end of the line directly above the </head>, press Enter for a new line, and paste your tracking code there.

Only one thing matters: the code has to sit before the </head> , not after it. The result looks like this::

<script defer data-domain="YOUR-DOMAIN.COM" src="https://app.trustlytics.ch/tl.js"></script>
</head>

Use the Copy button in your Trustlytics account. Selecting it by hand loses an angle bracket fast, and then Trustlytics counts nothing at all. The comment lines around it can stay right where they are, they do no harm in this file.

One more note on the address in the code. It has to hold the domain your customers actually use to reach the store. So your own address, not the internal Shopify address ending in myshopify.com. If you grab the code with the Copy button, that part is already filled in correctly.

Add the Tracking Code in Shopify: Pasting in the tracking code - Trustlytics

Saving

Click "Save" in the top right.

You do not need to publish on top of that. Unlike many website builders, your change in Shopify is live the moment you save it in the active theme.

Shopify reports an error? Then a character probably slipped while pasting. Undo the change with Ctrl + Z (Mac: Cmd + Z) and paste the code again, this time using the Copy button.
Add the Tracking Code in Shopify: Saving - Trustlytics

Checking

Now the moment of truth, and it takes less than two minutes:

  1. Open your store in a new browser window.
  2. Click through a few pages, two or three products for example.
  3. Switch to the Real-time report in Trustlytics.
  4. Within a few seconds, your own visit should show up there.

If something is there, everything is installed correctly. If it stays empty, the section below will help.

Is your store still password protected? Then nobody gets in, not even you, without the password. New stores often are, without anyone noticing. You will find the setting under "Online Store" and "Preferences" at the Store access item.

When nothing gets counted

Five causes cover almost every case:

Cause What to do
Edited the wrong theme The most common case, especially right after duplicating. Check in the theme overview which theme is marked as "Current theme", and look in theme.liquid there.
Code ended up after the </head> Open theme.liquid and search again with Ctrl + F. Your code line has to sit above the </head>, not below it.
Store still password protected As long as the password protection is on, nobody reaches your store. You switch it off under "Online Store" and "Preferences".
Not saved Open theme.liquid again. If your line is missing, the save did not go through. Paste the code once more and save in the top right.
Wrong address registered The code only works on the website listed in your Trustlytics account. Check that under Website. With Shopify, this is often a mix-up between your own domain and the myshopify.com address.

What you should keep in mind

One point that causes confusion later on: the tracking code sits in your theme, not in your store. That has two consequences you should know about.

Situation What happens What to do
You switch themes The code stays behind in the old theme. Measurement stops immediately and without warning. Go through this guide again in the new theme.
Your theme gets an update Depending on how it is done, theme.liquid gets overwritten. Your code is gone then. After every theme update, take a quick look at the Real-Time report to see whether anything is still being measured.

A good moment for that check is always right after somebody has worked on the design. Two minutes in the Real-Time report save you the nasty surprise of three weeks of missing data.

Common questions

Do I need to know how to code?
No. You paste a finished line into a specific spot. That is all that happens. And anyone who duplicates the theme first cannot break anything along the way.

Why does Shopify not have a simple box for custom code?
Because Shopify assumes that extra features come through apps. A Trustlytics app is in the works. Until it is here, the path goes through theme.liquid, and that one works flawlessly.

Is there a Trustlytics app for Shopify?
Not yet. It is on our list, but we cannot give you a date. Until then, the path through theme.liquid is the right solution, and it works fully. When the app arrives later, your measurement keeps running without a break. So there is no reason to wait and nothing to repair afterward.

Does the code apply to all pages of my store?
Yes, to every page that runs through your theme. So the home page, categories, product pages, the blog, and your own pages. New products are included automatically.

Do I have to put the code into other files too?
No, theme.liquid is enough. This file is the framework every page runs through. Put the code into other files on top of that, and you end up measuring twice.

Is the checkout measured too?
In Shopify, the checkout runs on its own pages that are not controlled by your theme. So whatever happens there does not show up in this measurement. Everything up to the cart is recorded.

The code editor is showing orange or red warnings. Did I break something?
Probably not. Notices like these are already in many themes beforehand and come from the theme itself. All that matters is whether your save went through and whether visits show up in the Real-Time report. But if a warning appeared at the exact moment you placed the code, undo the change and paste it in again with the Copy button.

What happens during a theme update?
It can overwrite your change. In Shopify, a theme update is often not a simple refresh but a new version into which your own adjustments have to be moved back by hand. So after every update, take a quick look in theme.liquid to check that your line is still there.

An agency takes care of my theme. Is there anything I should watch out for?
Yes, one thing. Let them know that there is a Trustlytics line in theme.liquid. If the theme gets rebuilt or replaced, it disappears unnoticed otherwise. A screenshot of this guide is plenty as a heads-up.

Can I remove the code later?
Yes. Open theme.liquid, delete the Trustlytics line, and save. From then on, nothing is measured anymore.

Will my store get slower because of it?
No. The script is tiny and loads in the background without holding up the build of your pages. In a store, especially, every tenth of a second counts, which is why we keep it small.

Was this article helpful?