by ShahzadaAliHassan - 14th November 2023
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:
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.
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.
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.
Here, the code snippet captures essential product information when a product page is accessed:
itemObject
filled with product specifics.dataLayer
object.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>
in theme.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 the dataLayer
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.
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.