Read this guide if
This guide assumes that you:
Matomo is an application that does mainly two things:
To achieve that result, several parts of Matomo come into play:
Matomo's codebase is composed of:
Plugins are not just targeted at 3rd party developers who want to customize Matomo: most of Matomo is implemented through plugins. Matomo Core is meant to be as small as possible.
As a result, there are two kinds of plugins:
plugins/folder) or through Matomo's Marketplace in the web interface
Here are the main files and folders composing Matomo's codebase:
Matomo uses Composer to install its dependencies (PHP libraries) into the
node_modules directory. The files from the
node_modules directory are committed to the Git repository, so users won't need to run any npm commands to keep packages up to date and this way we guarantee they use the right versions.
Matomo detects whether it has been installed by checking if the
config/config.ini.php file exists. During the installation this file will be created and Matomo knows whether the installation is in progress through a
[General]installation_in_progress=1 setting in the config file.
Matomo has a lot of configurations to change default behaviour. This configuration is meant to be edited by Matomo administrators.
Plugin developers may also use dependency injection to change the way Matomo works.
The entry point for the web application is
index.php in the root. This file initializes everything and calls the
A part of Matomo's long-term roadmap is to move more and more parts of Matomo's UI to Vue.js.
Read more about this in the "Working with Matomo's UI" guide.
The front controller will route an incoming HTTP request to a plugin controller based on URL parameters:
In this example, the front controller will call the action
index on the controller of the
CoreHome plugin. In the file
index() method will be called.
Plugin controllers return a string (usually HTML content) which is sent in the HTTP response.
If the specified controller action cannot be found, then Matomo checks if there is a matching widget or report having this name.
If one is found, it will call the
render() method of the widget or alternatively the report. This is done in the
CoreHome.renderReportWidget controller action and the matching widget or report is found in the ControllerResolver.
The HTTP reporting API works similarly to the web application. Its role is to serve reports in machine-readable formats (XML, JSON, …). It also serves information about various entities such as sites, users, goals, and more.
It has the same entry point and is also dispatched by the front controller.
This HTTP request will be processed like any other call to a controller: the plugin name is
API and no
action is given, which will fall back to
Piwik\Plugin\API\Controller class will be called, and it will dispatch the call to the targeted API, acting as a second front controller for API calls. In our example, the method
SEO.getRank means that the
Piwik\Plugin\SEO\API::getRank() method will be called.
API requests are authenticated using a
token_auth URL parameter and usually don't have a session loaded unless the
force_api_session=1 parameter is present. Learn more about Authentication in Matomo.
Its entry point is different from Matomo's web application and HTTP reporting API: it is through the
matomo.php file. Some older Matomo installations might still use
There are also various other Tracking Clients.
During tracking not all plugins are loaded. For performance reasons only the plugins that are identified as being needed during tracking will be loaded.
Any tracked data is stored in
log_* tables. These tables store all the raw data which is then later aggregated to report archives, see below. For each new visit and for each action a visitor takes a new row is created in their respective log tables. Some log tables such as
log_visit are also updated during a tracking request.
Matomo offers a command line API through the
./console script. This script uses the Symfony Console component.
Plugins can expose CLI commands that can be invoked like this:
Command classes are located in
plugins/*/Commands and are auto-detected by Matomo.
Read more about this in the "Matomo on the Command Line" guide.
Matomo lets you collect analytics data to then later retrieve as reports. Let's see what happens in-between and how Matomo models, processes and stores data.
The HTTP tracking API (i.e. the
Piwik\Tracker component) receives raw analytics data, which we call "Log data".
Log data is represented in PHP as
Piwik\Tracker\Visit objects, and is stored into the following tables:
log_visitcontains one entry per visit (returning visitor)
log_actioncontains all the type of actions possible on the website (e.g. unique URLs, page titles, download URLs…)
log_link_visit_actioncontains one entry per action of a visitor (page view, …)
log_conversioncontains conversions (actions that match goals) that happen during a visit
log_conversion_itemcontains e-commerce conversion items
Those tables are designed and optimized for fast insertions, as the tracking API needs to be as fast as possible in order to handle websites with heavy traffic.
The content of those tables (and their related PHP entities) is explained in more details in the "Matomo database schema" guide.
The log tables above are not designed or optimized for extracting high-level reports: aggregating the log entries to the day, week or month can become too intensive when there is a lot of data.
The archiving process will read log data (also known as raw data) and aggregate this data to produce "Archive data" (also known as reports). This is done for reports for a specific day. An example query that would count the number of visits for a specific day would look like
select count(*) as nb_visits from log_visit where idsite = 1 and visit_last_action_time >= '2021-08-04 00:00:00' and visit_last_action_time < '2021-08-05 00:00:00'.
All other periods (
year, and custom date
range) are generated by aggregating the reporting data from each sub period. This means for these periods we don't query the log data, but instead generate the reporting data for a week by aggregating the reports of each day within that week. To aggregate the data for a month it will aggregate the reports of various weeks and days within that month. To aggregate the data for the year, it will aggregate the reporting data of each month within the year. We don't generate these reports from the log data for these periods, as it would take too long to generate these reports for such a long period. The only exception are a few metrics like unique visitors and unique users which may be generated from raw data for these periods.
Archive data can be:
numeric metric records: simple numeric values (like the number of page views or the number of visists)
These are stored in the
archive_numeric_* tables. Values are stored as float.
table records: bidimensional data (can be numeric values as well as anything else), represented as
These are stored in the
DataTable objects are serialized to a string and compressed to be stored as
BLOB in the table.
Both numeric and table record objects stored in the database are named records to differentiate them from
DataTable objects manipulated and returned by Matomo's API that we name reports.
Every numeric metric or table record is processed and stored at each aggregation level: day, week and month. For example, that means that the "Entry pages" report is processed and stored for every day of the month as well as for every week, month, year and custom date range. Such data is redundant, but that is essential to guarantee fast performance when requesting a specific period.
Because Archive data must be fast to query, it is divided in separate tables per month. We will then have:
archive_numeric_2021_10: metrics for October 2021
archive_blob_2021_10: reports for October 2021
archive_numeric_2021_11: metrics for November 2021
archive_blob_2021_11: reports for November 2021
By default, Matomo generates these reports "on demand" every time they are requested in the browser or through the API. This can slow down Matomo, and therefore it is possible to configure auto archiving (sometimes also referred to as pre-archiving) which will instead generate these reports in the background periodically through a cron.
As shown above, data is stored either as numeric metrics or table records.
Sometimes, one persisted archive record can be used by different API reports.
You can read more details on how reports are created and served in the "Reports" guide.
Matomo Core only defines the main processes and behaviours. Plugins can extend and customize them through several extensibility points:
You can read more about this topic in the "Matomo's Extensibility Points" guide.
As a developer or system administrator you might also want to understand: