Tracking Cities With SlimStat
Although Slimstat already makes use of the great Ip-to-country database provided by webhosting.info, I like to be able to (if it's free) track cities of visitors. Maxmind is a company that has released a "lite" version of an ip-to-city database and the appropriate API under the GPL. This database is only accurate to 60% for cities but that's still better than nothing. Here's how to implement the proper changes into your slimstat installation.
The idea behind these modifications is to make as few changes as possible so future updates to slimstat are easily done. Otherwise they could have been implemented into the setup file etc. (perhaps in future releases Stephen might want to adopt the Maxmind database). Before you make any changes it is recommended that you backup your database. I take no responsibilty if something goes wrong.
First you'll need to add 3 additional fields to the table "slimstat" in your database. Add the following 3 after the country field. We will store the latitude and longitude to provide the coordinates for my plugin which will allow mapping of visitors.
city varchar(50), latitude varchar(12), longitude varchar(12)
We will also need to edit the following files (if you prefer you can download a zip of these modified files at the bottom of the page):
- i18n\en-us\index.php
- _config.php
- _details.php
- inc.stats.php
Depending on what language you are using you'll want to edit the appropriate index.php located in the language directory that you use.
Look for the following at line 135:
Look for the following at line 124:
"country" => true, // Countries
Look for the following at line 268:
if ( $config->show_modules["country"] && SlimStat::is_ip_to_country_installed() && !isset( $filters["filter_country"] ) ) {
print SlimStat::render_module(
$config->i18n->module_titles["country"],
percentage_table( "country", $config->truncate, "", $filters )
);
}
Add directly below it this snippet:
if ( $config->show_modules["city"] && SlimStat::is_ip_to_country_installed() && !isset( $filters["filter_city"] ) ) {
print SlimStat::render_module(
$config->i18n->module_titles["city"],
percentage_table( "city", $config->truncate, "", $filters )
);
}
$stat["country"] = SlimStat::my_esc( $this->_determine_country( $stat["remote_ip"] ) );
Now add this directly below it:
if (file_exists(realpath( dirname( __FILE__ ) )."/GeoLiteCity.dat")){
include(realpath( dirname( __FILE__ ) )."/geoipcity.inc");
$gi = geoip_open(realpath( dirname( __FILE__ ) )."/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, $stat["remote_ip"]);
$stat["city"] = $record->city;
if (empty($stat["city"])) {
$stat["city"] = "Unknown";
}
$stat["latitude"] = $record->latitude;
$stat["longitude"] = $record->longitude;
geoip_close($gi);
} else {
$stat["city"] = "Unknown";
$stat["latitude"] = "Unknown";
$stat["longitude"] = "Unknown";
}
That's it for the files, what you will need to do next is obtain the database that has the cities from Maxmind located here:
You will also need to obtain the php API files from here: Maxmind PHP API
The files you will need are:
geoip.inc
geoipcity.inc
geoipregionvars.php
Once you have all those files you'll want to extract GeoLiteCity.dat file from the zip archive and along with the 3 api files upload them to the /slimstat directory
Okay that's it now it'll start recording the cities. The database also contains the province/state however I figure that isn't necessary since we want to keep the database and SlimStat as Slim and quick as possible, besides the latitude & longitude more than help us figure out where it is located
If you want to be able to view the city information in the "Paths" be sure to get my updated plugin. Also if you wish to have the locations plotted onto a map get my GMapped plugin
Here are all the modified files in an archive. Keep in mind if you have modified your SlimStat install you're probably safer off making the changes manually as these files may overwrite any changes you have made. Again backup everything first.






11 Comments, Comment or Ping
Sounds good, so I tried this. Modified the 4 files, uploaded the other 4. No stats at all recorded for my site. Undid the 4 changes, stats resumed. Well, I like the idea, but not enough to troubleshoot it. Like I said, a good idea, so I hope others have better luck!
Jun 10th, 2006
Did you make sure you added those extra fields to the “slimstat” table in your database? If you didn’t it will all fail.
Jun 10th, 2006
I had the same issue. Stats have completely stopped, and I did add the fields to the database.
Jun 14th, 2006
Got it fixed!
It was because the blocks of code pasted from this page have smart quotes. I replaced them all with regular quotes and everything works fine now.
Jun 14th, 2006
Thanks for pointing that out, I’ve disabled the smart quotes on this page so that you can now copy and paste
Jun 19th, 2006
Hi !
Thanks for this great plugin !
During installation I noticed there were one little detail missing. Here it is.
The file “i18n\en-us\index.php” or the appropriate index.php need a second modification near the line 101 :
After the line
“country” => “Country”,
just add the following :
“city” => “City”,
The problem is visible only with language other than us-english.
For example, for french language :
“country” => “Pays”,
“city” => “Ville”,
Voilà !
Jul 17th, 2006
Thanks! I only developed for English, therefore if you utilize any of the other translations, you’ll need to enter your equivalent language’s word for “City” as pointed out by Dominique
Jul 17th, 2006
Can the commercial version of maxmind be used?
Jan 7th, 2007
Hi,
Is there any way to make this work with slimstat javascript? It does show up, but the cities just show unknown. Any suggestions would be greatly appreciated!
Mar 6th, 2007
I haven’t had a chance to take a look but seeing as the API from maxmind is the same one used for the free and commercial version I believe it will work.
As for working with the javascript version I don’t use that method of tracking so when I have more time I’ll take a look at it to see if there’s some modifications to get it working. I assumed it would work fine seeing as the cities essentially works with the ip data and the javascript version definitely stores that.
Mar 6th, 2007
Hi!
I’ve noticed that since I switched to fr-fr, the longitude and latitude format are displayed differently in the database.
Therefore GMapped doesn’t recognize the coordinates.
Eg. Instead of having latitude = 49.4833 and longitude = 0.5833000000
it gives me 49,4833 and 0,5833000000
how do I fix that?
Thanks
Feb 5th, 2008
Reply to “Tracking Cities With SlimStat”