Update 10/17/15 – Unfortunately it appears as though this is no longer working. If you’re able to get it to work, or if you have any further insight, please leave a comment below.
I recently started looking into IP geolocation/geotargeting for some projects. By “geolocation” or “geotargeting” I mean serving different content to users based upon their physical location as determined by their IP address. Now, as we all know IP addresses are far from perfect so the best you’re going to get seems to be ~80% accuracy. That said, there are still a lot of great things you can do for those people if you know where they are coming from.
An example for Detailed Image around holiday time would be showing different content to people who are past the safe cutoff date to receive a shipment before Christmas. For instance, on 12/21 a person in California might see an ad hyping up our gift certificates, while a visitor in New York would see an ad that they can still receive a ground shipment prior to Christmas.
And that’s just one example. I’m planning on doing some other things with geolocation for a future project, so I though it was time to finally figure out how I was going to do it.
And once I started looking into it I realized that the only way to do this with any kind of accuracy is to query a very large, actively maintained database of IP addresses and their corresponding locations. I narrowed it down to two options:
- GeoLite City – a free downloadable database that is update monthly and claims to be 79% accurate to a city level (which is where I got the 80% number above). They also offer a premium version that’s slightly more accurate.
- Google’s API, which maintains this database on their end for Google Maps and probably fifty other things that we don’t realize.
There are some definite advantages of having the GeoLite City database on your server, namely that you can query it on the server side prior to loading a page. To do this on any scale, however, you really shouldn’t install it as a MySQL database. Instead you should use their binary format and corresponding API.
This seems like an awful lot of work. Plus it requires that someone update the database monthly when new releases come out. Worth it for some people and some projects I’m sure. Not me though. I also don’t like using my resources when working on a project when I can use someone else’s resources, especially when it’s Google and it’s free and reliable.
So then I started digging into Google and I had a really, really hard time finding documentation. Finally I found this page with one example way down on the bottom, and with some common sense was able to use that to get what I needed.
In hopes of saving someone else some time, here’s a really simple way to implement IP geotargeting on your site in a few minutes. The simple function below gets all of the important info from Google (latitude, longitude, city, country, region) and writes it out to the page. What you do from there is up to your imagination!
Here’s all you have to do:
- Sign-up for an AJAX Search API Key
- That’s it! Check out a demo on my site.