eBay Geo-targeting in PHP
So in my post about eBay’s affiliates day I made a big deal about the crappiness of geo-targeting in javascript. I decided to be a little proactive and post some code for any eBay affiliates using PHP. This is an easy and free method to ensure your traffic is going to its highest-possible-converting destination.
Why do I want to geo-target?
Because it works better. For you and for the customer. Imagine if you were living in the US, you clicked on an eBay ad, and you went to eBay Canada? Well, that’s how your Canadian visitors feel when you send them to eBay US. Traffic sent to their home eBay converts much better, making you more money and making your visitors happier. This is a good thing.
What do I need to geo-target?
For this example you need some know how, a mysql database, and php 4 or newer. Also, instead of pointing your links to rover.ebay.com you would need to point them at this script you are going to make, for example click.php. This script will do the geo-targeting and then redirect the user to the appropriate eBay. For the purposes of this example we will be going to a specific auction ID but it the geo-targeting will work for any type of links.
Let’s get it on!
First, you need a Geoip database. Basically,what we are doing is looking at a visitor’s IP and comparing it to a range of IPs for each country. I used MaxMind’s Geo Lite Country product. They give it to you in a .csv which is kind of a pain to import into MySQL. If you know what you are doing, import a new version, because they update every month and it’s best to get it from them. I know I am going to regret this but you can download a mysql dump of the data here. No I am never going to update that file and by the time you read this it is probably going to be REALLY out of date. Aaaaanyways, somehow get that data into a MySQL table, in this example it’s called ‘geoip’ very original I know.
So to geotarget first we will connect to our DB (obviously put your own settings in here):
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
Yeah that’s probably the sloppiest DB connect code I have ever seen, I personally use Justin Vincent’s ez SQL class.
At any rate, now you have a connection to your database. Now you need to see where your visitor is from.
$ipNum = ip2long($_SERVER[REMOTE_ADDR]);
$result = mysql_query("select countryCode from geoip where $ipNum >= beginningIPNum AND $ipNum < = endingIPNum");
$country = mysql_fetch_assoc($result);
$visitorsCountry = $country['countryCode'];
So now you know where they are from, now you need to map it to the correct eBay country. First we will make an array with all of eBay's country codes.
$placementIDs['GB'] = '710-53481-19255-0';
$placementIDs['CA'] = '706-53473-19255-0';
$placementIDs['AU'] = '705-53470-19255-0';
$placementIDs['US'] = '711-53200-19255-0';
$placementIDs['BE'] = '1553-53471-19255-0';
$placementIDs['ES'] = '1185-53479-19255-0';
$placementIDs['FR'] = '709-53476-19255-0';
$placementIDs['HK'] = '3422-53475-19255-0';
$placementIDs['IN'] = '4686-53472-19255-0';
$placementIDs['IT'] = '724-53478-19255-0';
$placementIDs['NL'] = '1346-53482-19255-0';
$placementIDs['SG'] = '3423-53474-19255-0';
Now you can target to all or any of those countries if you want. I only target to english speaking countries because all my sites are English-only, but you can easily modify this code to target all of the above countries.
switch ($visitorsCountry) {
case 'GB':
$placeID = $placementIDs['GB'];
break;
case 'CA':
$placeID = $placementIDs['CA'];
break;
case 'AU':
$placeID = $placementIDs['AU'];
break;
default:
$placeID = $placementIDs['US'];
}
Then generate your URL you will be redirecting to, like I said in this example we will be going to a specific auction ID. In this example I am assuming you are passing it like click.php?AID=12345. Remember to put your own Campaign ID in!
header('location:http://rover.ebay.com/rover/1/'.$placeID.'/1?type=2&campid=000000000&toolid=10001&customid=&ext='.$_GET['AID'].'&item='.$_GET['AID']);
This is intended as a guide for someone who is familiar with PHP, not as a copy and paste one size fits all solution. If you have questions ask in the comments I will try and help out.

Can you geo target to specific states? I think i could use this on uslf. how do you deal w/ proxys like aol?
I also want to chat w/ you about getting kicksnet.com setup the best way($$) as an ebay affiliate.