My mission today is very simple: to get you started with geotargeting on your websites. It's not that geotargeting is hard, but we all need to start somewhere, and this post is it.

Basics of Geotargeting

Geotargeting is showing your visitors different content based on their geographical locations. For example, Amazon serves many countries, and so they have amazon.com, amazon.co.uk, amazon.fr and so on. Each of those domains target different visitors from around the world.

You can implement geotargeting either automatically or by prompting the user.

Amazon still requires you browse to the correct domain name for you to buy their goods correctly.

PHP.net international sitesGoogle on the other hand automatically redirects visitors in the UK from google.com to google.co.uk (Tip: if you don't want this redirect, type in google.com/ncr and be forever google.com bound); another example is php.net.

Many online retailers very annoyingly force users to choose their country in a splash screen before they enter the site.

Sigma Aldrich geo splash screen

As you can see, both options have upsides and downsides.

Another consideration is the mechanism the visitor is shown the geotargeted content. You can either redirect them to a different URL (as per Google) or you can show them different content on the same URL. The SEOs among you will immediately see that this could have serious SEO implications; for example, will the search engine bots see and index the geotargeted content? A very good question for Vanessa Fox to answer for us.

The point of this post is simple: the code side of geotargeting is very easy, and the biggest traps are on the business, SEO, and marketing side of things.

Let's get going. There are two things you will need to implement geotargeting:

  • An IP to location database which maps IP addresses to geographical locations.
  • Some code to query this database and do the "business logic" on your website or application.

Geolocation Databases

There are many available, many of which are free even for commercial use. The biggest consideration for choosing an IP-location database is its accuracy. Most freely available ones claim >99.5% accuracy for country-level geotargeting, which is not bad. However, consider that if you have thousands of visitors each day, the remaining 0.5% will quickly add up to significant volumes of traffic. Commercial databases typically claim more than that, around 99.8% or so.

State- and
city-level
accuracy is
much lower, typically
around 60-80% accuracy
While we're on the subject, it is very important to note that no database is, nor can be, 100% accurate. If accuracy is the most important criterion, make sure you pay for an accurate database and make sure you regularly update it. Updating, like installing, is easy.

Another consideration, very much related to accuracy, is the level geographical level of accuracy: when quoting >99.5% accuracy, we are talking about country-level geotargeting. State- and city-level accuracy is much lower, typically around 60-80% accuracy. This ties in with your business needs: at what level of "local" do you need to operate at?

Beyond cost and accuracy, the other main consideration is the format of the database. SQL is a very popular format, and also CSV. Some provide proprietary binary formats which they claim are optimized for speed. Many also provide APIs you can use to access geolocation data over HTTP as a service. To choose one, you simply have to test which suits your infrastructure and needs best.

Where to get a geolocation database? Start with these:

  • MaxMind: This is my favorite service which I use on many sites. The link is to their free city geolocation database, which includes the free country geolocation database.
  • IPInfoDB.
  • hostip.info.

Geotargeting Code

The easiest way to get started is this: Download the GeoLiteCity database from MaxMind (direct download link) and unzip it somewhere handy on your server.

Next, choose the code samples of your favorite programming language from their code samples page. Let's look at their (slightly modified) PHP code for the city-level geolocation:

<?php
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("/path/to/GeoIPCity.dat", GEOIP_STANDARD);

$record = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo '<p>Country code: ' . $record->country_code . '. Three letter country code: ' . $record->country_code3 . '. Country name: ' . $record->country_name . '</p>';
echo '<p>Region/state/district within country: ' . $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . '</p>';
echo '<p>City: ' . $record->city . '</p>';
geoip_close($gi);
?>

You can get the geoipcity.inc and geoipregionvars.php from the PHP code link above. Make sure you edit the path to GeoIPCity.dat!

Save this code above into a PHP file (let's call it geo.php) and upload it along geoipcity.inc and geoipregionvars.php to your website. Now browse to geo.php using your web browser, and it will automatically detect your IP address and geolocate you. It will show you various bits of info about you like the country and region and, hopefully, the city you're in. Very simple, no?

With this in hand, let's think about what we need to do. A visitor comes to the site, and we'd like to show them country-specific content. Suppose we want to show (say) Swedish visitors some content, and the rest of the world another content (say we're doing a promotion for Sweden only). Here is some code of what we can do:

<?php
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("/path/to/GeoIPCity.dat", GEOIP_STANDARD);

$record = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
if($record->country_code == "SE"){
echo "Hello! We can show you our Sweden promotion!";
}
else{
echo "Hello! We'll show you our default content.";
}
geoip_close($gi);
?>

What does this code? It geolocates the IP address and then checks if it is from Sweden (SE is the 2-letter country code for Sweden); if yes, it shows them some content, if not, it shows them the default content.

A very important note about this code: we're using the ISO 2-letter country code, but it is by no means the only standard that various databases and mapping libraries use. For a very basic intro to this world of incompatible standards, Wikipedia is your friend. Whichever database you use, make sure you know which country codes it is using and stick with them! Believe me, it will save you hours of chasing obscure bugs.

Don't Geotarget for Language

Talking in Languages by zinjixmaggir

A word of warning. A lot of websites geolocate an IP address and change the website's language based on the location. This is wrong wrong and even more wrong. Here is why.

Take a country like Switzerland where there are three different main languages: German, French, and Italian. Suppose you geotarget all Swiss users to (say) German. What happens when a Swiss visitors comes your website but they can read only French?

Another similar trap is travellers. As I've travelled many times with my UK laptop around Europe, every time I browse the net I get geolocated to whichever country I'm in, and the language of the website changes accordingly. I don't understand a word of what the site is saying and it is very annoying. The biggest culprit? None other than Google.

In summary, don't change the language based on geographical location. Instead, properly detect the language headers and react accordingly. Geolocation is not the same as language preference, OK?

Provide a Fall-back, or, When Geotargeting Fails

The travellers example I shared above is one way geotargeting can fail miserably. The usability implications of this are obvious and so you need to provide a way for your visitors to manually correct/change the automatic geotargeting your website just did.

There are many ways to do this. The simplest is a set of links (say with country flags) that let the user select their geotargeted website version of choice. I've seen placements in website headers, at the top of sidebars, and in footers. Whichever place you put this, make sure it's prominent and, here is the key point, multilingual! That's why I strongly advocate using country flags as geolocation indicators. They are images that are unique, immediately recognizable by the relevant visitors, and don't require any text to be translated.

You can get free flag icons from ip2location and the famous famfamfam library (makers of the popular Silk icon set).

Another way to do this is with a splash screen but pre-selecting/highlighting the country in the list. This, again, is automatic mechanism with manual over-ride.

Wrapping Up

I set out to show you how to implement geotargeting on your websites. I gave you links to pretty much everything you need and gave you a brief overview of the thinking required to decide how to implement geotargeting. The upshot of all of this is that if you need or would like to experiment with geotargeting, you can start right now. So come on, what are you waiting for?

Pierre has built and runs many websites including a short URL with analytics and geotargeting service called Cligs. You can follow and contact him via @pierrefar on Twitter.

You May Also Like

8 Responses to “Finding Your Visitors: The Geotargeting Manual for Dummies”

  1. [...] Finding Your Visitors: The Geotargeting Manual for Dummies, Search Engine People [...]

  2. Melayu Boleh says:

    I think it's definitely a good idea to personalize a page by determine where your audience is coming from.

  3. Great tip on not changing the language of your site when geotargeting. It seems like a mistake that a lot of websites make.

  4. Phillip Crum says:

    Excellent post. That you supplied the "how" to go along-side the "what" is much appreciated. Wish more bloggers did that.

  5. Abhishek says:

    Nice Post.

    I find MaxMind's Paid API based more accurate.
    A good way to speed it up is to set a country cookie for few months after checking against database

  6. James says:

    The MaxMind paid is a few hundred dollars I believe, and I think you have to pay for updates.

    I used the free version and it worked really well for what I wanted. Later on I needed the ability to find the closest major city which Lambda GeoIP is good for. All in all, if you just need the general location of your user: use Google's geo targeting or MaxMind's database. If you need the accurate location then pay for the MaxMind paid version or use Lambda GeoIP.

  7. The way I prefer to do the language thing, is looking into the headers, and change the language a cording.
    This happens a lot to me, I live in Argentina (which speaks spanish) but most of the time i'm on the internet, I look for english content, further more, my windows language is english, and all the applications I install as well.
    The geolocation is good, with some exceptions, google for instance, if I type .com, I want to go to the .com site, not the .com.ar. GC magazine, have something similar, and no intuitive way to go back.
    I prefer the way yahoo or msn do it. if you type .com, they place a div on top saying like "You are at yahoo.com, for local content, go to yahoo.com.ar" that you can close, and it doesn't bother you at all.

  8. Mickey Grenadine says:

    The web browser submits information regarding the user's language preferences to the server. There is no need to geolocate or other similar nonsense to figure out what language is preferred. An aspect of multilingual sites that this article doesn't cover is figuring out how to keep content changes in sync.