Importing logs within Matomo Analytics

As you know Matomo is a database on which you can import data within. For some reasons you may be interested in importing your log server within it, for example: to track bots activities, get historical data...

Importing logs in Matomo Analytics is straightforward and can easily be done thanks to a Python script.

Here is the syntax:

python3 /var/www/html/matomo/piwik/misc/log-analytics/import_logs.py --url=http://localhost/matomo/piwik/ --idsite=45 /var/log/apache2/access.log.1

Here are the explanations:

  • python3: as the script which allows to import data is written in Python, you need to indicate the version of python you would like to use, in my case it is Python3.
  • /var/www/html/matomo/piwik/misc/log-analytics/import_logs.py is the location of your Python script on your server. It is installed by default when you installed Matomo.
  • --url=something corresponds to the location of your Matomo instance.
  • --idsite=something corresponds to the website in which you would like to insert data within.
  • /var/log/apache2/access.log.1 is the location of the server logs on your server that you would like to import.

However if you want to import those data regularly you will need to have a cron running which may be a bit more complicated if you never heard about cron before.

Cron is nothing more than saying to your server to execute a task at a given time. This task will exactly be the one of the import.

Example:

19 13 * * * python3 /var/www/html/matomo/piwik/misc/log-analytics/import_logs.py --url=http://localhost/matomo/piwik/ --idsite=46 /var/log/apache2/access.log.1

This given cron will import the data of the last day at 13:19 every day. And that's all you need to know.

The only thing that you need to care of is the name of your log files. In my case I am lucky because access.log.1 always corresponds to the data of the previous day as a result I don't need to make extra changes.

Last modified: Monday, 23 September 2019, 12:08 PM