Script to automatically pull down the latest GeoIP city database and move it into place for Piwik analytics to use. This is accomplished with a single bash script and a cron job. This does not require root access to the server. This is ideal for a shared hosting environment.
Piwik is an excellent open analytics platform. It is able to utilize the MaxMind GeoIP country database for location statistics of your visitors. For even greater location accuracy, you can use the GeoIP city database.
MaxMind updates the database files (country and city) on
"the first Tuesday of each month".
This script will remove the old city database file, download the latest version (via wget), and then rename it for Piwik's use. The last step is needed as MaxMind names the file 'GeoLiteCity.dat', but Piwik expects the file to be called 'GeoIPCity.dat'.
The end result of this script is that you now have monthly updates of city locales for Piwik, with no manual maintenance intervention required.
- Grab the bash script. Recommended that you place it in the 'misc' folder of your Piwik installation (ex: /path/to/piwik/misc/)
cd /path/to/piwik/misc/
wget --no-check-certificate https://raw.githubusercontent.com/PeachFlame/geoip-update-for-piwik/master/update-geoip.sh
- Customize the script by setting the path to your Piwik installation: "PATH_TO_PIWIK"
- Add in a cron job to run the script monthly. Example:
25 2 9 * * /bin/sh /path/to/piwik/misc/update-geoip.sh
Alternatively, if you don't have command line access, or you are uncomfortable with SSH, you can simply FTP upload the file.
- Download the update-geoip.sh script from this GitHub repo
- Edit the file, changing the path to your Piwik installation: "PATH_TO_PIWIK"
- FTP upload it to your Piwik server. Recommended that you place it in the 'misc' folder of your Piwik installation (ex: /path/to/piwik/misc/)
- Add in a cron job to run the script monthly. Example:
25 2 9 * * /bin/sh /path/to/piwik/misc/update-geoip.sh
- This script can be run by a regular, non-privileged, user. This is ideal for a shared hosting environment where the user does not have root access to install via yum/apt. If you do have root access, you may want to consider using MaxMind's official program instead: https://github.com/maxmind/geoipupdate
- It is suggested that you set the cron to run on the 9th of the month (or later) so as not to slam MaxMind's servers as soon as they publish their updated databases. This is a courtesy to MaxMind, but not a requirement for you. It really depends on how much you need the most up-to-date City information. You are using their free database afterall. If you absolutely need city information as quickly as possible, consider buying a commercial subscription.