Articles on: CMS Overview

[Shopify] Set up purchase webhooks

By implementing webhooks to your Shopify store, you can efficiently track and process key events, such as purchases and user interactions, in real-time. Webhooks allow for seamless data collection by sending event information directly to GTM, where it can be processed, organized, and transmitted to analytics tools. This setup ensures accurate tracking, enhances data reliability, and provides better insights into customer behavior and store performance.

In this document you will learn:

Install Tagging Pixel in your Shopify Website

Google Tag Manager Web Container setup

Google Tag Manager Server Container setup

Test webhook via the sGTM

Parameters and their Values

Let’s get started 🚀

Install Tagging Pixel in your Shopify Website



To start, you need to install a tagging pixel on your Shopify website. The pixel is the foundation for collecting accurate and actionable data about user behavior and purchases.

Once installed, it’s essential to verify that the pixel is working correctly to ensure reliable data collection. This verification step helps avoid any tracking errors and ensures the smooth functioning of your analytics setup.



Google Tag Manager Web Container setup



The GTM Web Container helps manage your website tracking in one place. In this section, you’ll configure data variables, GA4 tags, and triggers.

Create Data Variables



To track user activity effectively on your site, you need to add DataLayer variables from the “trytagging_user_data” event to your Google Tag Manager (GTM) web container. These variables allow you to capture key data points that are essential for analyzing user interactions and behavior.

Create variable for user_id



Create variable for session_id



Create variable for session_count



Create GA4 Tag in Web Container



After adding the DataLayer variables to your GTM container, the next step is to create a GA4 tag. This tag is crucial for sending user data to Google Analytics 4

When creating the GA4 Tag, it is essential to include specific parameters to ensure accurate tracking and smooth functionality. The key parameters to include are send_page_view, first_party_collection, and server_container_url. These parameters optimize the tag for accurate tracking, efficient data processing, and server-side operations.

Sr.noParameterValue
1first_party_collectiontrue
2send_page_viewtrue
3server_container_urltagging.mydomain.com


For more information on how to create GA4 tag, and what parameters are required for GA4 tag, refer to the documentation on Create GA4 tag

After adding the required parameters, add the parameters to your GA4 Tag based on the type of data you want to track in your shopify store. For detailed information about the parameters and their values, refer to the tables Parameters and their Values

For this demonstration we have added the below 3 parameters to our GA4 tag

S.noParameterValueExplantation
1user_id{{marketing.user_id}}Unique user identifier for the user.
2session_id{{marketing.session_id}}Unique identifier for the current user session.
3session_count{{Marketing.session_count}}Counts the number of sessions for a specific user


Parameters and their Values

Setup Trigger for Google Analytics Tag



Triggers control when a tag fires. Instead of firing the tag on every page, set it to fire only on a specific event, such as trytagging_user_data. This prevents unnecessary data from being collected.

For more information on how to create trigger that fire only with the custom events, refer to the documentation for setup up trigger

trytagging_user_data Trigger

Once you have created the trigger for your GA4 tag in the Google Tag Manager (GTM) web container, the next step is to set up the GTM server container.

Google Tag Manager Server Container setup



The GTM Server Container processes data on the server instead of the browser. This helps reduce errors and keeps data secure.

Create Data variables in GTM Server Container



In the server container, set up variables to capture key data points similar to those in the web container. These variables ensure that the data collected on the server aligns with the data from the web container, maintaining consistency.

This setup helps in accurate data processing and avoids discrepancies between client-side and server-side tracking.

Create variable for user_id



Create variable for session_id



Create variable for session_count



Create GA4 tag in Server Container



Set up a GA4 tag to handle server-side data. This tag processes the data and sends it to Google Analytics, ensuring accurate tracking even when browser-based tracking fails.



Setup GA4 Trigger



Configure a trigger to fire the GA4 tag only when specific conditions are met. Exclude the purchase event from this trigger since purchases will be tracked using webhooks. This prevents duplicate tracking.

Before setup GA4 Trigger you need to create a transformation and attach your GA4 tag to the transformation. For more information how to create a transformation, refer to the documentation on Create a Transformation

Ensure the purchase event is excluded from the GA4 trigger, as purchase events are now sent via Webhooks. Failing to exclude it may result in duplicate data being recorded.



Create a Transformation



