Complete Guide to Facebook Pixel Purchase Event Setup on Shopify Using Google Tag Manager
by ShahzadaAliHassan - 16th November 2023
Introduction
In this comprehensive guide, we'll explore how to configure the purchase event on Facebook Pixel using Google Tag Manager (GTM) for your Shopify store. This setup is crucial for tracking conversions and ensuring accurate attribution in Facebook Ads. The process is divided into five distinct sections:
- Setting up GTM container on Shopify.
- Configuring the Facebook configuration tag for tracking.
- Adding tracking scripts for purchase events.
- Setting up GTM to track these scripts.
- Testing and finalizing the setup.
Prerequisites
- Google Tag Manager container.
- Shopify store.
- Facebook Business Manager account.
Section 1: Setting Up GTM on Shopify
Step-by-Step Instructions:
- Access GTM for Tracking Scripts: Navigate to the GTM overview page, find your GTM container ID, and access head and body container codes.
- Copy Head Code: Begin by copying the head container code.
- Shopify Store Setup:
- Go to 'Online Stores' in your Shopify dashboard.
- Ensure you have the necessary permissions to edit themes.
- Duplicate the theme for backup.
- Find and edit the
theme.liquid
file. - Insert the head container code below the
<head>
section.
- Add Body Snippet: Copy the body snippet from GTM and insert it into your Shopify store's theme layout, typically around line 255.
- Verify Tracking: Use the GTM preview and debugging tools to ensure events are firing correctly on your site.
Final Thank You Page Tracking:
- Add the GTM head container code in the 'Checkout' settings of your Shopify store, specifically in the 'Order Status Script' section.
Section 2: Configuring Facebook Pixel Tag
- Access Facebook Business Manager: Go to 'Event Manager' in your Facebook business account.
- Copy Pixel ID: Obtain your Facebook Pixel (now called 'database') ID.
- Set Up in GTM:
- Use the Facebook-provided template in GTM's template library.
- Create a constant variable for the Facebook Pixel ID for ease of use.
- Configure the Facebook Pixel tag to fire on all pages, ensuring page view tracking and user session data capture.
Section 3: DataLayer code for Purchase Events
The provided code snippets aim to integrate the dataLayer
object for Google Tag Manager (GTM) with a Shopify store. The main goal is to send an event called custom_purchase
whenever a user visits the final /thank-you/ page on Shopify after making the payment.
Order Status DataLayer Code
- This script, embedded within a Shopify platform, serves to track purchase-related data for analytics purposes.
- When a user accesses a site for the first time (
first_time_accessed
), the script checks if the user is a registered customer. If so, it fetches the user's order count and total spend. - The script then initializes the
dataLayer
object if it doesn't already exist and populates it with purchase details like transaction ID, total value, shipping, tax, currency, and details of items in the order. - These details include individual product attributes such as the product's ID, SKU, name, price, brand, variant, quantity, and other related info.
Steps to Add the DataLayer Code Snippets in Shopify:
-
Log into Your Shopify Admin Panel:
- Visit your Shopify store's URL followed by
/admin
. - Enter your login credentials to access the admin panel.
- Visit your Shopify store's URL followed by
-
Access Checkout Settings:
- On the left-hand side menu, click on
Settings
. - From the dropdown, select
Checkout
.
- On the left-hand side menu, click on
-
Locate the Additional Scripts Section:
- Scroll down until you find the
Order processing
section. - Within this section, you'll see a text box labeled
Additional Scripts
. This is where you can add scripts that will run on the order status page after a customer completes their purchase.
- Scroll down until you find the
-
Paste Your Script:
- Copy the script you have.
- Paste it into the
Additional Scripts
text box.
-
Save Changes:
- After you've pasted your script, scroll to the bottom of the page and click on the
Save
button.
- After you've pasted your script, scroll to the bottom of the page and click on the
-
Test the Script (Important):
- After saving, make a test purchase to ensure that the script runs as expected on the order status page.
- Check if all data is being collected and pushed to the
dataLayer
as intended by your script.
Note: The Additional Scripts section in the Checkout settings allows you to add scripts that will run after a customer has completed their purchase. This is a safe place to add conversion tracking codes, analytics scripts, or other scripts you want to trigger once a sale is confirmed.
Section 4: GTM Configuration for Purchase Tracking
- Create Custom Trigger: Set up a trigger in GTM for the custom purchase event.
- Set Data Layer Variables: Establish variables for transaction ID, value, tax, currency, and item details.
- Tag Creation and Setup:
- Use the Facebook Pixel tag.
- Configure event parameters (e.g., order ID, content type, items).
- Ensure correct mapping to Facebook Pixel requirements.
Section 5: Testing and Publishing
- Preview and Debug: Utilize GTM's preview mode to test the setup.
- Conduct Test Transactions: Perform dummy purchases to verify the firing of purchase events and data capture.
- Final Checks:
- Confirm event tracking on the Facebook Pixel helper extension.
- Review data in Facebook Event Manager.
- Publish Changes: Once satisfied, publish the changes in GTM.
Conclusion
By following these steps, you can effectively configure the Facebook Pixel purchase event on your Shopify store using Google Tag Manager. This setup enhances your Facebook Ads' tracking and attribution accuracy, crucial for successful digital marketing strategies.