Matomo (fka PIWIK)

Matomo kommt von InnoCraft und hiess bis Januar 2018 PIWIK. Es konkurriert mit Google Analytics, ist aber Open Source und kann self-hosted betrieben werden.

Unsere Blogbeiträge zu Matomo.

Matomo sollte wegen Content Security Policy Headern am besten auf einem eigenen Host (z.B. analytics.example.com) und nicht unter der URL der Webseite betrieben werden. Für Matomo müssen die CSP recht offen sein („data:“ erlaubt), was von Security-Scannern moniert wird, und im Falle eines Mischens/Betrieb unter der Webseite (mysite.ch/analytics) die Webseite in einem Security-Ranking runterstufen könnte.

Installation

Ein aktuelles PHP unter Apache httpd verwenden, als Datenbank ist MariaDB empfohlen.

PHP: memory_limit >= 192M setzen.

Darüber hinaus wird benötigt:

yum -y install gd
yum -y install php php-pdo php-mysqlnd php-mbstring php-gd php-dom

Download:

rm -f /tmp/latest.zip*
rm -rf /tmp/matomo*
wget http://builds.matomo.org/latest.zip --output-document=/tmp/latest.zip
unzip -o /tmp/latest.zip -d /var/www/html/
rm -rf /var/www/html/matomo/tmp/*

chown -R apache:apache /var/www/html/matomo
find /var/www/html/matomo/tmp -type f -exec chmod 644 {} \;
find /var/www/html/matomo/tmp -type d -exec chmod 755 {} \;
find /var/www/html/matomo/tmp/assets -type f -exec chmod 644 {} \;
find /var/www/html/matomo/tmp/assets -type d -exec chmod 755 {} \;
find /var/www/html/matomo/tmp/cache -type f -exec chmod 644 {} \;
find /var/www/html/matomo/tmp/cache -type d -exec chmod 755 {} \;
find /var/www/html/matomo/tmp/tcpdf -type f -exec chmod 644 {} \;
find /var/www/html/matomo/tmp/tcpdf -type d -exec chmod 755 {} \;
find /var/www/html/matomo/tmp/templates_c -type f -exec chmod 644 {} \;
find /var/www/html/matomo/tmp/templates_c -type d -exec chmod 755 {} \;

SELinux:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/matomo/config(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/matomo/matomo.js"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/matomo/piwik.js"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/html/matomo/tmp(/.*)?"

restorecon -Fvr /var/www/html/matomo

Wenn Matomo hinter einem Reverse Proxy läuft:

/var/www/html/matomo/config/config.ini.php
[General]
; for Reverse Proxying https://matomo.org/faq/how-to-install/faq_98/
assume_secure_protocol = 1
proxy_client_headers[] = HTTP_X_FORWARDED_FOR
proxy_host_headers[] = HTTP_X_FORWARDED_HOST

Cronjob für automatische Datenarchivierung und -optimierung einrichten. Sind diese nicht aktiv, macht sich das nach einiger Zeit im Betrieb in kaputten Widgets in der Weboberfläche aufgrund von Timeouts wegen zu grosser Datenmengen bemerkbar.

# Matomo Auto Archiving
5 * * * * /usr/bin/php /var/www/html/matomo/console core:archive --url=https://analytics.example.com/ > /var/log/matomo-archive.log 1> /dev/null

Der gleiche Cronjob als Systemd-Timer:

/etc/systemd/system/matomo-archive.service
[Unit]
Description=Matomo Archive Service

[Service]
ExecStart=/usr/bin/php /var/www/html/matomo/console --matomo-domain=analytics.linuxfabrik.ch core:archive
Type=oneshot
User=root

[Install]
WantedBy=basic.target
/etc/systemd/system/matomo-archive.timer
[Unit]
Description=Matomo Archive Timer

[Timer]
OnCalendar=05:07:00
Unit=matomo-archive.service

[Install]
WantedBy=timers.target

Jetzt die Instanz per http://matomo/index.php aufrufen und im Setup-Wizard den ersten Benutzer anlegen.

Matomo ohne Tracking-Code (matomo.js)

Adblocker verhindern die Ausführung der matomo.js. Wer trotzdem grundlegende Statistiken haben möchte, kann den Webserver anweisen, in Realtime direkt nach Matomo zu loggen.

Zunächst das Python-Script https://github.com/matomo-org/matomo-log-analytics/blob/4.x-dev/import_logs.py z.B. in /usr/local/sbin/import_logs.py auf dem Webserver ablegen, welcher die Seite über die Domain-Namen verarbeitet (also beispielsweise auf dem Reverse Proxy). Eventuell den Shebang auf #!/usr/bin/python3 ändern, damit das Skript auch wirklich ausgeführt werden kann.

In Matomo muss unter Personal > Security ein neues „Auth Token“ angelegt werden. Die „Description“ könnte „srv-proxy site.example.com import_logs.py“ lauten, wenn es ein Token für „site.example.com“ auf dem Reverse Proxy „srv-proxy“ sein soll.

Dann:

chmod +x /usr/local/bin/import_logs.py
chown apache:apache /usr/local/bin/import_logs.py

Apache vHost-Definition anpassen:

vhost.conf
# Matomo Realtime Logging
LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" matomo
CustomLog "||/usr/local/sbin/import_logs.py \
--debug --enable-http-errors --enable-http-redirects --enable-bots \
--url=https://analytics.example.com --output=/var/log/matomo.log --recorders=1 \
--recorder-max-payload-size=1 --log-format-name=common_complete --token-auth=2ac48e93-2ca7-4df3-9e7c-c81b36d0a474 \
-" matomo
httpd -t
setsebool -P httpd_can_network_connect on
systemctl reload httpd

Logging:

tail -f /var/log/audit/audit.log /var/log/httpd/analytics.example.com-error.log /var/log/matomo.log

Quelle: https://github.com/matomo-org/matomo-log-analytics/#readme

Matomo Console Cheat Sheet

cd /var/www/html/matomo

./console list

./console cache:clear-caches

./console core:archive
./console core:archive --url=https://analytics.example.com > /var/log/piwik-archive.log  1> /dev/null
./console core:convert-to-utf8mb4 --show
./console core:run-scheduled-tasks
./console core:update

# You'll find the names of all sections in /var/www/html/matomo/config/global.ini.php

./console config:get --section=APISettings
./console config:get --section=ArrayCache
./console config:get --section=Cache
./console config:get --section=ChainedCache
./console config:get --section=CoreUpdater
./console config:get --section=Debug
./console config:get --section=Deletelogs
./console config:get --section=Deletereports
./console config:get --section=Development
./console config:get --section=FileCache
./console config:get --section=General
./console config:get --section=GeoIp2
./console config:get --section=Languages
./console config:get --section=Live
./console config:get --section=log
./console config:get --section=Login
./console config:get --section=LoginOIDC
./console config:get --section=mail
./console config:get --section=Plugins
./console config:get --section=PluginsInstalled
./console config:get --section=PrivacyManager
./console config:get --section=proxy
./console config:get --section=RedisCache
./console config:get --section=Segments
./console config:get --section=Tracker
./console config:get --section=TwoFactorAuth

./console config:set --section database --key=host --value=127.0.0.1

./console database:optimize-archive-tables 2016-0{1..9} 2016-1{0..2} 2017-0{1..9} 2017-1{0..2}

./console diagnostics:run

Update/Upgrade

Eine bestehende Matomo-Installation wird wie folgt per Kommandozeile aktualisiert:

Dateibasis aktualisieren:

INSTALL_DIR=/var/www/html/matomo

rm -f /tmp/latest.zip*
rm -rf /tmp/matomo*

wget http://builds.matomo.org/latest.zip --output-document=/tmp/latest.zip
unzip -o /tmp/latest.zip -d /tmp

mkdir -p $INSTALL_DIR
\cp -r /tmp/matomo/* $INSTALL_DIR/

rm -rf /tmp/matomo

chmod +w $INSTALL_DIR/matomo.js
chmod +w $INSTALL_DIR/config/config.ini.php
chown -R apache:apache $INSTALL_DIR
restorecon -Fvr $INSTALL_DIR

systemctl restart php-fpm.service

Das eigentliche Upgrade:

php $INSTALL_DIR/console --matomo-domain=analytics.example.com core:update
php $INSTALL_DIR/console --matomo-domain=analytics.example.com core:clear-caches
php $INSTALL_DIR/console --matomo-domain=analytics.example.com core:run-scheduled-tasks

GeoIP

Im Web-GUI unter System > Geolocation nachsehen, wie eine GeoIP-Datenbank (Maxmind empfohlen) aufzusetzen ist.

Troubleshooting

Probleme?
php /var/www/html/matomo/console diagnostics:run
Unable to write in the cache directory (var/www/html/matomo/tmp/templates_c/8a).
php /var/www/html/matomo/console core:clear-caches
Unable to execute check for https://analytics.example.com/tmp/: curl_exec: Connection timed out after 2000 milliseconds. Hostname requested was: analytics.example.com

Matomo rennt bei lokalem Aufruf von curl https://analytics.example.com in einen Timeout. Könnte an fehlenden /etc/hosts-Einträgen auf dem Matomo-Host liegen.

Built on 2023-09-21