How to set up affiliate click tracking in WordPress With Google Analytics

In this article, I will show you how to track affiliate link clicks in WordPress and set affiliate clicks as goals within Google Analytics.

You can do this by using Google Tag Manager (GTM), diving into code, or using the plug-and-play plugin, I covered all options in this tutorial.

Part 1: affiliate link cloaking

Affiliate links cloaking is the first step you need to do.

OK you don’t have to if you don’t want, but it’s a very good practice.

I can hear you asking what is cloaking and why should I cloak my affiliate links?

There are plenty of reasons for cloaking affiliate links, including protecting affiliate commissions by hiding the affiliate ID.

However, ease of management and easier click tracking is why I recommend it and why you need to do it to complete this tutorial.

I am going to show you three ways to cloak affiliate links, first one is more technical but according to Yoast, it doesn’t impact page load speed, which is good for the UX. Also, this method can be utilized on non-WordPress sites as well.

Second way is via Redirection plugin, so this is only applicable to WordPress.

Third way to cloak affiliate links is by using a ThirstyAffiliates plugin. This also applies only to WordPress sites, but advantage over the first method is that setting it up doesn’t require any technical knowledge. Additionally, managing affiliate links using WP plugin is much easier.

Cloak affiliate links using Yoast solution

Yoast is one of the SEO industry giants and their products are widely used, especially on WordPress powered sites. No wonder they offered a solution to cloak affiliate links as well. Check out their guide, follow the three steps they outlined and download the files needed to cloak affiliate links. You will find a download link at the bottom of their guide.

One thing I would recommend doing differently, is to name the folder you’ll serve your redirects from something other than ‘out’.

Reason for that is the fact we will use the name of the folder to detect affiliate links and track them easily. So it is better to have a bit more unique folder name such as ‘recommends’ or ‘endorses’.

After you decide your affiliate link folder name, place the Yoast files in there. You only need to modify the redirects.txt file. Place your homepage URL on the first line as the default value:


After that first line, just add the pairs of affiliate name and affiliate URL, one per line, separated by the comma, no white spaces, just like this:


This ultimately means that you can use one of those two affiliate links on your pages like this (if you named your affiliate link folder ‘recommends’):

That link will redirect a user to:

Easy, right? Not only this is much nicer to read, it also allows you to manage affiliates easier, in one place, and on top of all, the script adds the rel=”nofollow” attribute to all of those affiliate links.

We can achieve all of that in WordPress by using the plugin instead of tweaking everything manually, so if you are not comfortable with setting up the Yoast solution, check out other options below. Otherwise, scroll down to part 2 of this guide.

Cloak affiliate links using Redirection plugin

Chances are, you are probably using one redirection plugin already to fix 404 errors on your website, so you probably don’t even need to install an extra plugin.

In case you do, I recommend Redirection plugin as it’s easy to use and I’m used to it already.

The idea is the same as the one we discussed above we Yoast solution, but it’s easier as you don’t have to change any files on server directly.

In fact, you don’t even need server access for this to work. The idea is similar to the above described solution, it’s important to “group” the links so we can easily target them later. On the image below I used the same /recommends approach.

Cloak affiliate links using ThirstyAffiliates plugin

Third solution to cloak affiliate links is using the ThirstyAffiliates Affiliate Link Manager plugin

It has plenty of features in the free plan and it is easy to use.

It gives you the possibility to pick default link insertion type, which is a feature that was really important for me while I was testing it. Other plugin I tried offered only link insertion via shortcode which caused me issues when I tried to insert affiliate link within another shortcode.

You can also choose to purchase a premium license that gives you fantastic additional features such as automatic keyword linking. This means you can automatically link affiliate links to given keywords throughout your content!

That is a great way to monetize your past content, but also user generated content such as comments or forum posts.

Other benefits include advanced statistics reports, CSV import/export tool for your affiliate links and Google Analytics click event pushing. Which means if you go for the premium version of the plugin, you don’t have to follow this tutorial any more! Get it here.
Using ThirstyAffiliates premium is a simpler way to track affiliate link clicks but if you want to get your hands dirty, continue to part 2 of this guide.

Part 2: Send affiliate click events to Google Analytics

We can send affiliate click events to Google Analytics either via placing a custom JavaScript code snippet in the footer of each WordPress page or we can use Google Tag Manager to do it for us. Either way is fine, using GTM is definitely easier but some folks are just not comfortable with GTM.

Using JavaScript to detect affiliate link clicks

If you’re comfortable with JavaScript let’s setup affiliate click detection without GTM.

By using JavaScript code snippet, we can accomplish that and send information to Google Analytics. In step 3, I will show you how to set those events as goals in GA.

