Skip to content
July 27, 2012 / Danii Oliver

GeoLocation for GeoTargeting (the Basics)

Geotargeting is content creation, content restriction and content display based on a viewers current or preferences location.

As technology becomes more and more sophisticated – thanks to the pioneers at Google & 1984’s Big Brother notions – content producers gain more control over what their viewers can see and the information they receive based on their whereabouts. This is extremely helpful to viewers at it provided them with easily accessible relevant information. If a person google’s pizza in Philadelphia there is no reason for the most popular pizza place in the whole country to come up. If that pizza place is in New York it does the Philadelphian no good.

Rather than viewers being given an over all higher rank of sites or general content that they must search through, Geotrageting gives viewer the information they want and need relevant to them right away. Less micro searches, less researching and less time spend trying to narrow down the information you need.

Geolocation for geotargeting can be accomplished by  IP address, Lat-Long coordinates, country, region, state, city, zip code, organization and ISP.

Server location and top level domain like .fr for France. Since most sites use .com and servers can be offsite for cost efficiency.

Content targeted to a location in a general global site. au.example.com or ca.example.com

You can geotarget a whole site individual subfolders and subdomains. Verify the top level site and the subfolders will automatically geotarget, however, subdomains must be verified individually.

To avoid extra server calls don’t asks what is the current location every visit. Instead, check to see if there is a cookie. If there is a cookie read the cookie and redirect the the appropriate site. If there is no cookie then find location, set the cookie then redirect to the appropriate site.

NOTE *THE FOLLOWING CODE IS NOT MY FINAL CODE!
Rather the following is a starting base which I have gotten down to 36 lines of very simple code with a “SET” algorithm and a “GET” algorithm that both include  algorithms the redirect based on the viewer’s location to a geotargeted site.

*Email, text, or tweet to hire/contract this service setup for your site. This can be done with geeolocation, IP address, Lat-Long, city, state, zip code, region, country.

<?php 
//$setLoc
//$getLoc
//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  include('ip2locationlite.class.php');
  $ipLite = new ip2location_lite;
  $ipLite->setKey('52af9d9bb580a4d36b4890b0128e3fff19827cf757cbcbed55aa03c8929f0ef8');

  $setLoc = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
  if ($setLoc['statusCode'] == 'OK') {
    $data = base64_encode(serialize($setLoc));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
    setcookie('statelocation', $setLoc['regionName'], time()+3600*24*7);
    var_dump($setLoc); //TESTING THE RETURN DATA
    //echo $data; #SEE THE SERIALIZED DATA
    $setLoc = $setLoc['regionName'];
    echo $setLoc;
    //put redirects here
  }
}else {
  if(isset($_COOKIE['statelocation'])){
    //if is set it will echo if not it will cause a problem
    $getLoc = $_COOKIE['statelocation'];
    if($getLoc =='-'){
      $getLoc = "Not Connected";
      //put redirects here
    }
    echo $getLoc;

}
  //$visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
} 
if($getLoc == '-'){
      $getLoc = "Not Connected";
}
if($setLoc == '-'){
      $setLoc = "Not Connected";
}
echo "<br/> Returned City: " . $getLoc;
echo "<br/> Setting City: " . $setLoc;
//setcookie('geolocation', $locations['regionName'], time()+3600*24*7);
//setcookie('hardlocation', 'NY', time()+(60*60*24*90));
//echo "HTML is now being set!";
#OLD HARD COOKIE
//setcookie('location', 'NY', time()+(60*60*24*90)); //must come before all other html 
?>
<?php 
#TESTING GEO FINDING SETUP IS FOR REPORTING AND WILL NOT SET A COOKIE BECAUSE THE REPORT CREATES HTML BEFORE IT SET THE COOKIE
#SEE ABOVE FOR PROPER SEQUENCING 
/*
include('ip2locationlite.class.php');

//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('52af9d9bb580a4d36b4890b0128e3fff19827cf757cbcbed55aa03c8929f0ef8');

//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();

//Getting the result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations) && is_array($locations)) {
  foreach ($locations as $field => $val) {
    echo $field . ' : ' . $val . "<br />\n";
  }
}
echo "</p>\n";

//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />\n";
  }
} else {
  echo "No errors" . "<br />\n";
}
echo "</p>\n";

//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  $ipLite = new ip2location_lite;
  $ipLite->setKey('52af9d9bb580a4d36b4890b0128e3fff19827cf757cbcbed55aa03c8929f0ef8');

  $visitorGeolocation = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
  if ($visitorGeolocation['statusCode'] == 'OK') {
    $data = base64_encode(serialize($visitorGeolocation));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
    setcookie('location', $locations['regionName'], time()+3600*24*7);
  }
}else{
  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
} 
var_dump($visitorGeolocation);

$loc = 0;
if(isset($_COOKIE['geolocation'])){
//if is set it will echo if not it will cause a problem
$loc = $_COOKIE['geolocation'];
}
//echo $loc; 
if ($loc === "NEW YORK") {
    echo ": New York is what you will see.";
    //header("Location: ../default.php?request=home_upcoming");
    //exit;
  }else{
    echo ": LosAngeles is what you will see.";
    //header("Location: ../LosAngeles.php?request=la_upcoming");
    //exit;
  }*/
?>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: