How to Track Converting Keywords for your Affiliate Marketing advertising using PHP.

September 1, 2007

Do it yourself keyword conversion tracking

I started to write this on then figured I needed some new content this week.

Have you ever wanted to find out which of your keywords are converting on your campaigns you’re running?

This is actually a relatively simple thing to do if you pass the keyword as your subid. However because we never know who’s looking at our keywords we’re going to want to rewrite the keyword as a number that we have reference to. If you’re traffic to a content site or something other then a straight redirect bounce page, you can track them with a cookie or session or both. So a very simple solution for an affiliate redirect page that tracks your conversion by keyword might be.


$keyword = trim($_GET[‘keyword’]);
$keyword = mysql_real_escape_string($keyword);

// Finding the keyword in the database if it’s there
$sql = “SELECT * FROM keywords WHERE keyword = ‘$keyword'”;
$sql_out = mysql_query($sql);


$row = mysql_fetch_assoc($sql_out);
$id = $row[‘id’]; //gets the id associated with the word
$sql = “UPDATE keywords SET count = count + 1”; // sql to increment the counter for that word


// This chunk puts our new word in the database and grabs it’s ID
$sql = “INSERT INTO keywords (keyword) VALUES (‘$keyword’)”; // sql to insert the new keyword we don’t have cataloged
$id = mysql_insert_id(); // gets the id of the record we just inserted.

// Here we’re redirecting to the end location with our replaced keyword with the new ID
$location = “Location:$id”;


SQL for this is just a table is very simple. Just a 3 fields.

1. id set to primary key and auto-increment
2. keyword set to varchar 200 and indexed
3. count set to double no indexing

And that should do the trick….

This will take the incoming visitor and log the keyword, then redirect to the offer based with the subid for the keyword. This is a straight redirect bounce page with replacement keyword tracking.

Now if you want to track based on them coming to a site do two thing make your page based on php. and at the top put that same code, just remove the header and location lines. Then when you generate your page replace your links with with the $id so for example that php page would look like this.

// previous code here minus the header redirect

<h1> here is some text</h1>
<p> here more text and a <a href=”<? echo $id; ?>”>link</a><p>

now you’ll see we’re calling the php inline to echo the number we’ve retrieved from the keyword above. Then when you run that page in your browser you’ll see the id’s replaced 🙂 pretty slick huh.

A couple ideas for this that could be added are geoip tracking and redirection based on the location of the visiting guest. Or how about finding out where your visitors are coming from? Do you know if you’re getting natural traffic? And if you do know, do you know where’s it coming from and which of those locations are converting for you? If you found a link to your site that was bringing a lot of nice traffic that was converting it might make sense to run a banner ad or something like that there wouldn’t it?

Tracking is Simple, Powerful and fun using a little PHP ingenuity.

another freebie from


Want More? The more people listening the more I’ll write.
Subscribe to get business insights in your inbox
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.