Reverb

inspired by the letter M: MURAKAMI, MISTS of MOSCHINO and MITSUKO

The MADSTATIC NetworkDuke of NYCReverb

Dig for dirt or gold.

Height: # of Posts | Opacity: # of Comments
Activity: Last 30 Days
Tracking Cities With SlimStat

Tracking Cities With SlimStat

2,093 views Since it was Spewed out 8 years, 2 months ago at 12:40 am

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.

i18n\en-us\index.php

Look for the following at line 135:

"country" => "Countries",

Now add below it:

"city" => "Cities",

_config.php

Look for the following at line 124:

"country" => true, // Countries

Now add below it:

"city" => true, // Cities

_details.php

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 )
);
}

inc.stats.php

Find the line that has this:

$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:

Download GeoLiteCity.dat.gz

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.

Download slimstat_cities.zip

View A Post:

Sponsored Links

11 Comments, Comment or Ping

  1. GravatarKevin

    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!

  2. Did you make sure you added those extra fields to the “slimstat” table in your database? If you didn’t it will all fail.

  3. GravatarJackola

    I had the same issue. Stats have completely stopped, and I did add the fields to the database.

  4. GravatarJackola

    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.

  5. Thanks for pointing that out, I’ve disabled the smart quotes on this page so that you can now copy and paste

  6. 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à !

  7. 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

  8. Gravatarfoobar

    Can the commercial version of maxmind be used?

  9. Gravataraeris

    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!

  10. 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.

  11. GravatarThao

    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 :)

Reply to “Tracking Cities With SlimStat”

Strangely Interesting

  • Check this out. Someone took the time to draw a pic of Homer Simpson purely in CSS.

  • Not feeling safe at night? How about a table that turns into a shield and bat?

  • Seeing as I will most likely never climb Everest, this will have to do.¬† Someone has kindly created this 360 Degree panorama of the view from the top.

Attention

Some other random sites of mine

Sponsored Links

XBOX Live