A transformation is used to organize raw data into a structure that can be processed by the GA4 tag. This ensures that the data is formatted properly and integrates smoothly into your analytics system. Without a transformation, raw data might not align with the expectations of your GA4 tag, leading to inconsistencies in your tracking.



Set up Data Client



A Data Client is a key component that connects your GTM server container to your analytics setup. It acts as a bridge, ensuring that the data collected on the server flows correctly into your analytics tools. Setting this up is essential for handling server-side data efficiently.

To create a Data Client, we first need to download it and add the data client template to your server container. After that we can create a client. To create the Dataclient we use the Stape Data Client. You can download it here. https://github.com/stape-io/data-client

Add “Data Client” Template



To begin, download the Data Client template and add it to your GTM server container. The template simplifies the process of managing server-side data by providing predefined configurations.



Create Data Client



After adding the Data Client template, create a Data Client in your server container. This step ensures that your server container is ready to process incoming data and send it to your analytics platform. The Data Client manages the flow of data from the server to your GA4 tags or other destinations.



Create Event Data Variable for Purchase Webhook data



Webhooks are used to capture specific events, such as purchases. To make the most of webhook data, you need to create variables that extract and store important details. For example, in the demonstration below, a variable for “Facebook Ads” is created. You can follow a similar process to create variables for other data points based on your needs.

For more details on the variables you can create, refer to the section “Parameters and their Values.”



Create Source name Data Variable



Set up a variable to specify the source of your data, which in this case should be set to web. This variable helps identify that the data is coming from the web container, ensuring accurate tracking. It is particularly important when setting up the Webhook Purchase Trigger, as it ensures the trigger activates only for events originating from the web.



Create a GA4 Purchase Tag



The GA4 Purchase Tag is used to send purchase data to Google Analytics. It includes variables like user_id, session_id, and session_count to associate purchases with specific users and sessions. The tag is configured to process webhook data using the event variables set up earlier, ensuring accurate tracking of purchase events.

For information about event variables, refer to the section “Parameters and their Values”



Create GA4 Purchase Trigger



To ensure that the GA4 Purchase Tag fires at the right time, set up a trigger. Configure this trigger to activate only when a purchase webhook event is received. This step ensures that purchase data is tracked accurately without any unnecessary or duplicate tracking.



Test webhook via the sGTM



Alright, now that we've set up the Shopify webhook in the sGTM container, it's time to test it. You can either process live orders or place an order yourself to ensure everything is working correctly. This helps verify that the webhook is capturing and transmitting data as expected.

For more information, refer to this document Testing webhooks and live preview mode via your sGTM

Parameters and their Values



Here’s a detailed list of parameters you can add to your Google Tag based on the type of data you want to track.

S.noParameterValueTypeExplanation
1ga{{marketing.ga}}Event DataGoogle Analytics client ID used for tracking users.
2fbc{{marketing.fbc}}Event DataFacebook Click ID for Facebook Ads tracking.
3fbp{{marketing.fbp}}Event DataFacebook Pixel ID for tracking user events.
4ttp{{marketing.ttp}}Event DataTikTok Pixel ID for tracking user events.
5gclid{{marketing.gclid}}Event DataGoogle Ads Click ID used to attribute ad clicks.
6gbraid{{marketing.gclid}}Event DataGoogle Ads ID for app campaigns.
7ttclid{{marketing.ttclid}}Event DataTikTok Click ID for ad campaigns.
8wbraid{{marketing.wbraid}}Event DataGoogle Ads Click ID for web campaigns
9user_id{{marketing.user_id}}Event DataUnique user identifier for the user.
10hostname{{marketing.hostname}}Event DataDomain name of the user's current session
11referrer{{marketing.referrer}}Event DataThe referring URL from which the user arrived.
12client_id{{marketing.client_id}}Event DataGoogle Analytics client ID for identifying users.
13session_id{{marketing.session_id}}Event DataUnique identifier for the current user session.
14user_agent{{marketing.user_agent}}Event DataDetails about the user's browser and device.
15utm_medium{{marketing.utm_medium}}Event DataThe medium used in UTM tracking (e.g., email, CPC).
16utm_source{{marketing.utm_source}Event DataThe source used in UTM tracking (e.g., Google, newsletter).
17session_count{{Marketing.session_count}}Event DataCounts the number of sessions for a specific user.

Updated on: 06/02/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!