This plugin, MatomoTracking
, is designed for Traefik as middleware to handle the tracking of requests using the Matomo analytics platform. The plugin inspects incoming HTTP requests, checks if tracking is enabled for the requested domain, and sends tracking data to Matomo if required.
The main purpose of this plugin is to enhance the accuracy of visitor tracking by overcoming limitations associated with the traditional JavaScript-based tracking method used by Matomo. Standard tracking relies on JavaScript code running in the user's browser, which can be blocked by certain browser extensions or privacy tools. By capturing tracking data directly on the server side, this plugin ensures that visitor information is accurately recorded even when JavaScript is disabled or blocked, providing a more reliable and comprehensive analytics solution.
Config
Struct
Purpose:
The Config
struct represents the configuration settings for the entire Matomo Tracking plugin. It defines the global settings that apply to all domains and the specific configurations for each domain that needs tracking.
Fields:
MatomoURL
matomo.php
file on the Matomo server, such as https://matomo.example.com/matomo.php
."https://matomo.example.com/matomo.php"
Domains
:
map[string]DomainConfig
string
) and the corresponding value is a DomainConfig
struct. This allows you to define tracking rules for multiple domains individually.domains:"www3.example.com":trackingEnabled: trueidSite: 21excludedPaths:- "/admin/*"- "\\.\\w{1,5}(\\?.+)?$"includedPaths:- "\\.(php|aspx)(\\?.*)?$""test.de":trackingEnabled: falseidSite: 456
DomainConfig
Struct
Purpose:
The DomainConfig
struct defines the tracking settings for a specific domain. Each domain that should be tracked (or explicitly not tracked) has a corresponding DomainConfig
entry.
Fields:
TrackingEnabled
:
bool
true
, the plugin will attempt to send tracking data to Matomo. If false
, tracking is disabled for that domain.IdSite
:
int
21
ExcludedPaths
:
[]string
(Slice of strings)excludedPaths:- "/admin/*"- "\\.\\w{1,5}(\\?.+)?$"
IncludedPaths
:
[]string
(Slice of strings)includedPaths:- "\\.(php|aspx)(\\?.*)?$"
Let's analyze how the configuration is used in the dynamic.yml
file:
Example Configuration
matomo-tracking:plugin:matomoTracking:matomoURL: "https://matomo.example.com/matomo.php"domains:"www3.example.com":trackingEnabled: trueidSite: 21excludedPaths:- "/admin/*"- "\\.\\w{1,5}(\\?.+)?$"includedPaths:- "\\.(php|aspx)(\\?.*)?$""test.de":trackingEnabled: falseidSite: 456
Explanation:
matomo-tracking
: This is the name of the plugin instance in Traefik.plugin
: Denotes that the following configuration is for a plugin.matomoTracking
: The name of your custom plugin. It references the code logic that you provided in your Go plugin implementation.matomoURL
: Specifies the Matomo server's URL where the tracking data should be sent.domains
: Contains specific tracking configurations for multiple domains:
"www3.example.com"
:
trackingEnabled: true
: Enables tracking for www3.example.com.
idSite: 21
: Uses 21
as the Matomo site ID.excludedPaths
: Specifies paths that should not be tracked. For example:
/admin/*
: Excludes all paths under /admin
.\\.\\w{1,5}(\\?.+)?$
: Excludes files with extensions between 1 and 5 characters, and optionally followed by query parameters.includedPaths
: Specifies paths that should be tracked, even if they are excluded.
For example:
\\.(php|aspx)(\\?.*)?$
: Includes files with extensions .php
and .aspx
, and optionally followed by query parameters"test.de"
:
trackingEnabled: false
: Disables tracking for test.de
.idSite: 456
: Uses 456
as the Matomo site ID.Main logic of the middleware:
Sends a tracking request to Matomo asynchronously:
User-Agent
and X-Forwarded-For
headers to identify the client.Checks if a given path matches any of the exclusion patterns:
excludedPaths
.false
immediately, indicating the path is not excludedincludedPaths
, the function returns false
, indicating that the path should not be excluded, as it is explicitly included.Step 1: Load/import the plugin into traefik
Edit your Traefik static configuration file (e.g., traefik.yml or traefik.toml), and add the plugin's Github repository:
Example: traefik.yml
:
experimental:plugins:matomoTracking:moduleName: "github.com/DIE-Bonn/MatomoTracking"version: "v1.0.2"
Ensure to use the current version tag.
Step 2: Configure Dynamic Configuration
Create a new or use an already existing dynamic configuration file (e.g., dynamic.yml) that defines how the plugin should behave:
Example dynamic.yml
:
http:middlewares:matomo-tracking:plugin:matomoTracking:matomoURL: "https://matomo-staging.die-bonn.de/matomo.php"domains:"www3.die-bonn.de":trackingEnabled: trueidSite: 21excludedPaths:- "/admin/*"- "\\.\\w{1,5}(\\?.+)?$"includedPaths:- "\\.(php|aspx)(\\?.*)?$""kansas-suche.de":trackingEnabled: falseidSite: 456
matomo-tracking
middleware, consisting of domain names with their individual tracking configuration.Step 3: Associate the middleware plugin to the entrypoint
Edit your Traefik static configuration file traefik.yml
:
Example traefik.yml
:
entryPoints:webinsecure:address: ":80"http:middlewares:- matomo-tracking@file
matomo-tracking
plugin can analyze all incoming requests to decide which requests will be sent to the matomo server for tracking purposes.Step 4: Restart Traefik
Start or restart traefik to load the plugin and apply the new configuration
docker compose down && docker compose up -d