PSR-14 events
Target group: Developers
Have a look into the event dispatcher documentation if you are not familiar with PSR-14 events.
BeforeMatomoApiRequestEvent
This event provides a possibility to adjust the site ID and the authentication token just before making the request to the Matomo API. This may be helpful in a big multi-site installation where you added a configuration independent from a site.
The event \Brotkrueml\
provides the following methods:
->get
Id Site () - Returns the site ID.
->set
Id Site () - Sets the site ID.
->get
Token Auth () - Returns the authentication token.
->set
Token Auth () - Sets the authentication token.
Example
Dependent on the host name the current backend user is using we change the site ID:
<?php
declare(strict_types=1);
namespace YourVendor\YourExtension\EventListener;
use Brotkrueml\MatomoWidgets\Event\BeforeMatomoApiRequestEvent;
use Psr\Http\Message\ServerRequestInterface;
use YourVendor\YourExtension\Mapping\MatomoSiteMapper;
final class BeforeMatomoApiRequestEventListener
{
public function __construct(
private readonly MatomoSiteMapper $matomoSiteMapper,
) {}
public function __invoke(BeforeMatomoApiRequestEvent $event): void
{
$hostName = $this->getRequest()->getServerParams()['REMOTE_HOST'];
if ($idSiteFromHostName = $this->matomoSiteMapper->getIdSiteFromHostName($hostName)) {
$event->setIdSite($idSiteFromHostName);
}
}
private function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
Registration of the event listener:
services:
# Place here the default dependency injection configuration
YourVendor\YourExtension\EventListener\BeforeMatomoApiRequestEventListener:
tags:
- name: event.listener
identifier: 'your-extension/before-matomo-api-request'