by ShahzadaAliHassan - 11th December 2023
To begin tracking, the first step is integrating Google Tag Manager with your Shopify store:
<head>
and <body>
tags.<head>
tag and the body snippet immediately after the opening <body>
tag.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.
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.
Here, the code captures essential data when a product page is loaded:
itemObject
is created, holding key product information.dataLayer
.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>
of theme.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 the dataLayer
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.