
Track Affiliate Sales with GTM and GA4 (2025 Guide)
Have you ever wondered how to track affiliate sales accurately?
With so many affiliates promoting your products, it’s not enough to just count clicks. You also need to know which partners are actually driving actual purchases and revenue.
That’s where Affiliate sales tracking with GTM and GA4 comes in. Together, they enable you to capture outbound affiliate clicks, connect them with completed purchases, and generate clear reports within Google Analytics 4.
In this guide, we’ll cover what affiliate sales tracking GA4 is, why it’s important for businesses, and step-by-step instructions on how to set up GTM and GA4 for tracking affiliate clicks, conversions, and sales performance.
Let’s get started!
Track Affiliate Sales with Google Tag Manager (TOC)
What is Affiliate Sales Tracking
Affiliate Sales Tracking is the process of monitoring and recording when a customer makes a purchase through a special link provided to an affiliate (known as a unique referral link). This link differs for each affiliate and indicates to the business which affiliate referred the customer.
Example: Suppose a blogger promotes a shoe brand. The brand gives the blogger a unique link like: www.shoestore.com?ref=John123
If a customer clicks this link and buys shoes, the tracking system records the sale and credits the blogger (John) with a commission.
This system records key details like the referral source, clicks, sales amount, and commissions. It helps businesses reward affiliates accurately and ensures transparency in performance-based marketing.
Click Tracking monitors when a user clicks an affiliate link. This indicates the amount of traffic affiliates are driving, but doesn’t confirm whether sales have occurred. Conversion Tracking goes a step further to measure when a user completes a desired action (like a purchase). It directly connects sales and revenue to specific affiliates.
GTM simplifies affiliate tracking by capturing clicks and events, while GA4 organizes this data into clear reports for analyzing affiliate performance.
How to Set Up Affiliate Sales with GTM and GA4 (Step by Step)
Below is a complete guide you can follow right now to set up affiliate sales with GTM and GA4:
Step 1: Capture the Affiliate ID from the Link
When someone visits your site through an affiliate link (for example, affiliate_id=John123), we need to grab that unique affiliate ID from the URL so we know which affiliate sent the visitor. This step ensures we capture the affiliate ID rights as the visitor arrives. For this:
- In Google Tag Manager, go to Variables >> New.
- The configuration of this variable will be:
- Name it Affiliate ID (URL param) (or {{Affiliate ID}}).
- Variable Type: URL
- Component Type: Query
- Query Key: affiliate_id (or the parameter you use)
- Then save the changes.
Now, {{Affiliate ID}} will return the value if the page URL contains “?affiliate_id=John123”.
Step 2: Save the Affiliate ID for Later (Cookie)
If a user clicks an affiliate link but doesn’t make a purchase immediately, the affiliate ID could be lost when they browse other pages. To keep it, you need to store the ID in a browser cookie (a small memory in the browser).
Later, when the user makes a purchase, GTM reads the cookie to know which affiliate referred them. This step is important because it ensures affiliates still get credit even if the purchase occurs later. Here’s how to set it up:
- In Google Tag Manager, navigate to Triggers and click New.
- Choose Page View as the trigger type.
- Select Some Page Views.
- Add the condition: Page URL contains affiliate_id=.
- Alternatively, you can use a condition that only fires when {{Affiliate ID}} is defined.
- Alternatively, you can use a condition that only fires when {{Affiliate ID}} is defined.
- Name the trigger Affiliate ID Page View and save it.
- Now, the next step is to create a tag. For this, navigate to the Tags section and click New.
- Under Tag Configuration, select Custom HTML as the tag type.
- Name the tag Set Affiliate Cookie.
- Paste the following code inside the tag:
<script>
(function(){
var aff = “{{Affiliate ID}}”;
if (aff && aff !== ‘undefined’) {
var expirationDays = 30;
var d = new Date();
d.setTime(d.getTime() + (expirationDays*24*60*60*1000));
document.cookie = “affiliate_id=” + encodeURIComponent(aff) + “; path=/; expires=” + d.toUTCString() + “; SameSite=Lax”;
}
})();
</script>
- Add the newly created Affiliate ID Page View trigger and connect it with the tag.
- Save the tag and publish your container.
- The created Tag and Trigger only store the affiliate ID in the cookie. Next, create a variable to read the cookie: For this:
- Go to Variables >> New.
- Under variable configuration, select 1st Party Cookie as the variable type.
- Enter Cookie Name: affiliate_id.
- Name the variable Affiliate ID (cookie).
- Then, save all these changes.
Step 3: Track Outbound Affiliate Link Clicks
Outbound affiliate link tracking GTM is optional but helpful. It helps you see how many times people clicked affiliate links on your site. You’ll know which links or affiliates generate the most interest, not just sales. It’s like tracking “click activity” before the actual purchase. Here is how we can set up GTM affiliate link tracking:
- First, we need to create a trigger in GTM. To do this, go to Triggers >> New.
- Name: Affiliate Link Click – Outbound
- Trigger Type: Click – Just Links
- This trigger fires on: Some Link Clicks
- Condition: Click URL matches RegEx affiliate_id=|ref=|aff= (adjust to your link parameter patterns)
- Then, click on the save tab.
- For GTM event tracking for affiliate links, we need to create a GA4 Event Tag to send a click. In GTM, go to Tags >> New.
- Next, choose Google Analytics as the tag type. Then, select Event GA4.
- Tag name: GA4 – affiliate_click
- Configuration Tag: select your GA4 Config tag
- Event Name: affiliate_click
- Event Parameters (examples) along with their values:
- affiliate_id = {{Affiliate ID (final)}}
- Click_url = {{Click URL}}
- link_text = {{Click Text}}
- affiliate_id = {{Affiliate ID (final)}}
- Tag name: GA4 – affiliate_click
- Add Trigger: Affiliate Link Click – Outbound.
- Then, save the tag and publish it.
Step 4: Send Purchase Data, Including Affiliate ID
When someone completes a purchase, we need to send GA4 the full order details, like order ID, total amount, items purchased, and the affiliate ID to track affiliate conversions GA4.
This is important because it connects the affiliate who referred the customer with the actual sale. On the checkout “Thank You” or confirmation page, push a dataLayer event that contains the order details. We’ll reuse the affiliate ID from the cookie here so GA4 knows which affiliate is tied to the purchase. Example (developer adds this):
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: ‘purchase’,
transaction_id: ‘ORDER12345’,
value: 129.99,
currency: ‘USD’,
items: [
{ item_id: ‘sku123’, item_name: ‘Blue Sneakers’, price: 129.99, quantity: 1 }
],
affiliate_id: (function(){
var m = document.cookie.match(/(?:^|; )affiliate_id=([^;]+)/);
return m? decodeURIComponent(m[1]) : null;
})()
});
</script>
Note:
- Ensure transaction_id is unique per purchase.
Step 5: Create Data Layer Variables in GTM to Read Purchase Values
After pushing purchase details into the data layer, you need to create variables in Google Tag Manager so that GA4 can read them.
- In Google Tag Manager, go to Variables.
- Click on New and select Variable Configuration.
- Choose a Data Layer Variable.
- Create the following variables one by one:
- DLV – transaction_id: Set the Data Layer Variable Name as transaction_id.
- DLV – value: Set the Data Layer Variable Name as value.
- DLV – currency: Set the Data Layer Variable Name as currency.
- DLV – items: Set the Data Layer Variable Name as items.
- DLV – affiliate_id: Set the Data Layer Variable Name as affiliate_id.
- Save each variable after configuration.
These variables will now be available for your GA4 event tag setup.
Step 6: Creating a GA4 Event Tag in GTM to Send the Purchase
Once your dataLayer variables are set, you need to create a GA4 Event Tag to send purchase data, including the affiliate ID, to Google Analytics 4 (GA4). Follow these steps:
- For Triggering, select Custom Event and set the Event Name to purchase (this should match your dataLayer push).
- Alternatively, you can use a Page View trigger on the confirmation page if your site does not use dataLayer pushes.
- Next, to create a GA4 Event tag in Google Tag Manager, go to Tags and click New.
- Under Tag Configuration, select Google Analytics: GA4 Event.
- In the Configuration Tag dropdown, choose your existing GA4 Configuration Tag.
- Enter the Event Name as purchase. (You can also use a custom name like affiliate_purchase if you want to separate affiliate sales.)
- Under Event Parameters, add the following:
- transaction_id mapped to {{DLV – transaction_id}}
- value mapped to {{DLV – value}}
- currency mapped to {{DLV – currency}}
- items mapped to {{DLV – items}}
- affiliate_id mapped to {{DLV – affiliate_id}} (this one is crucial for tracking affiliates)
- transaction_id mapped to {{DLV – transaction_id}}
- Save the tag.
Step 7: Verify in GTM Preview and GA4 DebugView
- In GTM Preview mode, simulate a purchase flow: land with ?affiliate_id=John123, confirm cookie set, complete checkout, see purchase event in dataLayer, and confirm GA4 Event tag fires.
- In GA4 DebugView, you should see the purchase or affiliate_purchase event, along with the affiliate_id parameter.
Step 8: Custom Dimension Setup (Affiliate ID in GA4)
To make the affiliate ID available in your reports, you need to register it as a custom dimension in GA4. For this, follow these steps:
- Open your Google Analytics 4 property.
- Go to the Admin section in the bottom left corner.
- Under the Property column, click on Custom Definitions.
- Click the button Create Custom Dimension.
- In the setup form, enter the following:
- Name: Affiliate ID
- Scope: Event
- Event parameter: affiliate_id
- Name: Affiliate ID
- Click Save to complete the setup.
It may take 24 to 48 hours for the data to begin populating in standard reports, but you can view the affiliate ID immediately in Explorations or DebugView.
Step 9: Affiliate Sales Reporting in GA4
Once you have affiliate tracking set up, GA4 enables you to create reports that display how affiliates contribute to clicks, conversions, and revenue. Using the Explore feature, you can create custom reports that visualize affiliate performance by using the Affiliate ID dimension you registered earlier.
Follow the steps given below to create the custom reports and track affiliate conversions GA4:
- Open your GA4 property and go to Explore.
- Select Free Form exploration.
- In the Variables panel, click on the plus sign parallel to Dimensions and Import Affiliate ID (your custom dimension), along with any other useful dimensions, such as Page title or Device category.
- Under Metrics, click the plus sign and add Event count, Conversions, Transactions, and Purchase Revenue.
- Drag Affiliate ID into the Rows section.
- Then, place your chosen metrics in the Values section.
- Apply filters (for example, only purchases) to create a focused report.
This will show affiliate sales performance broken down by each affiliate ID. You can also add filters to focus on purchase events only.
How Analytify Helps Track Affiliate Sales
Join 50,000+ beginners & professionals who use Analytify to simplify their Google Analytics!
Setting up Sales tracking manually in GTM and GA4 can be overwhelming if you’re not a technical expert. That’s where Analytify comes in.
Analytify is the best Google Analytics plugin for WordPress. It seamlessly integrates with Google Analytics, bringing all your essential reports like clicks, conversions, and revenue directly into your WordPress dashboard.
For anyone running campaigns on WordPress, Analytify makes tracking easier, faster, and more beginner-friendly.
Here’s how Analytify helps you track sales effectively:
- Real-Time Stats: See link clicks and conversions as they happen, so you can quickly spot which campaigns are driving traffic right now.
- Referral Traffic Sources: Identify which partners, blogs, or websites are sending you affiliate traffic and how much revenue they generate.
- General Statistics: Get an overview of sessions, page views, and user behavior to see how affiliate traffic compares with other channels.
- UTM Campaign Tracking Addon: Track affiliate promotions more accurately by attaching UTMs to your links, making it easier to measure campaign performance.
- WooCommerce Addon: You can check eCommerce analytics for your online store directly, including product sales, to see which products drive the most revenue.
With Analytify, sales tracking becomes simpler and faster, without coding or manual GTM setup.
Best Practices for Tracking Affiliate Sales Using GTM and GA4
Here are some best practices to follow:
1. Set Up Outbound Affiliate Link Tracking GTM: Use GTM affiliate link tracking to capture outbound clicks.
2. Track Affiliate Conversions GA4: Mark both affiliate events, affiliate_click, and affiliate_purchase as conversions in GA4. Also, include the affiliate_id in every purchase event.
3. Maintain Consistent Event Naming: Keep event names and parameters consistent between GTM and GA4. Clean naming helps avoid confusion and makes reports easier to analyze.
4. Test and Validate Your Setup Regularly: Use GTM preview mode and GA4’s real-time reports to confirm clicks and conversions are being tracked correctly.
5. Segment and Compare Affiliate Performance: Segment affiliates in GA4 reports to compare results, spot top performers, and refine campaigns.
How to Track Affiliate Sales (FAQs)
1. How can the clicks on affiliate links be tracked?
Clicks can be tracked through GTM affiliate link tracking by setting up outbound link triggers. These triggers detect when a user clicks a link containing parameters like ref or affiliate_id. You can then send these events to GA4 for reporting. This method ensures every affiliate link click is recorded accurately.
2. How to track sales in affiliate marketing?
You can track affiliate sales with Google Tag Manager and GA4 by connecting affiliate link clicks with purchase events. First, GTM stores the affiliate ID from the URL or cookie, then sends it along with GA4’s ecommerce purchase events. This enables GA4 ecommerce tracking for affiliates, letting you measure conversions and revenue linked to each affiliate partner.
3. Why is GTM affiliate link tracking important?
GTM affiliate link tracking records outbound link clicks so you know which affiliate links your visitors are using. This ensures accurate attribution when combined with purchase data in GA4, helping you identify which affiliates bring in the most revenue.
4. Can Google Analytics track affiliate links?
Yes, it can. Using Google Analytics 4 affiliate tracking setup, you can monitor both clicks and sales. Using GA4, you can capture affiliate clicks as custom events and mark purchases as conversions. Combined with GTM affiliate link tracking, you get complete affiliate sales reporting in GA4, including clicks, conversions, and revenue for each affiliate partner.
5. How do I use GA4 ecommerce tracking for affiliates?
With GA4 ecommerce tracking for affiliates, you can include affiliate IDs as event parameters in purchase events. This enables GA4 to connect revenue and sales to specific affiliates directly, providing detailed affiliate sales reporting in GA4.
6. What is the best way to set up Google Analytics 4 affiliate tracking?
The best Google Analytics 4 affiliate tracking setup involves capturing affiliate IDs from URLs, storing them in cookies, and passing them into purchase events. This way, you can track affiliate conversions GA4 even if the purchase happens days after the initial click.
7. How does affiliate sales tracking work in GA4?
Affiliate sales tracking GA4 utilizes event data passed from GTM or your website’s data layer. When a purchase occurs, the affiliate ID is included in the event, allowing GA4 to report conversions, revenue, and engagement tied to affiliates.
Final Thoughts: How to Track Affiliate Clicks and Sales
In this guide, we explored how to track affiliate clicks and sales with GTM and GA4, starting with capturing affiliate parameters, storing IDs in cookies, and using outbound affiliate link tracking to monitor clicks.
We then connected purchases by pushing affiliate data into the data layer, creating GTM variables, building GA4 event tags, and registering the affiliate ID as a custom dimension in GA4.
Once the setup is complete, affiliate sales reporting in GA4 can be performed through custom Explore reports, allowing you to analyze metrics such as affiliate click-through rate, conversion rate, and revenue generated per partner.
Alongside the setup steps, we also discussed best practices such as testing regularly, maintaining consistent event names, and marking affiliate events as conversions to ensure accurate reporting.
Overall, combining GTM affiliate link tracking with GA4 ecommerce tracking for affiliates provides clear visibility into affiliate performance, enabling you to reward top partners fairly and optimize campaigns for improved results.
For further guidance, you can read:
- How to Track Affiliate Link Clicks in GA4
- What is Affiliate Marketing?
- Best Strategies for Google Ads Budgeting
Which part of affiliate tracking do you find most challenging? Share your thoughts in the comments below!. I would love to hear from you.