Internationalising your e-commerce site with MaxMind customer geo-location

July 15th, 2010 by Alex

One of the major attractions of selling online is the ability to address global markets as well as your local market. Doing this effectively means localising key content (e.g. prices) for people visiting from countries around the world. The impact on sales if this is done correctly can be extremely positive: one client saw a 300% increase in international sales since implementing geo-located pricing and delivery. Nor is this overly complex to do, thanks to widely available global payment platforms such as PayPal and geo-location tools such as MaxMind.

Many countries

And so for the second in our series of technical blog posts, we are going to look at the opportunities to enhance your e-commerce site using geo-IP location. Geo-IP location sounds complicated but it is simply the process of determining where your individual website visitors are geographically located in the world; this is achieved by looking up each visitor’s IP address in a database which maps known IP addresses to individual countries or even cities.

As an online retailer, knowing where your website visitors are located allows you to provide them with a much more personalised shopping experience – for example, you could:

  • Show specific contact details for your visitor’s country
  • Price your product catalogue in your customer’s local currency
  • Automatically calculate delivery times and costs for their order

These sorts of personalisations work in two ways to improve your bottom-line: firstly, they increase the level of confidence and trust which a visitor feels in your site by showing that you can treat them as a ‘local’. And secondly, they reduce friction in the check-out process, removing difficult steps for the user such as converting the given currency into their own money. Using these techniques can significantly increase conversions among overseas visitors, as we have seen above.

On to the technology: although there are various providers of geo-IP address databases, we use MaxMind because it is free, simple to use and regularly updated. Also note that many e-commerce packages such as Magento or Prestashop have MaxMind integrations available already for free or low cost – check online to see if your e-commerce package has one too.

For this example we will be proceed as if we are integrating MaxMind directly with a simple PHP-based online shop; we will use MaxMind to display some simple internationalised information to your site visitor. In future blog posts we will explore some more sophisticated localisation approaches, to really drive more sales.

Now on to the technical steps…

The first step is to install the MaxMind API and database. The commands below all assume that you are working in a web root directory, in a Debian/Ubuntu-like environment:

mkdir MaxMind
cd MaxMind
wget -r --no-parent --reject "index.html*" -nH --cut-dirs=4 http://geolite.maxmind.com/download/geoip/api/php/
mkdir data
cd data
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
tar -xvf GeoIP.dat.gz
cd ../..

We’re also going to install the country flags from the famfamfam icon set, so that we can show the user their national flag:

mkdir flags
cd flags
wget http://www.famfamfam.com/lab/icons/flags/famfamfam_flag_icons.zip
unzip famfamfam_flag_icons.zip
cd ..

Next, we write a simple helper PHP file which will be included into our shop and will make it easy to run MaxMind and lookup the user’s country:

mkdir includes
vi includes/geoip.php

And enter the text:

<?php
/**
 * This is a MaxMind helper library
 * Author: Alex Dean (@alexatkeplar http://www.keplarllp.com)
 */

// Include the required PHP file
require_once dirname(__FILE__) . '/../MaxMind/geoip.inc';

// Get the country code using MaxMind geo-IP lookup
function getCountryCode() {
    $mm = geoip_open(dirname(__FILE__) . "/../MaxMind/data/GeoIP.dat", GEOIP_STANDARD);
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    $countryCode = geoip_country_code_by_addr($mm, $ip);
    geoip_close($mm);

    return $countryCode; // 'GB', 'US' etc, or null if not found
}

With this done, let’s next display the user’s current location, so that they know that this online shop is localised for their specific country. Start by creating an index file:

vi ../index.php

And add in the following code:

<?php
require_once("includes/geoip.php");

$countryCode = getCountryCode();
if (isset($countryCode)) {
    echo "Hello, you are from <img src='flags/png/" . strtolower($countryCode) . ".png'>";
} else {
    echo "Sorry, we don't know what country you are from";
}

A few things to note about this code:

  1. We need to check that MaxMind successfully found the IP address, because some IP addresses don’t exist in the MaxMind database.
  2. Rather than just displaying the visitor’s country’s flag, we could use the Zend Framework to map the country code onto the country’s name. (Installing the Zend Framework is out of scope for this blog post).
  3. The MaxMind database is regularly updated (typically once a month) with new and changing IP addresses, so it would be worth setting up a cronjob to update the database automatically

With the basic country-detection code functioning, the next steps would be to layer in more country-specific features, such as pricing in local currency and country-specific contact details. Also it’s a good idea to allow the user to change their country manually, in case MaxMind got it wrong or their country could not be determined. Let us know in the comments what aspects of this internationalisation you would like us to tackle next!

I hope the above is useful and leaves you with a better understanding of what geo-IP location is, and why it is such a powerful tool for e-commerce. And do let me know how you get on with the code samples – I will reply to any questions in the comments.

Are you interested in internationalising your e-commerce site? Keplar can provide you with strategic and technical support – please drop us an email to find out more.

2 Responses to “Internationalising your e-commerce site with MaxMind customer geo-location”

  1. Santosh says:

    Alex, good article. I felt to share one point : with upcoming HTML5, geo-location will be part of it and hence making it much easier to any web app to use this info, also less rely on 3rd party solutions.
    We at SignureTech have used Maxmind for various projects and like it.

    • Alex says:

      Thanks for your message Santosh! I wasn’t aware of geo-location being included in HTML5, but sure enough found the API specification here. It will be interesting to see to what extent the browser implementations go beyond using mobile-only GPS lookups and head into ‘fixed internet’ geo-IP database lookups, which as you say would threaten commercial providers…

Leave a Reply