Track Affiliate Link Clicks with Universal Analytics

  • follow us in feedly
Published February 19, 2014 by Brad Knutson
Tracking Affiliate Clicks with Universal Analytics

Full disclosure: the square images in the sidebar to the right are all products and services that I’m an affiliate for. If you click on one of the image links, you’ll be sent along your way with my affiliate cookie in your browser. If you decide to purchase said products or services, I’ll get a small percentage of the sale.

I’ve chosen to include these affiliate links as more of an experiment than a way to monetize my blog. Over the last year I’ve been changing out the affiliate images and running split A/B tests to see what works and what converts the best. During this entire time frame, I’ve made little to no effort to push these products and services on my visitors – I was more interested in seeing what aspects (image color, amount of savings, font in the image, text in the image, etc.) produced the most click-throughs.

Most of my affiliate links are managed through Share-A-Sale, but a few are managed through proprietary or third-party platforms. It’s a minor annoyance to have to gather data from multiple sources to get the bigger picture of what I’m testing.

So – I decided it would make more sense to track click-throughs in one place. Enter Google Analytics, my go-to place for tracking traffic, conversions, you name it – so why not track affiliate outbound clicks through Universal Analytics?

Setting Up the HTML

The HTML is extremely simple – all you need to do is add a unique identifier for each affiliate link. Because I only have 5 affiliate images in the sidebar, I choose to simply add a unique class to each affiliate link.

Here is an example of the Raven Tools affiliate link I have in the sidebar.

I added the unique class “affiliate-link-1″. With this unique class in hand, we can now identify the affiliate link – we just need to trigger a click on it.

jQuery Push

I choose to use jQuery because it’s already included in my theme and I feel more comfortable using it. The first step is to listen for a click on this affiliate link and trigger an event.

Now we have our mechanism with which to handle a click on the affiliate link.

Next, we need to push this data to Universal Analytics. We’ll use the built-in Universal Analytics function ga(). This function is available to anyone who includes the Universal Analytics script on their website.

Note: The ga() function is only available to you if you use new Universal Analytics tracking code, not the older Google Analytics tracking code.

The first variable ‘send’ let’s Universal Analytics know we’re sending it data, and the second (‘event’) variable specifies that it’s an event.

The third variable (‘affiliate’) is the category.
The fourth variable (‘image’) is the action. I entered ‘image’ here because I wanted to specify a difference between affiliate image link clicks and affiliate text link clicks. You could use ‘outbound-click’ here if you don’t care about the difference.
The fifth variable (‘Raven Tools’) is the label. This is how you will distinguish which affiliate link has been clicked in your Universal Analytics report.

Now we just need to put the two together.

That’s it! We’re now set up to track clicks on the affiliate link.

Here is the full script I use to track clicks for all my affiliate links.

I’m aware that this code could be refactored and rewritten into a better performing function, but for sake of brevity and complexity, this solution works just fine.

We’re Set Up – Now Generate The Report

Now that we’re all set up and ready to go, we just need to wait for some data to come in. Depending on the amount of traffic you get, or the traction your affiliate links get, you may have to wait a few days, weeks, or months.

Once you’re confident you have the data, you can log into Google Analytics and generate the report.

On the Google Analytics sidebar, click Behavior > Events > Top Events.

In the table, find ‘affiliate’ in the Event Catgory column and click on it.

Filter by the date range you’re interested in, and choose your Primary Dimension as Label. You’ll see something like this.

Universal Analytics Affiliate Report

All my affiliate click data in one place, finally!

One could take this a step further by adding a value to the event, but I chose not to at this point.

I hope you found this as useful as I did. Let me know if you have any questions in the comments below!

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date



See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

4 thoughts on “Track Affiliate Link Clicks with Universal Analytics

    1. Brad Knutson Post author

      Hey Sahil,

      The new Universal Analytics tracking code looks like this:

      and the older Google Analytics tracking code looks like this:

  1. Patrick Mackaaij

    As you’re using Yoast’s Google Analytics plugin and are redirecting affiliatelinks via /go/ you could also just setup the WordPress plugin to treat /go/ links as external and label them as an affiliatelink.

    I’ve asked Yoast to implement a way to label affiliatelinks marked with rel=”nofollow” for those who’d rather do without a /go/-redirect.


Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">