Build Your Own PHP Affiliate Tracker – The Redirect Link

I’m going to continue down the road of sharing some of the coding knowledge I’ve been slowly picking up with my next Affiliate Hack – setting up your first tracking link.

The tracking link is perhaps the most fundamental piece of building your own tracker, as it’s the first tangible piece of your campaign as well as the last (you send a user to your page using a tracking link and then send them off to the offer via your tracking link). Rather than start from the beginning and write out a sequential guide to building a tracker, I’m just going to share what a basic tracking link looks like and how you can use it in your affiliate campaigns. Like I said the essence of all PHP trackers are built on this so it’s good knowledge to start with.

There are free trackers out there like Prosper202, but using your own tracking is challenging, rewarding, and can offer a more robust and customized product for yourself.

Split Testing Headlines with a Tracking Link

For this example I’m going to show you how you can test multiple headlines on your landing page and track which one leads to more sales. Say we have a page selling Printer Ink (hey that was big online in like 2006 remember?), and we want to test a few headlines at the top of our page and see how they perform. We want to test these 3 headlines:

  • $49 Printer Ink Blowout!
  • Cheap Brand Name Ink
  • Clearance Sale on Ink (50% Off)

The first question is: How do we rotate these 3 headlines on our page? Which then follows up with the question Once they’re rotating, how do I actually track performance? Let’s break down each question:

How do we rotate these 3 headlines on our page?

This problem has a pretty easy solution with PHP using PHP arrays and a couple nifty functions. Here’s the code you’ll place at the very top of your landing page:

<?
$h1 = "$49 Printer Ink Blowout!";
$h2 = "Cheap Brand Name Ink";
$h3 = "Clearance Sale on Ink (50% Off)";

$headlines = array(
array('h1', $h1),
array('h2', $h2),
array('h3', $h3)
);

$c = count($headlines)-1;
$headline = $headlines[rand(0,$c)];
?>

Let’s break down what we’re doing line by line:

The $h1-$h3 lines : These lines simply define the headlines we’re split testing. The first headline would be called with $h1 so that if you ran the code ‘echo $h1’, the screen would display “$49 Printer Ink Blowout!”.

The $headlines array : this is where we create the array (kind of like a container or bucket) to store our headlines. This is a multidimensional array (a bucket with a bucket inside of it) that initially complicates things a bit more but will save us time in the end. Our $headlines array contains 3 arrays within it, 1 for each headline. The individual arrays store two things, first ‘h1’, which is just the name we’re going to use to track the headline (you can make this whatever you want). That way when we run a report in our affiliate network, the subid will show up as “h1” so that you know the sale came from that headline. The second part of the array stores the headline itself, which are the variables we defined at the top.

Setting the count ($c) : The next thing we’ll do to simplify our code is take a basic count of all of our headlines so that we can pick one randomly. The count($headlines) function will tell us that there are 3 headlines to pick from, we then add -1 at the end because arrays start with 0, meaning our headlines are actually 0,1,2 and not 1,2,3. The -1 tells $c that the count ends at 2 instead of 3 so that we don’t echo out a headline that doesn’t exist (blank).

Setting the $headline : Now we get to randomly select our headline. $headlines[rand(0,$c)]; tells us to first look at our $headlines array (which contains the split tests) and then select randomly between 0 and 2 (remember our headlines are 0,1,2 so we’re getting all 3 here).

Now we’ve randomly selected our headline! It’s time to use it. From the code above we’ve produced two tangible variables:

$headline[1];
$headline[0];

When you want to use the actual headline in your page and display it, simply paste this:

<? echo $headline[1]; ?>

That’s going to pull our randomly selected headline array and echo the SECOND value, which is the headline itself (remember again arrays start with 0, so the second piece of the array has a 1 value). $headline[0] will echo out ‘h1’, ‘h2’, or ‘h3’, depending on the random selection.

Once they’re rotating, how do I actually track performance?

Cool, now we’ve got a few headlines out there rotating to our customers. It’s time to actually track which ones are leading to clicks and sales, which is where our tracking link comes in. On your page, at some point you’re going to link to the affiliate offer, which very basically would look like this

<a href="http://affiliatenetwork.com/?campaign=1234&subid=">This is my affiliate link</a>

We’re going to change that link to this

<a href="http://mylandingpage.com/redirect.php?h=<? echo $headline[0] ?>">This is my affiliate link.</a>

This is going to be the outbound tracking link you use on your call to action buttons, links, etc. You’ll see that we’re adding ?h= to the end of our URL…this will pass the subid for the headline (h1, h2, h3) to our redirect link. Now we have to build out the redirect.php so our customers get rerouted to the offer with the conversion data we’re testing for. The redirect.php file will look like

