Google Ads Remarketing Tracking: Shopify AddToCart Tracking with GTM and DataLayer
by ShahzadaAliHassan - 11th December 2023
Section 1: Setting Up Google Tag Manager on Shopify
To begin tracking, the first step is integrating Google Tag Manager with your Shopify store:
- Access GTM Account: Log into your GTM account and note your container ID.
- Implement GTM Snippets:
- Go to your Shopify admin, navigate to Online Store > Themes.
- In the theme editor, find the
<head>
and<body>
tags. - Insert the GTM head snippet below the opening
<head>
tag and the body snippet immediately after the opening<body>
tag.
- Verify the Integration:
- Use GTM's 'Preview' mode to confirm that the container is firing correctly across all Shopify pages.
Section 2: Implementing 'Add To Cart' Tracking in Shopify Using Google Tag Manager's Data Layer
This section is dedicated to integrating Google Tag Manager's dataLayer
with a Shopify store to track the custom_add_to_cart
event. This event is triggered each time a user accesses a product page and clicks the AddToCart button.
Initial Setup of DataLayer in the Head Section
This part involves setting up the dataLayer
array. If it isn't already in place, the code will initialize it. Specifically, it checks if the page is a product page and, if so, implements a 'datalayer-product' snippet.
Embedding Product Details in DataLayer
Here, the code captures essential data when a product page is loaded:
- It identifies the product's current variant.
- An
itemObject
is created, holding key product information. - Upon the page's full load, this information is pushed to the
dataLayer
.
Inserting DataLayer Code into the Theme
This section of code links the head-datalayer
snippet to your Shopify theme. This is generally where the head datalayer code is placed. It should be added to the GTM Body Code.
Steps for Integrating DataLayer Code Snippets into Shopify:
-
Backup First: Always create a backup of your current theme before any modifications.
-
Access Theme Files: Log into your Shopify Admin, navigate to Online Store > Themes. Under your live theme, click the "Actions" dropdown and choose "Edit code".
-
Insert Head DataLayer Code: Find
theme.liquid
in the left sidebar and add the "Head DataLayer Code" snippet within the<head>
section. -
Place Product DataLayer Code: Select "Add a new snippet" as per your Shopify version. Name it
datalayer-product.liquid
. Input the "Product DataLayer Code" snippet into this file and save. -
Apply Theme DataLayer Code: Decide the placement (usually within the
<head>
oftheme.liquid
). Add the "Theme DataLayer Code" snippet accordingly and save your changes. -
Verification: To test, visit a product page on your Shopify store and open the developer console in your browser. Check that the
custom_add_to_cart
event triggers and that thedataLayer
reflects accurate product details.
Note: These steps are based on a standard Shopify configuration. Custom themes may require a different approach. Always conduct thorough tests before and after applying these changes.