Geo-Targeting Content

I’ve recently been spending some time looking into Geo-Targeting content and links on my websites. The idea being that you supply specific content or links to visitors in different countries. This can be very useful for affiliate marketing, where it makes more sense to send US visitors to a affiliate store in the US and UK visitors to an affiliate store in the UK. Visitors are more likely to convert on a store in their own country. Even if the store does then redirect them to the more local store, your affiliate tracking cookie may not carry over, thus you lose commission on the click through.

So how do you determine the country a visitor is from? There are numerous databases that map IP addresses to specific countries. The most accurate and up to date ones cost money, but there are still very good databases available for free. The one I used is from MaxMind.

To query this database there is a lovely PEAR module that does all the hard work for you. This is Net_GeoIP. PEAR modules can sometimes be a little bit of faff to install, but the CPanel on Hostgator provide a nice interface for installing PEAR modules quickly and easily.

Here is my sample shortcode WP plugin to Geo-Target a link.


<?php
/*
Plugin Name: Fubaby Geo-Target Link
Plugin URI: http://www.fubaby.com
Description: Adds ability to provide geotargeted instances of a link for specific countries. Examples: fooo
Version: 0.3
Author: Arthur Yarwood
*/
/*

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// Initialise plugin, query client country code and save to session cookie. 
function fubaby_geotarg_init()
{
	$ip = "";
	if( !empty( $_SERVER['HTTP_CLIENT_IP'] ) )
	{
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	}
	elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
	{
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else
	{
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	
  require_once "Net/GeoIP.php";

  // See if we have already determined client country and saved it to a cookie.
  $clientCountryCode = "";
  if (isset($_COOKIE['ClientCountry']))
  {
  	$clientCountryCode = $_COOKIE['ClientCountry'];
  }
  else
  {
	  try 
	  {
	  	$geoip = Net_GeoIP::getInstance("/edit me!/GeoIP/GeoIP.dat");
	  	$clientCountryCode = strtolower($geoip->lookupCountryCode($ip));
	  	setcookie('ClientCountry', $clientCountryCode, 0, "/", str_replace('http://www','',get_bloginfo('url')));
	  	$_COOKIE['ClientCountry'] = $clientCountryCode;
	  } 
	  catch (Exception $e) 
	  {
	      // Handle exception
	      error_log('Caught exception: ' .  $e->getMessage());
	  }
  }
}

add_action('init', 'fubaby_geotarg_init');

// Shortcode handler to create an anchor link using geotargeting to offer links 
// unique to the visitors country.
function fubaby_geotarg_link($attrs, $content, $name)
{	
	$clientCountryCode = $_COOKIE['ClientCountry'];
	
  $finalLink = "";
  $defaultLink = "";
  foreach($attrs as $attrKey => $attrVal )
  {
  	// If no default link is given, the first link attr will be default.
    if ( $defaultLink == "" ) $defaultLink = $attrVal;
    
    // Attribute key will be an underscore delimitered list of country codes.
    $attrCCodes = explode('_', $attrKey);
    
    if ( in_array($clientCountryCode, $attrCCodes) )
    {
    	// Found link for client country.
    	$finalLink = $attrVal;
    	break;
    }
    
    // See if default is in here before looping.
    if ( in_array("def", $attrCCodes) )
    {
    	$defaultLink = $attrVal;
    }
  }
	
  if ( $finalLink == "" ) $finalLink = $defaultLink;
  
  $results = "<a href=\"$finalLink\" ";
  $results .= " id=\"country_$clientCountryCode\" rel=\"nofollow\">";
  $results .= $content;
  $results .= "</a>";
  return $results;
}

add_shortcode('fu_geo_link', 'fubaby_geotarg_link');
?>

You will need to change the path to where you have downloaded the geo ip database. The plugin will save the country that is found in the database in a cookie, to save having to search for it repeatedly, which would be less than optimal.

To use this shortcode use the syntax like this:-


[fu_geo_link def="http://default.com" uk="http://linkforUKvisitors.com" us_ca="http://linkforUSandCAvisitors.com"]]Link text here.[[/fu_geo_link]

The attribute names form a list of underscore separate two letter country codes. If the visitor is in any of these countries, they’ll be presented with the link in that attributes value. If none of the attributes match the visitors country, they’ll be given the link specified by the ‘def’ attribute or just the first link given in the absence of a default link.

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>