
November 11, 2017
Track Affiliate Links with Google Tag Manager
If you run a blog, you probably want to monetize it, right? In order to do that you might have to place special links in articles which redirect prospects to various landing pages. If visitors purchase anything there, you get a commission. Naturally, you wish to track how your readers interact with your content and, more importantly, whether they click affiliate links.
By collecting some data you’ll be able to see which content attracts more clicks and which one should be improved. In this blog post, I’ll show you how to track affiliate links with Google Tag Manager and send this data to Google Analytics.
Note: this blog post teaches how to track events with Universal Analytics. If you want to get familiar with event tracking in Google Analytics 4, you can also refer to this blog post.

What is affiliate marketing?
Affiliate marketing is one of the oldest forms of marketing wherein you refer someone to any online product and when that person buys the product based on your recommendation, you receive a commission.
How is it possible to track who gets the commission? Usually, this works with the help of cookies. Say, you want to promote someone’s products which are available on Amazon. You sign up for the Amazon Affiliate Program, get your special Affiliate ID and add it to every amazon.com link you want to promote on your blog.
On your blog, when a visitor clicks any Amazon link with that special Affiliate ID (e.g. amazon.com/?ref=1234567), he/she lands on Amazon page which stores a special cookie with that ID. When the visitor makes a purchase, Amazon checks the Affiliate cookie and attributes that sale to you, thus you get a reward.
I won’t dive deeper into the world of affiliate marketing as it is not the purpose of this blog. But if you insist, here’s a comprehensive beginner’s guide I’ve found online.
Identifying affiliate links on your blog/website
One of the reasons why I see people starting to use Google Tag Manager is the ability to easily track clicks. Actually, I’m not surprised, it’s a built-in GTM feature which is fairly easy to configure.
You’ve probably set up tracking of outbound link clicks too, haven’t you?
But just like in every other GTM implementation, you can’t just track all clicks, you need to distinguish only important ones (in this case, affiliate links).
Unfortunately, there is no “global standard of affiliate links”, therefore you’ll end up having a variety of links with different structures, for example:
- https://www.example.com/?affiliateid=ao2b2o39
- https://www.anotherwebsite.com/?ref=123
The more partners you work with, the more different affiliate link structures you’ll have to support, therefore trigger conditions might become longer and more complex. To make things a bit clearer and clean-cut, I use one of two options.
Do not be limited by my ideas. If you feel comfortable with a bit different approach, that’s fine. All I want to do is share something from my experience.

