In part #1 I explained the benefit of displaying the location of your visitors to your visitors. Today I’ll show you how to do it. There is more then one way to do this, but I chose to show you how to do it with GeoPlugin.com. This service is not only free and easy to integrate, but also very flexible should you ever need more power.
You can use either PHP or Javascript to display the location of your visitors to your visitors. PHP is my preferred method because with PHP the location shows up immediately (with Javascript there can sometimes be a slight lag), and because with PHP other people visiting your site can’t see the script when they look at your source code (with Javascript they can). I’ll show you how to do it with both though, because the PHP method does require your files to have the .php extension. And I realize that renaming files might not be a desirable thing to do if you have pages indexed and ranked in the search engines.
First paste this code snippet at the top your page (your page should have the .php extension):
<?php
//load the geo location plugin
$geo_data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
//converts US state abbreviations to full state names
function stateName($stateAbbr) {
$states = array('AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming');
return $states[$stateAbbr];
}
?>
Then anywhere within your page paste one or more of the code snippets below. The snippets should be self explanatory, but if not, experiment… You’ll figure it out pretty quickly.
<?php /* If you copy and paste the code below as is it will output something like this: Houston, Texas, TX, United States */ ?>
<?php echo $geo_data['geoplugin_city']; ?>
<?php echo stateName($geo_data['geoplugin_region']); ?>
<?php echo $geo_data['geoplugin_region']; ?>
<?php echo $geo_data['geoplugin_countryName']; ?>
That’s basically all there is to it. There is more power under the hood, but the code above should be all that’s needed for the majority of people using the geo location service.
Doing this with Javascript is just as easy. First paste this line of code between the <head> and </head> tag of your page:
<script type="text/javascript" src="http://www.geoplugin.net/javascript.gp"></script>
Then anywhere within your page paste one or more of the code snippets below:
<script type="text/javascript">document.write(geoplugin_city());</script>
<script type="text/javascript">document.write(geoplugin_region());</script>
<script type="text/javascript">document.write(geoplugin_countryName());</script>
That’s it, this tactic has the potential to instantly increase your conversions. Why not try it now?
Great post!
Hey,
I did everything what you you wrote on integrating geo location with javascript, but i still cant location when i preview my web page. I think there is a code missing that would output the location on the web page.
sorry my mistake. it works now
This is a great tool to use when you’re marketing nationally but want to zero in on the local market to feel like you’re targeting them specifically.
I love it!