Most websites that do any affiliate marketing will almost never run naked affiliate links on the site. Using redirects has several benefits over plain affiliate links:

  • Easier to maintain. If you need to swap out all the links from one affiliate company and replace them with another, it’s impossible if you have to change the link on every page. Change it at the redirect and save yourself tons of time.
  • Looks cleaner. Most affiliate links look like a string of random letters whereas your redirects can be cleaner, using company or product names rather than a string of jumbled letters.
  • Lets you track clicks better. Before the days of Analytics, the easiest way to track clicks was to record it in your database and that was only possible if you use redirects. Now, you can just javascript to track it in Analytics or, if you are concerned that many visitors have disabled javascript, you can still use database calls to record clicks.

The reason I use redirects is mostly because of the first reason – maintenance. I prefer it over other solutions like GoCodes (a plugin that achieves the same result) because the way I’ve implemented it requires no additional database calls and is pretty much as fast as you can get.

Setting Up The Redirect Scripts

<?php
include('allredirects.php');
$tag = htmlspecialchars($_GET['tag']);
 
$link = "<strong>[the url]</strong>".$tag;
header("Location:".$link);
?>

The links themselves will look like this –

http://www.wangarific.com/r/redirect.php?tag=SubID

The above code will pull the variable out of the URL (in the above case, it’s “SubID”) and append it to the affiliate link. You will have to look at the affiliate link to know where that information goes.

For example, for CJ, you take your affiliate link and append ?sid=XXXX to the end to pass along that SubID tracking value.

The include(‘allredirects.php’); is an empty file that I left in there in the event I wanted to add something to the start of all redirects. Years ago, before Google Analytics, I had it insert data into my database to help me track clicks. Nowadays there’s a far simpler solution.

Tracking Clicks

Often times the affiliate network will provide this data but if you don’t trust them or you want to verify their data, you can add your own tracking. Tracking clicks is dead simple with Google Analytics. Simply take the link and add the following call in the OnClick function:

onClick='javascript: pageTracker._trackPageview("/click/affiliate/tag")'

You can change /click/affiliate/tag to whatever you want but every time someone clicks, Analytics tracks it. Then, you can go into analytics and search for “click” or “click/affiliate” to see how many clicks you have on those links. Replace tag with the SubID and you get even more data to work with.

One tip, you will need to move your javascript for Google Analytics above these calls for it to work. I put mine in the header.

One script or many?

In the above example, there’s one .php file for each redirect. Another alternative is to use one script for every redirect and just pass in two variables – one for the subID and one for which affiliate link to use.

http://www.wangarific.com/r/redirect.php?aff=AffiliateCompany&tag=SubID

What you do is up to you, I personally like the individual pages though it really affords no significant advantage.

Wasn’t that simple?

RSS Subscribe Like this article? Get all the latest articles sent to your email for free every day. Just click "Subscribe" and enter your email. Your email will only be used for this daily subscription and you can unsubscribe anytime.

5 Responses to “How to Set Up an Affiliate Link Redirect System”

  1. mbhunter Says:

    I’m enjoying these articles very much.

    So htmlspecialchars is enough to prevent injection?

  2. jim Says:

    In the above case, I wouldn’t be too worried because the code just redirects people. It doesn’t go into a database and it doesn’t use the tag data to do it, so it’s harmless.

    However, if you do access the database and use that tag information, htmlspecialchars won’t be enough for someone experienced. If you want to learn more, you can read about it here. The first answer is especially detailed.

  3. mbhunter Says:

    Right, there’s no DB issue, but what about code injection or remote file inclusion via the $_GET variable? (There are decent descriptions of these on Wikipedia.)

  4. jim Says:

    Since the data is being used to form a URL, I don’t think you have that risk.

  5. Mrs. Accountability Says:

    So that is how that is done. All way over my head, unfortunately. First, where does that snippet of php coding go? Into a notepad file saved to the root directory of my blog? And named something.php, or does it go somewhere in my blog coding? I am self hosting with WordPress and Atahualpa.

Leave a Reply