Auto archive in Matomo

As I’ve discovered, setting up auto archival of Matomo reports makes Matomo faster (and also helps automate downloading of the geolocation databases). Generally speaking, follow the official auto-archiving guide .. BUT the cron job instructions don’t put things in the “correct” cron.hourly folder, and my OCD needed to fix that. 😉

Create the following file: /etc/cron.hourly/matomo-archive

#!/bin/sh
# Script to automate Matomo report archival

/usr/bin/php /path/to/matomo/console core:archive --url=https://matomo.example.com/ > /var/log/matomo-archive.log

Update the bits in yellow to whatever is appropriate for you. Run the cron job manually – it may take quite some time! Afterwards, make sure you’ve disabled browser triggers for Matomo auto archival (per the official guide).

IPv6 Geolocation in Piwik

Note: As of Matomo 3.5.0 or so, none of the below is needed any more as Matomo now supports the Geo IP 2 databases natively. If you followed these instructions, be sure to remove the cron script below – and configure automatic archival of Matomo reports.

 

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:

#!/bin/sh
# Script to download GeoLite2 databases for Piwik

# Variables
PIWIKDIR="/var/www/piwik/misc"
PIWIKUSER="www-data"

# Get files
cd $PIWIKDIR
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.