Archive for May 20th, 2009

20th May
2009
written by Tristan

I need an affiliate system for an internet business that I am building, so rather than go and buy a ready made “off the shelf” system for lots of money or get suckered into using an existing network and stuck paying their horrendous setup and monthly fees, I figured it would be easier to simply build my own affiliate tracking system.

At first I thought it would be really difficult, and wasn’t sure how I would go about doing it until I spoke to my brother who recommended using an Iframe for the tracking, apparently it’s how most of the big affiliate networks solve the problem.

The thinking is that by giving a non-technical web retailer a simple line of code that can be pasted into their webpage(s), the rest can be taken care of by the affiliate network. This seemed simple enough, so I decided having a spare half hour, I would have a crack at it myself.

Within the iframe, you need to pull in a tracking url and pass it at least one variable – this would be the id of the affiliate that you are tracking. You can add more variables if you like, but it makes more sense to try and track other variables dynamically using your script rather than the retailer having to edit the iframe code to ensure a bunch of fiddly variables are passed back to the affiliate network.

Here’s a simple example of an affiliate tracking iframe:

<iframe src=”http://www.affiliate-network.co.uk/affiliate-tracker.php?merchantid=1” width="0" height="0"></iframe>

The iframe simply pulls in the url from the affiliate network server, passing it the id of the merchant back to the server. This allows the affiliate network to know that there has been traffic on the merchants website.

The next problem is tracking the affiliate id, which needs to be dynamically passed to the affiliate network. This again can be done by passing a variable in the query string, for instance:

&affiliateid=<?php echo $_GET[‘affiliateid’]; ?>

This will pick up the affiliate id from the query string and pass it through to the scrip on the affiliate network server. You can leave this code in the iframe on every page, but it is not a safe bet to rely on there always being an affiliate id in the query string. To ensure that your affiliate gets credit for any sales that go through a merchant, it is imperative that you save the affiliate id in a cookie.

To do this is fairly simple, you will need to pass the affiliate id through to the tracker script initially via the query string, use the id to set the cookie, the code (in php) can be something like this:

<?php

session_start();
$merchantid                = $_GET['merchantid'];
$affid                        = $_GET['affid'];
$cookie_time_out    = time()+(60*60*24*31);

if ($affid)
{
    setcookie("cookie_affid", $affid, $cookie_time_out)
}

This will set the cookie for a 31 day month, clearly to adjust the length of days for the cookie, simply adjust the last figure in $cookie_time_out to be the number of days that you wish the cookie to last for.

What you need to understand about this cookie is that it will only work on the affiliate networks’ domain, it will not be accessible from the domain of the merchant, this is a good thing, as it means they can’t destroy or amend the cookie.

The next issue to solve is saving the stats for each affiliate, this can be done quite simply by sticking the data into a database, most likely MySQL. I won’t go into the details of how to do this, instead I’ll look at how to record a sale.

On the “check out” page, the merchant will need to pass a unique transaction reference to the affiliate tracker script. This will usually be the transaction id, which is unique to a transaction, can be traced to a customer and will only materialise if there has been a sale. This can also be used to cross reference between the merchant and the affiliate network should there ever be any discrepancies between the amount of business the affiliate has recorded for the merchant and what the merchant claims has been transacted.

So there we have it, a really simple way of recording third party transactions through an affiliate tracking script. To make this more secure, it would be better to give the merchant a code slug of Javascript and keep all the code needed to pass data back to the affiliate tracking script on the affiliate networks’ servers.