The extension integrates Matomo Analytics easily into TYPO3. The extension
supports Matomo 5.
The extension takes the
Content Security Policy (CSP) into
account: a nonce attribute is added to the script tag, if the feature is enabled
for frontend.
Tip
The Matomo Widgets extension
provides dashboard widgets with reports directly in TYPO3.
When to use this extension
This extension is useful if you want to add further Matomo method calls
dependent on certain conditions — such as custom dimensions or setting the user
id. Another option is to enable the Matomo tag manager and add data layer
variables. PSR-14 events are available for these purposes. Also have a
look at the use cases.
When not to use this extension
If you only use Matomo's default tracking code or have only static values
for additional Matomo method calls, insert the JavaScript snippet directly
via TypoScript or the Fluid template.
Limitation
This extension can only embed one tracking code for one Matomo instance. If you
have to add multiple tracking codes for one or more Matomo instances you cannot
use this extension and have to do it on your own.
Enabling this option may track personal data: Someone downloads a page
from your website and stores it locally. Then the user opens it and the
user will be tracked. The URL might look like
file:///C:/Users/myname/AppData/Local/...
Activating this option has a positive impact on privacy.
By default, Matomo will send campaign parameters (mtm, utm, etc.) to the
tracker and record that information. Some privacy regulations may not allow
for this information to be collected. If this applies to you, activate this
method to prevent campaign parameters from being sent to the tracker.
If you use the Matomo Tag Manager, enter the container IDs in this field.
If no value is given, the tag manager code will not be embedded in the web
page.
A data object is available for use in the PSR-14 events:
JavaScriptCode
The
\Brotkrueml\MatomoIntegration\Code\JavaScriptCode object holds a piece
of arbitrary JavaScript code used in the BeforeTrackPageViewEvent,
AfterTrackPageViewEvent and AddToDataLayerEvent events. This
object is necessary to distinguish between a "normal" string and JavaScript code
for later embedding.
Example:
$javaScriptCode = new \Brotkrueml\MatomoIntegration\Code\JavaScriptCode(
'/* some JavaScript code */'
);
Copied!
The object provides the following method:
__toString(): string
__toString(): string
Returns the JavaScript code.
PSR-14 events
To enrich Matomo's JavaScript tracking code with additional calls,
PSR-14 events are available. You can draw inspiration from the Use cases
chapter.
<?phpdeclare(strict_types=1);
namespaceYourVender\YourExtension\Matomo;
useBrotkrueml\MatomoIntegration\Event\ModifySiteConfigurationEvent;
useTYPO3\CMS\Core\Attribute\AsEventListener;
#[AsEventListener(
identifier: 'your-vendor/your-extension/modify-matomo-site-id',
)]
final readonly classModifyMatomoSiteId{
publicfunction__invoke(ModifySiteConfigurationEvent $event): void{
if ($event->getRequest()->getAttribute('language')->getLanguageId() === 1) {
// Override the site ID when in another language
$event->setSiteId(42);
}
}
}
Copied!
EnrichScriptTagEvent
With this event you can add attributes to the surrounding
<script> tag.
For a concrete usage have a look into the
use cases.
Adds a Matomo method call for the given method and optional parameters.
The value can be of type:
array,
bool,
int,
float,
string or
\Brotkrueml\MatomoIntegration\Code\JavaScriptCode.
<?phpdeclare(strict_types=1);
namespaceYourVender\YourExtension\Matomo;
useBrotkrueml\MatomoIntegration\Code\JavaScriptCode;
useBrotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent;
useTYPO3\CMS\Core\Attribute\AsEventListener;
#[AsEventListener(
identifier: 'your-vendor/your-extension/set-document-title-example',
)]
final readonly classSetDocumentTitleExample{
publicfunction__invoke(BeforeTrackPageViewEvent $event): void{
// Set the document title
$event->addMatomoMethodCall('setDocumentTitle', 'Some Document Title');
// OR:// Add some JavaScript code
$event->addJavaScriptCode('function getDocumentTitle { return "Some Document Title"; }');
// Set the document title
$event->addMatomoMethodCall('setDocumentTitle', new JavaScriptCode('getDocumentTitle()'));
}
}
Copied!
TrackSiteSearchEvent
The event is useful for tracking site search metrics, such as the keyword or the
number of results. Especially the number of results can be interesting, since
Matomo displays a list of keywords without results.
Further information can be found on the Matomo website:
<?phpdeclare(strict_types=1);
namespaceYourVender\YourExtension\Matomo;
useBrotkrueml\MatomoIntegration\Event\EnrichTrackPageViewEvent;
useTYPO3\CMS\Core\Attribute\AsEventListener;
#[AsEventListener(
identifier: 'your-vendor/your-extension/some-enrich-track-page-view-example',
)]
final readonly classSomeEnrichTrackPageViewExample{
publicfunction__invoke(EnrichTrackPageViewEvent $event): void{
// You can set another page title
$event->setPageTitle('Some Page Title');
// And/or you can set a custom dimension only for the track page view call
$event->addCustomDimension(3, 'Some Custom Dimension Value');
}
}
Copied!
AfterTrackPageViewEvent
This event can be used to add calls after the embedding of the
trackPageView code.
Adds a Matomo method call for the given method and optional parameters.
The value can be of type:
array,
bool,
int,
float,
string or
\Brotkrueml\MatomoIntegration\Code\JavaScriptCode.
Depending on the URL structure, it is not easy to accumulate page views for one
or more sections on a website. However, you can use an "action"
custom dimension to measure these sections (such as "Blog", "Jobs",
"Videos").
In this use case, a page type is set for a specific page when a configured
section is available in the root line. The page type should only be available
for the
trackPageView call, so we implement an event listener based on the
EnrichTrackPageViewEvent event.
The given use case results in the following code when the current page id or a
parent page id is 167:
To separate the configuration from the implementation, the ID of the custom
dimension and the configuration of the page types are also injected.
The
$pageTypes argument is a simple associative array with the page ID
of the parent page of a section as the key and the value of the custom
dimension as the value of the array.
We need to inject the custom dimension ID and the page types configuration
into the event listener:
EXT:your_extension/Configuration/Services.yaml
YourVender\YourExtension\EventListener\AddPageTypeToMatomoTracking:arguments:$customDimensionId:2$pageTypes:# key: parent page ID, value: value to set for the custom dimension167:Blog218:Jobs112:Videos# ... and possibly some more typestags:-name:event.listeneridentifier:'your-ext/addPageTypeToMatomoTracking'
Use a separate field in the page properties to select the Matomo page type.
Colour scheme as custom dimension
If you provide your page with a light and a dark colour scheme, it might be
interesting to see how many visitors prefer which colour scheme. This can be
analysed in Matomo with a "visit" custom dimension.
In contrast to the use case above, where the custom dimension should only be
used for tracking a page view, this custom dimension can be defined "globally"
so we can use the BeforeTrackPageViewEvent event.
The event provides an
addMatomoMethodCall() method. With this method
you can insert any JavaScript code, so be careful what you do. In this
example, we use the
window.matchMedia() function to get the colour
scheme currently in use
As in the use case above, the ID of the custom dimension is injected via
dependency injection.
In one project we have person-related parts in the URL, namely tokens. They are
used after submitting a form to request access to downloads or videos. These
tokens are unique and can be used to identify a user through other sources
and retrieve their Matomo visit data.
To be compliant with the GDPR and to simplify the analysis of URLs (the token
is not needed here), the token is stripped from the URL. This can be achieved
by setting a custom URL and the BeforeTrackPageViewEvent event of this
extension.
The given use case results in the following code. The original URL is something
like https://example.com/downloads/detail/some-download/hz6dFgz9/, where
hz6dFgz9 is the token to be removed from the logged URL.
The TYPO3 request object returns the information of the current URL, so the
last part of the URL must be removed and set for Matomo's setCustomUrl
method call.
<?phpdeclare(strict_types=1);
namespaceYourVendor\YourExtension\EventListener;
useBrotkrueml\MatomoIntegration\Event\BeforeTrackPageViewEvent;
usePsr\Http\Message\ServerRequestInterface;
finalclassRemoveTokenFromUrlForMatomoTracking{
publicfunction__invoke(BeforeTrackPageViewEvent $event){
$request = $event->getRequest();
if (!isset($request->getQueryParams()['tx_myext']['token']) {
return;
}
$uri = $request->getUri();
$pathParts = explode('/', $uri->getPath());
// The path ends with a slash, which we want to preserve, so we// need to remove the second last part (which is the token)unset($pathParts[count($pathParts) - 2]);
$tokenRemovedUri = $uri->withPath(implode('/', $pathParts));
$event->addMatomoMethodCall('setCustomUrl', (string)$tokenRemovedUri);
}
}
Using tracking tools like Matomo within the European Union need special treatments in order
to let the customer consent and agree with the tracking. Although Matomo respects the browser's "Do not track" setting, not everyone is aware of it.
Some GDPR tools like klaro.js require special attribute settings within the script tag in order to work.
The event listener
Before the script tag is rendered the event EnrichScriptTagEvent dispatched from the injector.
This events allow to register an id, a type and add additional data attributes.
When offering a site search, the main thing you want to know is what users are
searching for. Also worth knowing are, for example, the search terms without
results or the query time for a search phrase. The blog post Display search metrics from TYPO3 extension ke_search in Matomo illustrates how to
achieve this within the extension ke_search. The article can also serve as a
blueprint for other search extensions.
Changelog
All notable changes to this project will be documented in this file.
The setting
matomoIntegrationTagManagerContainerId in the
site configuration has
been changed to
matomoIntegrationTagManagerContainerIds to reflect that
it can hold multiple container IDs since version 1.6.0.
Migration: Search the YAML files of your site configuration(s) for this string
and adjust it accordingly.
Sitemap
Reference to the headline
Copy and freely share the link
This link target has no permanent anchor assigned.The link below can be used, but is prone to change if the page gets moved.