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.

What is an 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 help of cookies. Say, you want to promote someone’s products which are available on Amazon. You sign up for 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.

Google Tag Manager Course - Learn More

#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. Image 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.

Redirection plugin - Link groups

Then, create two redirections. One of them is displayed below (while the other one is https://www.yourwebsite.com/aff/another-product):

Redirection plugin - Create a Redirect

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.

Google tag manager lookup table

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:

  1. Takes the URL of a clicked link (variable {{Click URL}})
  2. Checks it against two affiliate links I’ve entered (one for Amazon and another one for eBay).
  3. 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.

Regex Table - Affiliate Link

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):

Click Trigger - Contains Rule

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.

Click Trigger - Regex Table


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:

GA Event - Affiliate Link Click

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.



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.

Tag - Affiliate Link Click in Google Tag Manager

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 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.

Julius Fedorovicius

Head of Developer Operations at Omnisend. I am highly interested in Google Tag Manager, Google Analytics, Adwords, Email marketing, Email Deliverability, Digital Marketing in general. You can follow me on Twitter or Linkedin.