Tracking 'Add to Cart' Event in Shopify Using Google Tag Manager and Facebook Pixel🚀
by ShahzadaAliHassan - 14th November 2023
Introduction
In today's blog, we'll cover tracking the 'Add to Cart' event for Facebook Pixel using Google Tag Manager on your Shopify store. The tutorial is divided into four sections:
- Configuring Google Tag Manager Container on Shopify
- Configuring Page View Tracking for Facebook Pixel
- Adding Add to Cart Script in Shopify
- Finalizing Add to Cart Event Tracking in Google Tag Manager
Prerequisites
- Google Tag Manager Container
- Shopify Account
- Facebook Business Manager with a hosted Facebook Pixel
Section 1: Configuring Google Tag Manager Container on Shopify
- Go to your Google Tag Manager account.
- Copy the container ID. (Your ID may vary.)
- In Shopify, navigate to Online Stores and access the Theme file.
- Duplicate the theme file (recommended for major changes).
- Edit the code to paste the Google Tag Manager head container code under the theme section.
- Return to Google Tag Manager to copy the body code.
- Paste the body code right below the Shopify store's body tag.
- Preview and verify Google Tag Manager's functionality on your Shopify store.
Section 2: Configuring Page View Tracking for Facebook Pixel
- Access Facebook Business Manager.
- Navigate to 'All Tools' > 'Event Manager'.
- Click on your Facebook Pixel or create a new one.
- Instead of using direct code, opt for Facebook Templates provided by the Google Tag Manager community.
- Copy your Facebook Pixel ID (Database ID).
- Import the Facebook Pixel template in Google Tag Manager.
- Create a configuration tag using the Facebook Pixel template and attach it to 'All Pages' trigger.
- Verify the tag's functioning via preview mode.
Section 3: Adding Add to Cart Script in Shopify
- Add scripts to global pages or directly on product pages.
- In Shopify, create two snippets: 'Head Data Layer' and 'Product Data Layer'.
- Insert the provided code into these snippets.
- Include the 'Head Data Layer' snippet below the Google Tag Manager body snippet.
- Verify the Add to Cart event triggers in the data layer.
This section we will see how to capture more data, like the item ID, name, value, quantity, etc., when the "Add to Cart" event is triggered.
Tip:Â For this section, some programming knowledge is beneficial. If that's not your strength, you can share our document with your developer.
DataLayer Code Snippets Overview
This section provides code examples for integrating Google Tag Manager's dataLayer
object with a Shopify store. Its primary function is to track and send a custom_add_to_cart
event when a user clicks the AddToCart button on a product page in Shopify.
Initializing DataLayer in Head Section
This snippet ensures the dataLayer
array is set up. It detects if the user is on a 'product' page and, if so, implements a 'datalayer-product' snippet.
Capturing Product Page Details in DataLayer
Here, the code snippet captures essential product information when a product page is accessed:
- It identifies the product's current variant.
- It constructs an
itemObject
filled with product specifics. - Upon document loading, it sends this information to the
dataLayer
object.
Integrating DataLayer with Shopify Theme
This snippet places the head-datalayer
code within the Shopify theme. Typically, this should be added to the GTM Body Code section.
Steps for Implementing DataLayer Snippets in Shopify:
-
Backup First: Always create a backup of your theme before modifications.
-
Navigate to Theme Files: Log in to Shopify Admin, select Online Store > Themes. In your active theme, click "Actions" then "Edit code".
-
Insert Head DataLayer Snippet: Find
theme.liquid
in the left menu. Place the "Head DataLayer Code" within the<head>
section. -
Add Product DataLayer Snippet: Choose "Add a new snippet" (this varies with Shopify version). Name it
datalayer-product.liquid
and include the "Product DataLayer Code". Save the file. -
Place Theme DataLayer Snippet: Decide the location for this code (usually within the
<head>
intheme.liquid
). Insert the "Theme DataLayer Code" there. Save your changes. -
Verification: To test, visit a product page on your Shopify store and open the browser's developer console. Ensure that the
custom_add_to_cart
event triggers and thedataLayer
shows accurate product information.
Note: These instructions are based on a standard Shopify configuration. Custom themes may require different methods. Always conduct thorough tests before and after implementing changes.
Section 4: Finalizing Add to Cart Event Tracking
- In Google Tag Manager, create a custom event trigger for the Add to Cart event.
- Use Facebook Parameter Generator for mapping event parameters.
- Create necessary variables for item ID, price, quantity, etc.
- Add a data layer variable for 'ecommerce.currency'.
- Create the Add to Cart event tag using the Facebook Pixel template.
- Configure the tag with event parameters and set it to trigger on the Add to Cart event.
- Verify the event firing through preview and Facebook Pixel Event Manager.
- Submit the changes to make them live.
Conclusion
By following these steps, you'll effectively track the 'Add to Cart' events in your Shopify store, using Google Tag Manager and Facebook Pixel. This setup helps in detailed analytics and marketing strategies. Remember, always preview your changes before going live to ensure proper tracking and data accuracy.