Home » , , » An Accurate Geo Location IP Database from Wikimedia - A Guide for tracking user location

In these days User tracking is most important to every web developers. With HTML 5 Geolocation tracking API helps to track users' location. Unfortunately  Users must allow to website to track physical location of reader. But, We can get user physical location by tracking his IP. Maxmind is famous IP database provider. But They provide low accurate Database for free usage  If you want to track more accurate, You will need to buy a plan from maxmind service.


This post guide to get a 6 decimal digit Geo location details from IP for free.
I've discovered a free IP database from wikimedia source. They hide their service from search engine. But I did find it.

Call IP database from Javascript

<script  src="http://bits.wikimedia.org/geoiplookup"/>

This script will respond you following details of ip in json format

Geo = {"city":"(null)","country":"IN","lat":"7.000000","lon":"80.000000","IP":"165.157.80.121","netmask":"24"}

You can easily understand json response. But if you want to use these details for a useful purpose, You need to parse json response in a variable. I've write three examples with this Geo location .

Example 1: Track as Event in Google Analytics

This script will record your users' IP and location as an Event in your Google Analytic. (you need to install GA ansync script in head)

<script src="http://bits.wikimedia.org/geoiplookup"></script>
<script>
 function getip(json){
 _gaq.push(['_trackEvent', 'wiki Geo Data', Geo.city, "Long: " + Geo.lat + " Lati: " + Geo.lon ]);
_gaq.push(['_trackEvent', 'wiki User IP ', Geo.netmask, Geo.IP]);
}
getip();
</script>

Example 2 - Track as Custom variable in Piwik Analytic

Piwik does not support to event tracking in current version 1.11.1 So you need to track your users' location as custom event. This script will record your users'  location as a custom variable in your Piwik Analytic. (you need to install Piwik ansync script in head)

<script src="http://bits.wikimedia.org/geoiplookup"></script>
<script>
 function getip2(json){
_paq.push(['setCustomVariable','5','Wiki track', "City: " + Geo.city, " Long: " + Geo.lat + " Lati: " + Geo.lon, 'visit']);
_paq.push(['trackPageView']);
}
getip2();
</script>



Also you can use this script to make Google map.
Finally I thank to Wikimedia. You can start to track Geo location with Wikimedia with your own risk.