But first, here’s the JavaScript snippet that allows us to detect affiliate link clicks. As a bonus, I added also other outbound link clicking detection as that information could be useful too.

 var a = document.getElementsByTagName('a');
  for(i = 0; i < a.length; i++){ if(a[i].href.indexOf('recommends') >= 0){
    a[i].onclick = function(){
     var affiliate = this.href.split('/recommends/');
     ga('send', 'event', 'Affiliate link', affiliate[1]);
  else if(a[i].href.indexOf( == -1){
   a[i].onclick = function(){
    var url = this.href.split('://');
    ga('send', 'event', 'Outbound link', url[1]);

You need to place this code to the footer on every page of your website, just before the closing body tag: </body>

This is an easy task to do if your site is on WordPress platform. In that case we can take advantage of the wp_footer action. So we can modify the above code just for WordPress like this:

function learnedia_event_tracking(){ ?>
 var a = document.getElementsByTagName('a');
  for(i = 0; i < a.length; i++){ if(a[i].href.indexOf('recommends') >= 0){
    a[i].onclick = function(){
     var affiliate = this.href.split('/recommends/');
     ga('send', 'event', 'Affiliate link', affiliate[1]);
  else if(a[i].href.indexOf( == -1){
   a[i].onclick = function(){
    var url = this.href.split('://');
    ga('send', 'event', 'Outbound link', url[1]);
<?php }
add_action('wp_footer', 'learnedia_event_tracking');

Paste the code above to the functions.php file of your active WordPress theme. Easy, right?
Syntax errors in the PHP code can potentially break your site. If you are not comfortable in PHP, I recommend these easy to follow beginners PHP courses on Pluralsight:

PHP fundamentals and Getting started with PHP

Code explanation

The JavaScript code above checks all the links on the page, whether the value of the href attribute has the ‘/recommends/’ part in it. If it does, it means that is actually an affiliate link we want to track. So every time a user clicks on such a link, we send that as an event to Google Analytics. Neat, huh?

Google Analytics will record that click as an event with ‘Affiliate link’ as an Event category and whatever is left in the link URI after the ‘recommends’ part as an Event action. There is also an opportunity to send some extra bit of information as an Event label if you need it. This is the function that is used to send the data above to the GA

ga('send', 'event', 'Affiliate link', affiliate[1]);

The third parameter above is a string that will show up as Event category in Google Analytics reports. The last parameter is what we pass as an Event action. This is whatever is left after the part od the URL. This way, you can identify which exact affiliate link the user clicked.

For more information on how that works, visit this Google Developers resource on Event tracking

If you don’t have GA installed on your website or if you are not experienced in Google Analytics enough to gain actionable insights from GA reports, I can highly recommend this fantastic Pluralsight Google Analytics course.

Track other outbound link clicks with Google Analytics

As a nice bonus feature, I added part of the code that tracks clicks on all other outbound link clicks on your website. This allows you to see which links are appealing to your users and what is it that they find interesting.
This is done via the following part of the above code:

else if(a[i].href.indexOf( == -1){
 a[i].onclick = function(){
  var url = this.href.split('://');
  ga('send', 'event', 'Outbound link', url[1]);

We are checking if the href attribute of the clicked link contains your domain. If not, new GA event is sent with ‘Outbound link’ as an Event category and URL (without http(s):// part) as an Event action.

If you installed Google Analytics via plugin, such as MonsterInsights, you might need to tweak my code a bit.

For example, MonsterInsights uses a different function to send pageviews to GA, and that function should also be changed when you are recording click events. So in case you are using MonsterInsights, don’t forget to make the changes in the two lines of JavaScript code that is actually sending data to GA like this:

__gaTracker('send', 'event', 'Affiliate link', affiliate[1]);
__gaTracker('send', 'event', 'Outbound link', url[1]);

In the nutshell, change the function name from ga to __gaTracker and it should work 🙂

If you are using some other plugin, just check the source code of your website and find the function that sends the pageviews to GA.

It looks like this when Analytics is installed with MonsterInsights plugin:


Or like this, if you installed GA manually:

ga('send', 'pageview');

Using GTM to detect affiliate link clicks

Let’s see now how to implement affiliate link click tracking with Google Tag Manager.

If you already used vanilla JS approach, you can skip this part and go straight to part 3.

We need to define a custom GTM variable, trigger, and a tag.

First we define a variable linkClickPath that captures the path of the Element URL

Next, let’s define the trigger affiliateLinkClick:

We are listening for link clicks that have the /recommends/ part in the ELement URL and this is where we utilize the variable created in the previous step.

Finally, let’s create a tag that sends the affiliate link click event to Google Analytics:

This time I’ve used the page path from which the affiliate link was clicked as the Event action and our custom variable showing us which affiliate link was clicked as the Event label, but you can change that to whatever makes sense to you.

If you also want to track other outbound link clicks via GTM, you can follow the steps in this tutorial.

Part 3: Setting up affiliate link clicks as goals in Google Analytics

Finally, as a last step, we can use events with the Evnte category of Affiliate link as goals in Google Analytics. This is very easy to do. Just navigate to the Administration area of your Google analytics account. Click on “Goals” in the View section of the Administration area.

Admin area Google Analytics

Create new goal by clicking +New goal button. There are three steps to finish goal setup, first pick the Custom goal option like it is shown on the image below.

GA goal setup step 1

On the next step, enter goal description and pick a goal type: Event.

GA goal step 2

Finally, choose an event condition that triggers the goal completion. In our case, goal is completed when a user clicks on the affiliate link. That occurs when event with Event category containing Affiliate keyword is sent to Google analytics. So we we pick just that like it is shown on the image below. When you are done, click Save and that’s it.

GA goal step 3


There you go, tap yourself on the back, you successfully cloaked your affiliate links, you’ve set up click tracking and you are able to analyze what happens on your website better, by analyzing affiliate and other outbound link clicks.

Hope you liked this tutorial, if you did please share with others on social media. If you need some extra help, please comment below or contact us.

1 thought on “How to set up affiliate click tracking in WordPress With Google Analytics”

  1. Thank you so much, the article is very good and useful for me and other readers, my site is better because this plugin is super fast.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.