#1. Unify all different affiliate links under the same link structure
If you’re using WordPress, I recommend a plugin called Redirection. Not only does it track 404 errors which you can then easily turn into correct redirects, but also it enables to create Link Groups. Imagine that you want to promote links of two partners, Amazon and eBay.
Amazon’s affiliate links might look like https://www.amazon.com/awesome-product?affiliate=123456, while eBay’s might be https://www.ebay.com/another-product?r=12358372.
In Redirection plugin, create a new group called Affiliate Links.
Then, create two redirections. One of them is displayed below (while the other one is https://www.yourwebsite.com/aff/another-product):
Source URL is the web address that I could use on my blog and mention it in relevant blog posts, while Target URL is the final destination of where the user should be redirected. Why did I do this? There are two main reasons:
- Affiliate links with my blog’s web address look a bit prettier as in many cases affiliate links are really long.
- I added an additional subdirectory /aff/ in all of the affiliate links, therefore, it will be much easier for me to create a trigger.
If you’re not using a WordPress, check whether your content management system (CMS) has some similar functionality. If not, continue reading, I have an alternative for you.
#2. Regex Table for all affiliate links
In this case, all affiliate links that will be added to your blog/website will not be as pretty as in the aforementioned chapter. Nevertheless, you can still track them and keep things tidy. I recommend using a Regex Table variable.
Just like with Lookup Tables, Regex Tables enable you to have multiple lines of conditions and each condition could return a different value.
But unlike Lookup tables, Regex tables don’t require you to enter exact affiliate links. Instead, you can enter conditions with the operator contains. Let me illustrate it with an example. In the screenshot below, I’ve created a Regex Table which:
- Takes the URL of a clicked link (variable {{Click URL}})
- Checks it against two affiliate links I’ve entered (one for Amazon and another one for eBay).
- If {{Click URL}} contains either (1) www.amazon.com […] ?affiliate=123456 or (2) www.ebay.com […] ?r=12358372, the Regex Table variable will return true, otherwise false. This variable will be needed in the upcoming chapter.
After reading all of this, you might ask, why didn’t I use a regular Click Trigger with the condition Click URL Matches Regex (www\.amazon\.com.*\?affiliate\=123456)|(www\.ebay\.com.*\?r\=12358372)?
In fact, I could do that and it would work just fine. Unfortunately, long Regular Expressions displayed in one line are really hard to read, that’s why I choose to keep them in a more readable format, table.
Affiliate link click trigger in Google Tag Manager
The 2nd step of tracking affiliate links with Google Tag Manager is a trigger. We need to define a precise rule when the Google Analytics Event Tag should fire. Fortunately, this will be easy because the previous chapter of this blog post set a solid foundation.
If you’re using a Redirection (or any other similar) plugin, just create a trigger with the following settings (keep in mind that your contains rule look be different):
If you chose to go with a Regex Table, create a Link Click Trigger with a bit different rule: if Regex Table Variable returns true then the clicked link is an affiliate one.
Send affiliate link clicks to Google Analytics
And the final step, let’s send the data to Google Analytics. In Google Tag Manager, go to Tags and create a Universal Analytics event tag with the following settings:
Every time a visitor clicks an affiliate link, a Google Analytics event will be sent with 3 attributes:
- Category (its value is always Affiliate Link Click)
- Action (its value is Clicked: [the URL of a clicked link])
- Label (its value is Page: [the URL of the page where the click occurred]).
Also, don’t forget to assign the Click trigger you’ve created in the previous chapter.
Test
After you complete all 3 steps mentioned in this blog post, enable the Preview and Debug mode, click any affiliate link on your website/blog and then check whether the GA tag was fired.
Also, don’t forget to check Google Analytics Real-time event reports. If the event is displayed there correctly, congratulations, you’ve succeeded with this little tutorial!
Track affiliate links with Google Tag Manager: final words
In this blog post, I’ve explained some quick tips on how to track affiliate links with Google Tag Manager and send this data as events to Google Analytics.
By implementing this solution you’ll be able to see which affiliate links are clicked the most and which content drives the most affiliates-related engagement. If your partner offers some sort of affiliate reports, you will be able to compare their data with yours.
There are many ways how you could distinguish affiliate link click from other random clicks, but I prefer two options:
- Unify all affiliate links and make them look a bit more similar (with help of plugins or CMS’ default features)
- Use Regex Table to keep them all in one place.
Got anything to add regarding affiliate links and Google Tag Manager? Feel free to share your thoughts in the comments below.

29 COMMENTS
This is a great article. I love how straight to the point it is. To go a step further, how do you track this in a web form? How can you track which affiliate the "quote form" came from?
That is a good question and I'll bookmark it. Sounds like an idea for another blog post. It's not that easy to explain it via comment.
Hey again,
Here's a blog post I've written which will help you track which affiliates drive the most form submissions. Just prefill the affiliate ID in the hidden form field.
Although the blog post is talking about the UTM parameters, replace them with an affiliateID and you should be fine.
Cheers
Julius
Hi,
in the chapter "AFFILIATE LINK CLICK TRIGGER IN GOOGLE TAG MANAGER >> If you’re using a Redirection" :
- in "Page Path" you set : start with --> /post/
Which is "/post/" ? In your url example, there is never post. Your basic url before redirect is yourwebsite.com/aff/. So why /post/ ?
Hey, /post/ means that I'll be tracking only those links which are added to blog posts. All my blog posts have /post/ in their URL.
Since I'm using "Wait for tags" and it waits for 2 seconds after the click, I don't want to this trigger to fire on other pages, all I need is just blog posts.
If you wish, you can set another condition:
- Page URL "matches regex" .*
In that case, the affiliate link trigger will be working on all pages.
Hi Julius !
Ok i see :) Yes, i want it for all my pages, so i don't need to add /post/.
So, you tell me to set :
Page Url > equals to > matches regex .*
Is it the good way ?
sorry i wanted to tell :
Page Url > Matches Regex > .*
is it right ?
Yes, you are correct. Page Url > Matches Regex > .*
Dear sir hello ,My problem is how can I put affiliate web link into webmaster console or other search engine otherwise that web link host by website owner ? Dear master please reply me,i want direct submit not use any blog or site
Sorry, this question is not related to GTM, GA, or another similar topic. By the way, Google's search engine isn't that stupid just to easily accept affiliate links. You need to work harder, create content and use affiliate links there, instead of spamming searches with your links.
OK sir but what "cloudflair" use on blog ,who completely host by Google blogspot,if yes so how it is use ?
This is very helpful and well-written, thank you. Could you consider adding a screenshot towards the end of the post showing where in Google Analytics these events/goals/triggers would be displayed? Thank you once again.
Julius, thank you for your helpful article!
We plan to build a Seller Price Comparison website (like Shopmania.com) but for a niche market. So (as you said in a related article) we are the affiliate and we promote someone else’s products and just want to track all the outbound affiliate link clicks.
OK, but those affiliate links must be protected against click fraud. We are searching and searching for a solution to protect our outbound links, but all that we found so far (like ClickGUARD, Improvely, PPC Protect etc etc) are solutions for protecting only the AdWords campaigns.
Could you recommend us a solution for protecting our outbound affiliate links against click fraud?
Thank you
Valdinia
Sorry, I don't have experience with detecting click fraud.
Thanks for the article.
Can we implement Commission junction through GTM?
Hey, could you elaborate a bit?
Double-check your #1 suggestion about redirecting Amazon links. I've been an Amazon Associate for over 3 years now, and I'm pretty sure that obscuring an Amazon URL in any way (such that it's not visually clear that it's a link to Amazon) is a violation to their program policies, as outlined here:
(do a page search for 'redirect')
https://affiliate-program.amazon.com/help/operating/policies
I've seen other bloggers have their accounts permanently shut down for redirecting Amazon links.
Of course, Amazon's policies are difficult to understand, so it's possible I'm wrong or somehow misinterpreting the rule here. Just to be safe, I stay away from redirecting or changing Amazon links, because once they ban you, you can't get back in or appeal.
Thank you for your great content, I learned invaluable tips from it.
I have a question, and it's this:
Assume you want to start a startup in a developing country which their websites are not professional and affiliate networks don't exist there yet. And you want to launch a cashback website there, how you can track outgoing traffic to different websites with different links?
How you can make sure that they bought what you offered them and whether if they're qualified to take cash back or not?
In other words, Does Google tag manager can be a scalable solution for this kind of startup, or the target websites should have their affiliate tracking system so you can join them with API, etc. ?
Hi Julius
Thanks for sharing this great article.
Yet, I have a question which would take this a step further.
What if I have links to different affiliate offers on my blog and I want to append a variable to the affiliate links during the outbound "click event".
For example,
1. I get traffic from a partner site and generate a custom variable: var_id=asdf
2. i have several affiliate links on the site from different programs. On oubound
- afflink.foo?subid=
- afflink2.foo?clickid=
- linkaff.foo?sub1=
3. During the click event, i'd like to append the var_id value to the affiliate link
NB: the var_id is in the data layer, not in the landing page URL.
Since i work with several affiliate programs, and each has a different querystring parameter where i can insert a custom var.
Any help would be very appreciated!
Thanks
Thanks, if I understand correctly this is tracking clicks but how do you track conversions? I have an affiliate code i.e. I promote other people's products/services but there is no postback (not sure of the term). I see easily how to track number of clicks but not conversion. Is there anyway of doing this? I understand how this can be done if the other party ie. Amazon postback the results but I am working with small partners who don't have those features. Thanks.
amazon doesn’t have a postback
Hi Julius, awesome article with lots of practical guidance. I'd love to ask what do you use when you want to track conversion rate from Google Adwords campaign, since the merchant's website doesn't allow us to put the tracking snippet in theirs sites?
You can use Google Analytics conversions and share them with Google Ads.
Hi Julius,
How would go about tracking actual revenue and sales in affiliate platforms like Amazon?
The issue I have is that I do track clicks (like you explain with this method) but after that I do not know which one my outbound clicks actually convert on the sites of my affiliate partners. Therefore, it makes it difficult to buy traffic and optimize based on ROI. I can, of course, take the average revenue I get per outbound click and use that as reference but ROI tends to vary greatly from one source to another.
If we take the case of Amazon, something I was considering was to create multiple tracking IDs (e.g. xxxxxfbads-01, xxxxxgoogleppc-02..etc.) and dynamically insert those in the outbound clicks URLs based on the utm_source value of the user.
Would that be possible with GTM?
Thanks,
Kevin
I just want to stop by and thank you for the article. I was able to add custom events through GTM and that's awesome (the site was submit in the form, don't threat like a spam =)
The only thing is not very clear is "Wait for Tags". Initially I wasn't able to set this, since my articles doesn't have any specific prefix. Looking through the comments shed some light on this, however I'm still haven't got an understanding why should I use the delay?
Hi Julio, first thank for this content.
i did not understand the propose to unify Aff links, in my case I went to distinguish between my Aff platforms.
this is because the landing page (product) is on my website, and just the checkout page is on Affiliate link.
so i create one variable for each Aff. Platform and set them into a variable. So i trigger the {URL click} ig the url contains the variable.
and at least i write this on GA.
its right this sequence?
This is a great article. I love how straight to the point it is. To go a step further, how do you track this in a web form?
Hi Julius,
I have tracked all the Affliate outbound clicks. But how do I see what conversions took place in real time in the 3rd party website? Should I necessarily have to onboard a Referral software etc.
In my use case, we have a sole partnership with a company (3rd party), and we are doing this only for them. Which is why we do not have an affliate software onboarded yet
If you want to track something, you either need to have your tracking code on a 3rd party website or they somehow must send data back to you with referral software