Wordpress

Affiliate click tracking in WordPress via Google Analytics – The ultimate guide

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 getting your hands dirty or via plugin, I covered both options. Enjoy!

Part 1: Cloak your affiliate links

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

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 two 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 to cloak affiliate links is by using a WordPress plugin. This only applies to WordPress, 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:

default,http://your-domain.com

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:

affiliate-name,http://first-affiliate-link.com
another-affiliate,http://second-affiliate-link.com

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

http://your-domain.com/recommends/another-affiliate

That link will redirect a user to:

http://second-affiliate-link.com

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 the plugin below. Otherwise, scroll down to part 2 of this guide.

Cloak affiliate links using ThirstyAffiliates plugin

So if you are not comfortable outside of the WP dashboard, and couldn’t set up Yoast solution above, you can choose to install the plugin. I recommend 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: Using JavaScript to detect affiliate links and send onclick events to Google Analytics

Next thing we need to do is to detect when somebody clicks on the affiliate link. By using some JavaScript code, we can accomplish that and send that information to the 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.

<script>
 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(location.host) == -1){
   a[i].onclick = function(){
    var url = this.href.split('://');
    ga('send', 'event', 'Outbound link', url[1]);
   }
  }
 }
</script>

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(){ ?>
<script>
 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(location.host) == -1){
   a[i].onclick = function(){
    var url = this.href.split('://');
    ga('send', 'event', 'Outbound link', url[1]);
   }
  }
 }
</script>
<?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. This is the function that is used to send the data above to the GA

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

Third parameter above is a string that will show up as Event category in Google Anlaytics reports. The last parameter is what we pass as an Event action. This is whatever is left after the your-domain.com/recommends/ 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(location.host) == -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:

__gaTracker('send','pageview');

Or like this, if you installed GA manually:

ga('send', 'pageview');

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

Conclusion

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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