IPv6 Geolocation in Piwik

Piwik is a fantastic and free web analytics tool that unfortunately has very limited support for IPv6 Geolocation information. This has been requested time and time and time again – and with a little effort, it’s pretty easy to solve.

First, a script to download the GeoLite2 databases, seeing as the database downloader in Piwik only works with the older GeoLite databases – I have this saved as /etc/cron.monthly/piwik. Update the variables to suit your install:

# Script to download GeoLite2 databases for Piwik

# Variables

# Get files
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz

# Untar city database, clean up
tar zxvf GeoLite2-City.tar.gz --strip-components=1 --wildcards GeoLite2-City*/GeoLite2-City.mmdb
rm GeoLite2-City.tar.gz
chown $PIWIKUSER.$PIWIKUSER GeoLite2-City.mmdb

# Untar country database, clean up
tar zxvf GeoLite2-Country.tar.gz --strip-components=1 --wildcards GeoLite2-Country*/GeoLite2-Country.mmdb
rm GeoLite2-Country.tar.gz
chown $PIWIKUSER.$PIWIKUSER GeoLite2-Country.mmdb

Run the script once to ensure it works – you should end up with two files in Piwik’s misc directory.

Next, install and activate the GeoIP2 plugin:

Log in to Piwik as an admin user, then click to access the Settings menu. Click Plugins on the left, then click the link for installing plugins from the Marketplace.

Find Geo IP 2 from the list, click Install and then Activate.

Finally, click Geolocation on the left, then make sure Geo IP 2 is selected as the Location Provider (update and save if necessary). Over on the right, it should have correctly identified the country you’re in, as well as mentioning that this GeoIP implementation has access to city and country databases.

Note that Piwik’s System Check will warn you about File Integrity (because of the new database files) and Geolocation (because of the non-standard GeoIP provider) after configuring Geo IP 2 – this is perfectly normal.