Geo-Targeting with MaxMind’s GeoIP API
Geo-targeting is something I’ve wanted to figure out how to implement, but it’s also something I’ve had no need for.
That changed, however, when I was accepted into AzoogleAds publisher program. Azoogle has offers for both the US and International markets, but most offers are targeted to one or the other–not both. As such, to show multiple offers on a given page or website based on the user’s country I’ll need to implement a little geo-targeting.
The only website that receives any noticeable international traffic is Smart Capper. The handicapping tools receive a lot of non-sticky search engine traffic, so I’m going to use Azoogle to try and monetize that traffic.
The top three traffic sources I’m looking to monetize are the United States, United Kingdom, and Canada.
To do the geo-targeting for these locations I first tried to use hostip.info’s IP address database. I quickly realized, however, that their database is far from complete (it had no idea where I was located). As such, I needed to find an alternative.
The alternative: MaxMind’s GeoIP API.
To use the GeoIP API, you must have one of the GeoIP databases. MaxMind has a wide range of databases to choose from, but I chose their free GeoLite Country database, as I only need to get the user’s country, and 98% accuracy is fine for my traffic.
Once you’ve got the database of choice and the GeoIP API for the programming language you’re using, all you have to do is make the correct API call and you’ve got the information you need.
As an example, you can use the following code to get the country code for an IP address using the GeoIP API with PHP:
// include GeoIP PHP API
include("geoip.inc");
// open the GeoIP binary database
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// $ip is an IP address
$country_code = geoip_country_code_by_addr($gi, $ip);
// Country codes:
// http://www.immigration-usa.com/country_digraphs.html
echo $country_code;
This made geo-targeting very easy, and based on the other databases MaxMind offers, they’ll be my first choice if I ever need more detailed information.
If you’re in need of geo-targeting and not sure where to start, then I hope this post has helped make things easier for you.
Tags: geo-targeting, maxmind, geoip
