Tracking View Category Events in Facebook Pixel Using Google Tag Manager and Shopify
by ShahzadaAliHassan - 17th November 2023
In this comprehensive guide, we'll delve into how you can effectively track "View Category" events in Facebook Pixel. This capability is crucial for understanding user engagement on category pages of your website and can be pivotal for creating targeted audiences or data-related events. We'll be utilizing Google Tag Manager and a Shopify store for this project.
Prerequisites
Before diving into the tutorial, ensure you have the following:
- A Shopify store.
- A Google Tag Manager container.
- A Facebook Pixel set up in your Business Manager.
Section 1: Configuring Google Tag Manager on Your Shopify Store
- Accessing Container ID: In your Google Tag Manager account, locate the container ID at the top right corner.
- Copying Container Codes: There are two codes provided - one for the head section and another for the body. Both must be placed in your Shopify theme file to track user interaction across the website.
- Editing Shopify Theme File:
- Click on "Online Store" in your Shopify dashboard.
- Duplicate the theme file for backup.
- Access the theme's code editor.
- Paste the head tracking snippet just below the opening
<head>
tag. - Similarly, paste the body tracking snippet near the opening
<body>
tag.
Section 2: Configuring the Facebook Pixel Base Tag
- Navigating to Event Manager: Go to your Facebook Business Manager and access the Event Manager.
- Setting Up Pixel: If you haven't already, create a new pixel named 'Tracking Academy' or similar.
- Creating Variable in Google Tag Manager: In Google Tag Manager, create a constant variable for the Facebook Pixel ID for easy reference.
- Installing Template for Facebook Pixel: Search for and install a Facebook Pixel template from the Google Tag Manager template gallery, created by the Facebook archive team.
- Configuring Pixel Tag: Create a new tag in Google Tag Manager using the Facebook Pixel template and the previously created Pixel ID variable.
Section 3: Adding View Item List Scripts
This script begins by ensuring the dataLayer
array is established. It then identifies if the webpage is a product page and, if so, implements the 'datalayer-collection' snippet.
Collection Page DataLayer Script
The primary function of this script is to enrich the dataLayer
object with product-specific information when a collection page is accessed:
- It initially identifies the current product variant.
- An
itemObject
is created, encapsulating various details about the product. - Upon the document's loading, this information is added to the
dataLayer
object. - Save the file with name
datalayer-collection.liquid
Theme-Level DataLayer Script
This script executes the head-datalayer
snippet. Typically found in the head section of your Shopify theme's setup, this snippet is key for integrating the head datalayer code. Incorporate this within the GTM Body Code.
Steps for Implementing DataLayer Scripts in Shopify:
-
Backup First: As a precaution, ensure you have a backup of your current theme.
-
Theme File Access: Log into your Shopify Admin, navigate to Online Store > Themes, find your active theme, click "Actions," and select "Edit code".
-
Head DataLayer Script Insertion: Find
theme.liquid
in the sidebar, and within the<head>
tags, place the "Head DataLayer Script". -
Incorporating Product DataLayer Script: Opt to "Add a new snippet" (this varies with Shopify versions). Name this snippet
datalayer-product.liquid
and include the "Product DataLayer Script". Save your modifications. -
Theme DataLayer Script Placement: Decide the appropriate spot (usually within the
<head>
section intheme.liquid
) to insert the "Theme DataLayer Script". Implement and save. -
Verification: Test the integration by visiting a product page on your Shopify store. Open the developer console in your browser to confirm that the
custom_view_item
event triggers and thedataLayer
object accurately displays product information.
Section 4: Creating View Category Event in Google Tag Manager
- Setting Up Trigger: Create a new custom event trigger in Google Tag Manager using the name 'custom VI item list'.
- Creating Variables: Set up data layer variables in Google Tag Manager to extract values like currency, item list name, and items array.
- Mapping Event Parameters: Since Facebook Pixel uses different event parameter names, map Google Analytics' e-commerce event parameters to Facebook's using a variable template.
- Finalizing Tag Configuration: Create a new tag for the 'View Category' event, ensuring all necessary parameters are included.
Section 5: Testing and Publishing Changes
- Testing Setup: Use the Google Tag Manager preview mode and Facebook Pixel helper extension to ensure tags are firing correctly on your Shopify store.
- Publishing Changes: Once verified, publish the changes in Google Tag Manager with a descriptive version name.
Conclusion
By following these steps, you will have successfully set up tracking for 'View Category' events in Facebook Pixel using Google Tag Manager and Shopify. This setup allows for detailed insights into user interactions with your category pages, paving the way for more refined audience targeting and data-driven marketing strategies. Remember, it may take 1-3 hours for the event to become available in your Facebook Event Manager, after which you can start creating retargeting audiences or custom reports in your ad account.