theme.liquid file of your Shopify theme. Then it adds events
for product views, cart additions, checkouts, purchases, and searches. It takes about 10 minutes.
Before you start
You need three things:- Admin access to your Shopify store.
- Your tenant alias from the dashboard. It is the alias shown at the top of the dashboard, not the UUID.
- 10 minutes.
1. Open the theme code
- Log in to Shopify admin.
- Click Online Store, then Themes.
- Find your active theme. It has the “Current theme” badge.
- Click Actions, then Edit code.
2. Edit theme.liquid
- In the file list, open the Layout folder.
- Click theme.liquid.
- Find the
</head>tag. Use Ctrl+F or Cmd+F to search. - Add this code directly before
</head>:
your-tenant-id with your tenant alias.
{{ template }}, {{ shop.name }}, and {{ shop.currency }} are Liquid variables. Shopify
replaces them with the real values when it serves the page.
3. Save
Click Save (top right). Shopify shows a confirmation message. The Grain Tag now records page views, clicks, and scroll depth on every page of the store. Read Automatic tracking.4. Track e-commerce events
Each snippet in this step sends one event withGrainTag.track. If a snippet runs before the
script is ready, the Grain Tag queues the event and sends it later.
Add to cart
Intheme.liquid, add this code directly before </body>:
Checkout started
Add this code after the add-to-cart snippet:Purchases
The order status page is not part of the theme. Go to Settings, then Checkout, then Order status page, then Additional scripts, and add this code:Order status scripts are available in Settings, then Checkout, on Shopify Plus or certain
themes.
Product views
Add this code intheme.liquid. The {% if %} tag limits it to product pages:
Searches
Add this code intheme.liquid. It runs on the search results page only:
5. Test the installation
- Open your store in a private (incognito) window.
- Open a product page, add the product to the cart, and go to checkout.
- Open the browser console (F12, then Console).
- Type
window.GrainTag. The console shows theGrainTagobject. - Open the dashboard and make sure that the events are there.
Troubleshooting
Read Troubleshooting for cases that are not specific to Shopify.
Next steps
Trackers
Track clicks on specific elements from the dashboard, without code.
Heatmaps
See where visitors click and how far they scroll.
Tracks
Build funnels from landing page to purchase.
Privacy and consent
What Grain stores, cookieless mode, and consent modes.