Skip to main content
Understand how customers navigate your Shopify store. See what paths lead to purchases and where people drop off.
Back up your theme first! Go to Themes → Actions → Duplicate before editing code. This gives you an easy rollback.

What You’ll Need

Step 1: Open Theme Code

  1. Log into Shopify admin
  2. Click Online StoreThemes
  3. Find your active theme (has “Current theme” badge)
  4. Click ActionsEdit code

Step 2: Edit theme.liquid

  1. In the file list, find Layout folder
  2. Click theme.liquid
  3. Find the </head> tag (use Ctrl+F / Cmd+F to search)
  4. Add this code right before </head>:
Replace your-tenant-id with your tenant identifier (not UUID)!
Those {{ template }} and {{ shop.name }} things are Liquid variables—Shopify automatically fills them in with real values.

Step 3: Save

Click Save (top right). You’ll see a confirmation message. That’s it! Grain is now tracking your store. 🎉

Track E-commerce Events

Let’s track the important stuff: cart additions, checkouts, and purchases.

Track “Add to Cart”

Still in theme.liquid, add this just before </body>:

Track Checkout Started

Track Purchases

For completed orders, add this to Settings → Checkout → Order status page → Additional scripts:
Order status scripts: Available in Settings → Checkout on Shopify Plus or certain themes.

Track Product Views

Add this in theme.liquid to track product page visits:

Test Your Integration

  1. Open your store in an incognito/private window
  2. Browse a product, add to cart, go to checkout
  3. Open browser console (F12 → Console)
  4. Type window.grain — you should see the Grain object
  5. Check your Grain dashboard for events
Debug mode: Temporarily add debug: true to see console logs:

Troubleshooting

Code Not Working

  • Check for typos in your tenant ID
  • Clear cache (Ctrl+Shift+R / Cmd+Shift+R)
  • Check browser console for errors (F12)
  • Verify code is before </head> or </body> tags

Events Not in Dashboard

  • Wait 1-2 minutes (slight delay is normal)
  • Check that window.grain exists in console
  • Verify tenant ID matches your dashboard
  • Make sure you saved the theme file

Theme Broke

  • Revert to backup: Themes → Actions → Unpublish, then publish your backup
  • Double-check you didn’t delete any Liquid tags ({{ }} or {% %})

What’s Next?

Tracks & Funnels

Analyze customer journeys from landing to purchase

E-commerce Guide

Best practices for e-commerce analytics

Remote Config

A/B test pricing, messaging, and layouts

Query API

Build custom reports
Need help? Chat with us at grainql.com or email support@grainql.com.