<?
$subid = $_GET['h'];
$go = 'http://affiliatenetwork.com/?campaign=1234&subid='.$subid.'';
header('Location: '.$go.'');
?>

This one is nice and easy.

The $subid : Here we’re just pulling the ?h= from the landing page.

The $go : Now we build out our tracking link, which is just our affiliate link from before but now with $subid attached to the subid portion of the URL. Most affiliate networks have similar subid structure, either subid, s1, sub1, etc.

The header redirect : Once we have our affiliate link populated with a subid, we’re all good to send them off to the offer!

See, it wasn’t that hard was it? A few lines of code and you’re now testing multiple headlines (or images, or colors, or anything) and tracking which ones lead to clicks and sales. You can even compare CTR on the headlines very crudely by simply looking at how many clicks each subid receives in the affiliate network.

Once you understand the core concept of the tracking link, building out other elements of the tracker aren’t nearly as daunting as they might seem. Prosper202 and other trackers largely rely on what we just did – setting a variable to test and then tracking it via subid. In the exact same way you can rotate and test landing pages, offers, traffic sources, ad copy, and anything else you want to follow. The real fun comes when you want to compile all of the data and interface it for reporting.

As always, if there are holes in my code or things that could be more efficient, please comment so I can update.


13 Comments

  1. amonies
    November 6, 2013

    It would be simpler to set the array indexes directly.. e.g. $headers[] = “one”; $header[] = “two”; Rand has also been pretty much phased out for mt_rand, which is a (supposedly) improved function.Copying the headline into a new var is simple but also uses more memory, for what its worth. More efficient to reuse the existing variable. $active = mt_rand(0, (count($headlines)-1); Then do echo $headlines[$active]; Food for thought!

  2. Nick
    November 6, 2013

    Where do you take the subs into account with this? Are you just assuming $active (the indexed position of the headline array used) is going to be the sub passed along to the tracker? This would work so long as you never change the headlines testing and don’t care about historical data, but if your constantly ab’ing in new headlines and removing old ones, your going to want an enumerating to track them by – h1,h2,h3 in paul’s example. Otherwise, subs of 0,1,2,etc are going to reflect different headlines on different dates in the tracker.

  3. Rick
    November 6, 2013

    Or save the time and use optimizely.

  4. adam
    November 14, 2013

    Ur blog is very nice. its very usefull for me.Throught this i learn many points to improve my experiance.iPhone screen Protectors

  5. January 13, 2014

    Look at what this man can make out of paper

    http://gonon2014.blogspot.com/2014/01/li-hongbo-out-of-paper.html

  6. April 17, 2014

    Awesome tips on rotating headlines. Using this I can also rotate multiple headline, images and CTA at the same time :D

    But in multivariate testing to find out the winning combination, which statistical method should I use?

    Like for A/B test, g-test (http://adcalc.net/split-test/) is used but not sure what to use for multivariate testing.

    Let me know.

    Thanks

  7. tshering bhutia
    June 3, 2014

    Still puzzled
    how to satisfy your cutest person on their most remarkable days? Check out any
    Online shopping site and Send
    Valentine’s Day Flowers to France. These sites have a huge assortment of
    various kinds of popular summer and spring floral assortment, highly enjoyable Bottle of Champagne and many
    other gift items to give your loved ones an adequate pleasant on their faces.

  8. arsenal f.c
    June 12, 2014

    Render
    waves of happiness in the life of your special one and make your own life happy
    too! Indulge in ordering Cheap
    Valentine Flowers to Mexico
    from the shopping portals available on the web to witness
    the heart of those you love skip a beat and hug you tight.

  9. David Curry
    April 7, 2015

    I think this is the best tool for A/B split testing:
    http://www.coreminer.com/calculators/ab-test

  10. April 8, 2015

    Make the day
    of your loving mummy bright and happy with the amazing fragrance of Flowers and
    show your deep love and concern. Send Mother’s Day Flowers to Spain to the doorsteps of your dear mom and say your
    words in exclusive way. Order online for Mother’s Day Gifts Delivery in Spain and convey your sincere
    regards to your Mother.

  11. April 20, 2015

    Nothing will be better than this Bouquet of Mixed Flowers to your
    adorable mother on this special day of love and affection. Send Mother’s Day Gifts to Brazil at right price through any
    online shopping store and show your best love and respect. Order online for Mother’s Day Flower Delivery in Braziland incorporate happiness into her life.

Leave a Comment