Before we get into using geotargeting, let’s quickly look at what it is 1st.
Geotargeting, or geographical targeting, is a way for a website to display content specific to a geographical location.
Whenever you visit any web site on the Internet, your IP address is logged. Your IP address is given to you by your Internet Service Provider (ISP). Each ISP is assigned a unique range of IP’s. Those IP’s are logged in a database which can be referenced, and thus, find out from which country you are from, and in the case of the larger countries like the US and the UK, even what state/county or even what city you are in!
Generally, smaller companies are not bothered with this information, as they believe it would be too difficult to use.
Well, hopefully in this article, I will show you how easy it is to use and drastically increase your online profits.
There are 2 ways you can use geotargeting on your site.
Geotargeting happens on the server, before the page is sent to the browser. When a user requests a page from the server, the server queries the user’s IP address against a database of worldwide IP addresses. The database identifies the country (sometimes even city!) of the user’s IP address, and then the server decides what to do.
What can you use this for in your marketing efforts?
If you are creative with geotargeting, you can drastically increase your sales, and/or, improve customer loyalty.
One fair warning if you are on shared hosting! These databases are quite big. Around 60K entries. Searching through that puts a fair load on your server, and I know of people who has had their shared hosting suspended for using this. Best is to use this only if you have a dedicated server.
Now, let’s look at some code!
I would recommend downloading the GeoIP Lite database from MaxMind because it is free and updated on a monthly basis.
Getting GeoIP data into your database is quite an easy process and only requires a single table. Then, when a visitor loads a page on your website, you can convert their IP address to a value in the database and match it up with what country they are in. Here is a PHP script you can use:
// connect to the database $db = mysql_connect("localhost", "username", "password"); mysql_select_db("databasename", $db); // get the user ip address and convert to an array $ip = $_SERVER['REMOTE_ADDR']; $ip_arr = explode(".", $ip); // convert the ip address to an ip number $ip_num = 16777216*$ip_arr[0] + 65536*$ip_arr[1] + 256*$ip_arr[2] + $ip_arr[3]; // get the country code $result = mysql_query("SELECT country FROM geo_ip WHERE $ip_num BETWEEN begin_num AND end_num"); $row = mysql_fetch_array($result); $country = $row['country'];
The “$country” variable will now contain the user’s country code (US, GB, CA, etc) which you can then use to determine what content to show or where to redirect them.
For a better explanation on how to use the database, read the official instructions.
What To Do With The Country Code
If you do any programming, you will have already figured out what you can do once you have the visitor’s country code. For those of you who are not familiar with a programming language, here is an example.
Lets say you have three pages set up. One page is specifically designed for United States visitors, one is for Canada visitors, and the last page is for all other visitors. You would use a simple “if-then” statement that checks the country code and sends the visitor to the correct page. Here is the PHP code for this example:
if ($country == 'US') { header('Location: /united_states.html'); } else if ($country == 'CA') { header('Location: /canada.html'); } else { header('Location: /other.html'); }
I hope this has given you some ideas of what can be done with geotargeting!
Tags: Geotarget, php geotargeting, maxmind geotargetting with php
Very informative and timely article Richard. Thank you!
[...] Using Geotargeting… Read More [...]
RSS feed for comments on this post · TrackBack URI
Our deepest fear is not that we are inadequate. Our deepest fear is that we are powerful beyond measure. It is our light, not our darkness that most frightens us. We ask ourselves, Who am I to be brilliant, gorgeous, talented, fabulous? Actually, who are you not to be? You are a child of God. Your playing small does not serve the world. There is nothing enlightened about shrinking so that other people won’t feel insecure around you. We are all meant to shine, as children do. We were born to make manifest the glory of God that is within us. It’s not just in some of us; it’s in everyone. And as we let our own light shine, we unconsciously give other people permission to do the same. As we are liberated from our own fear, our presence automatically liberates others.
Jason Rakowski said,
February 7, 2008 @ 4:53 pmI found your site on technorati and read a few of your other posts. Keep up the good work. I just added your RSS feed to my Google News Reader. Looking forward to reading more from you.
Jason Rakowski