What you’ll learn in this guide
This guide will show you how to track three essential customer interactions in your Shopify store using Google Tag Manager (GTM)
- Page views: When customers browse different pages
- Product views: When customers look at specific products
- Collection views: When customers explore product collections
How to set up specific tracking
Step 1: Initial GTM setup in Shopify
- Log into your Google Tag Manager account
- Select your store’s account
- Go to Admin section
- Click ‘Install Google Tag Manager’
- Get your unique GTM code
- Remove the HTML tags from this code
- In Shopify admin, go to Settings > Customer Events > Custom Pixel
- Paste the modified code
Step 2: Setting up specific tracking
For page view
- In GTM workspace, click ‘New Tag’
- Choose ‘Google Analytics: GA4 Event’
- Set Event Name to ‘page_view’
- Add this code to your Shopify custom pixel:
analytics.subscribe('page_viewed', (event) => {
window.dataLayer.push({
'event': 'page_viewed',
'page_location': event.context.window.location.href,
'page_title': event.context.document.title,
});
});
For product view
- Create another GA4 tag
- Set Event Name to ‘view_item’
- Add this code:
analytics.subscribe('product_viewed', (event) => {
window.dataLayer.push({
'event': 'product_viewed',
'product_id': event.data?.productVariant?.product?.id,
'product_title': event.data?.productVariant?.title,
'product_sku': event.data?.productVariant?.sku,
'timestamp': event.timestamp,
'url': event.context.document.location.href
});
});
For collection view
- Create a third GA4 tag
- Set Event Name to ‘view_item_list’
- Add this code:
analytics.subscribe('collection_viewed', (event) => {
window.dataLayer.push({
'event': 'collection_viewed',
'collection_id': event.data?.collection?.id,
'collection_title': event.data?.collection?.title,
'timestamp': event.timestamp,
'url': event.context.document.location.href
});
});
Step 3: Create trigger
For each tag, create a trigger:
- Click ‘New Trigger’
- Choose ‘Custom Event’
- Set event names to match your code:
- ‘page_viewed’
- ‘product_viewed’
- ‘collection_viewed’
Then link each trigger to its corresponding tag
Understanding how Shopify event tracking works
Shopify’s analytics system is built on an event-driven architecture. When customers interact with your store, Shopify automatically fires specific events like ‘page_viewed’, ‘product_viewed’, and ‘collection_viewed’. These events contain detailed information about each interaction.
The analytics.subscribe
method listens for these events, while the dataLayer acts as a bridge between Shopify and Google Tag Manager, making the data available in a format GTM understands.