Shopify, Facebook Pixel & Google Tag Manager Setup for View Content! 🚀
by ShahzadaAliHassan - 13th November 2023
Introduction
In this tutorial, we'll explore configuring the 'View Content' event for Facebook Pixel on a Shopify store using Google Tag Manager (GTM). This is particularly useful if you're not using Shopify's built-in Facebook and Instagram app.
Prerequisites
- Google Tag Manager Container
- Active Shopify Store
- Facebook Business Manager Account with a Facebook Pixel
Section 1: Configuring Google Tag Manager on Shopify
- Go to your GTM Container, click on the container ID or navigate to 'Admin' > 'Install Google Tag Manager' to get the code snippets.
- Copy the 'Head' snippet and paste it right under the
<head>
tag in thetheme.liquid
file on Shopify. - Copy the 'Body' snippet and paste it just after the opening
<body>
tag in Shopify. - Save changes and use GTM's preview mode to verify the setup.
Section 2: Setting Up Facebook Pixel Base Code
- In Facebook Business Manager, navigate to 'Event Manager' > 'Data Sources'.
- If you have a Pixel created, select it; otherwise, create a new one.
- Copy the Pixel's base code.
- In GTM, create a new tag with this base code as a 'Custom HTML' tag.
- Set the trigger for all pages and save.
- Alternatively, use Facebook's custom template in GTM for a simpler setup.
Section 3: Setting Up 'View Content' Event Scripts
This section details the setup and implementation of a data layer script on a Shopify website, crucial for tracking and analytics. It involves initializing and populating the dataLayer
object to accurately capture key data.
Initialization of the DataLayer
Firstly, a script snippet is used to initialize the dataLayer
array, assuming it's not already set up. This snippet also checks if the current page is a product page, indicated by the template type, and if so, triggers the 'datalayer-product' snippet.
Gathering Product Information
The 'datalayer-product' snippet plays a critical role in capturing product information. Its process involves:
- Identifying and recording the active product variant.
- Creating an
itemObject
containing detailed product information. - Pushing this information to the
dataLayer
object when the document loads.
Theme DataLayer Code Integration
To incorporate the theme-specific data layer code, the 'head-datalayer' snippet must be rendered. This is typically found in the head section of the Shopify theme. It should be added to the GTM Body Code section.
Implementation Steps in Shopify:
-
Backup Creation: Start by backing up your current theme.
-
Theme File Access: Navigate to Online Store > Themes in your Shopify Admin, and choose "Edit code" from the "Actions" menu of your active theme.
-
Head DataLayer Code Addition: Locate
theme.liquid
in the sidebar, and embed the "Head DataLayer Code" snippet within the<head>
section. -
Product DataLayer Code Insertion: Form a new snippet (name it
datalayer-product.liquid
if required), and include the "Product DataLayer Code" snippet. Save the changes. -
Theme DataLayer Code Placement: Identify the appropriate spot within the
<head>
section intheme.liquid
for the "Theme DataLayer Code" snippet and insert it. Save the modifications. -
Verification: To test, visit a product page on your Shopify site, open the browser's developer console, and confirm that the
custom_view_item
event fires and thedataLayer
correctly reflects the product details.
By following these steps, you ensure your Shopify site effectively gathers and leverages data for tracking and analytics.
Section 4: Creating Tags for 'View Content' Event in GTM
- Create a new trigger in GTM for the custom 'View Item' event.
- Set up necessary variables for enhanced e-commerce tracking in GTM.
- Create a custom HTML tag or use the Facebook Pixel template tag in GTM.
- Ensure the tag fires the 'View Content' event with the correct parameters.
- Verify the setup through GTM's preview mode and Facebook Pixel's helper.
Section 5: Testing and Publishing
- Publish your GTM changes with a descriptive version name.
- Test the integration using Facebook Pixel's debug view.
- Ensure the 'View Content' event is firing correctly with all required parameters.
Conclusion
By following these steps, you can successfully configure the 'View Content' event for Facebook Pixel on your Shopify store using Google Tag Manager. This setup is crucial for accurate tracking and data collection for your marketing efforts.