I recently set up a new Wordpress site and wanted to track outbound clicks to merchants in Google Analytics.

Joost de Valk's plugin (Google Analytics for WordPress - Yoast - Tweaking Websites) does a great job of automating this, however this only works with outbound links - it stops working when you mask your affiliate links.

I came up with a couple of lines of code which tags masked affiliate links so they show up in Google Analytics as pageviews like this:

/outbound/AmazonKindle

My masked URLs look like this: example.com/go/amazon/kindle

In the kindle folder is an index.php file which redirects to the affiliate URL.

In my wordpress theme's single.php (the file that displays a single post), I added in the following code where I wanted the link to appear:

PHP Code:
<?php $buyurl get_post_meta($post->ID'buyurl'true); 
                
                if(!empty(
$buyurl)) {    
                
$outboundref str_replace(' '''$post->post_title); 
                
$buylink "<a href=\""$buyurl ."\" onClick=\"javascript: pageTracker._trackPageview('/outgoing/"$outboundref ."'); \">Buy</a>";
                echo 
$buylink;
                } 
?>
In every post I added a custom field called 'buyurl' with the value being the affiliate URL. In order for this to work, your Google Analytics tracking code needs to be above where this code appears. I recommend Joost's plugin for this.

Now in your Google Analytics reports, you will see every affiliate click show up as /outbound/PostTitle

I hope someone finds this of use! If it doesn't make sense or you have trouble implementing it, shoot me a PM!