How to increase conversions automatically serving the right content to the right audience with geo targeting

11 April 2019
Serving different website content for different geo located audiences can increase website conversions and it’s very easy to implement. Are you missing out?

While working with Flexitol / Dermal Therapy, a leading skin care Australian brand, we realised we needed a better way to showcase different product offerings depending on the country the visitor was from. Originally, the user would visit different LTD’s (domain extensions eg: .com, .com.au, .co.uk) to go directly to the content they were after – but when searching in google, they could end up visiting a product that was indeed not available in their country of origin.

This will increase conversions, as you are actually showing content that is specifically aimed at that audience.

The technical bit.

Let us show you how we use a free API (ipstack.com *free for 10k requests a month) and basic PHP to make our website ready to serve different content for audiences from different countries (and even cities if we want to…)

So the first step for us would be getting the users IP address.

To accomplish that we are using something along the lines like this.

$ip = $_SERVER['REMOTE_ADDR']; // or for more advanced users $ip = $_SERVER['HTTP_CLIENT_IP']?$_SERVER['HTTP_CLIENT_IP']:($_SERVER['HTTP_X_FORWARDE‌​D_FOR']?$_SERVER['HTTP_X_FORWARDED_FOR']:$_SERVER['REMOTE_ADDR']);

After we do that, we are going to send an API request with PHP’s file content function to ipstack.com which kinda looks like this.

$geo = json_decode(file_get_contents("http://api.ipstack.com/" . $ip . "?access_key=" . $IPStack_apiKey . "&format=1")); $country = $geo->country_name;

The geo variable contains the IP stack Response and looks like this.

{
  "ip":"34.239.167.200",
  "type":"ipv4",
  "continent_code":"NA",
  "continent_name":"North America",
  "country_code":"US",
  "country_name":"United States",
  "region_code":"VA",
  "region_name":"Virginia",
  "city":"Ashburn",
  "zip":"20149",
  "latitude":39.0853,
  "longitude":-77.6452,
  "location":{
    "geoname_id":4744870,
    "capital":"Washington D.C.",
    "languages":[
      {
        "code":"en",
        "name":"English",
        "native":"English"
      }
    ],
    "country_flag":"http://assets.ipstack.com/flags/us.svg",
    "country_flag_emoji":"ud83cuddfaud83cuddf8",
    "country_flag_emoji_unicode":"U+1F1FA U+1F1F8",
    "calling_code":"1",
    "is_eu":false
  }
}

Quite a nice set of information for a free API. Like mentioned before, the first 10k requests are for free which will never get expired by most small business websites but the fact that you can even serve different content to different Post Codes makes it extremely powerful.

If you have a website with a lot of traffic have a look at the pricing table https://ipstack.com/product

So, that gives us the User location. All that’s left to do is serve the right content.

For that reason, all we basically did is set a variable in PHP that looks like this.

if ($country == "united States" ){
   $_SESSION["SESScountry"] = 'United States';
   setcookie('SESScountry', 'United States');
   include 'states.tpl.php';
}
if ($country == 'Australia') {
   $_SESSION["SESScountry"] = 'United States';
   setcookie('SESScountry', 'United States');
   include 'australia.tpl.php';
}

And that’s it.

Multi-location content enabled website in less than 20 lines of PHP code.

Worth to mention on that point here is the Session and Cookie settings for PHP.

Of course, we don’t want our user to hit the API on every single request they are making so we set a cookie and a session when they first hit our website and all subsequent requests get’s served based on their cookie value. This not only increases the usability of our API service, even more, it also gives us the capability of user grouping, retention etc.

The possibilities are huge.

If you are using Drupal 7, Note that the cookies need to have the SESS letters followed by lowercase letters, otherwise the varnish cache will stripe them out and gives you not the desired result.

Book a FREE consultation to discuss your next project

We'll discuss your goals and offer our expertise on your project design, assisting with scoping, implementation, or digital service innovation.

Book your project

Not feeling chatty?

Fill in our project planner