---
title: "PSR-14 events"
manual: "Matomo Widgets"
version: "main"
permalink: "https://docs.typo3.org/permalink/brotkrueml/typo3-matomo-widgets:events@main"
source: "Events/Index.rst"
rendered: "2026-09-22T16:06:29+00:00"
---

# PSR-14 events {#events}

Target group: **Developers**

Have a look into the [event dispatcher documentation](https://docs.typo3.org/m/typo3/reference-coreapi/14.3/en-us/ApiOverview/Events/EventDispatcher/Index.html#EventDispatcher) if
you are not familiar with PSR-14 events.

## BeforeMatomoApiRequestEvent {#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](https://docs.typo3.org/permalink/brotkrueml/typo3-matomo-widgets:configuration-independent-from-site@main).

The event `\Brotkrueml\MatomoWidgets\Event\BeforeMatomoApiRequestEvent`
provides the following methods:

-   **`->getIdSite()`**

    Returns the site ID.

-   **`->setIdSite()`**

    Sets the site ID.

-   **`->getTokenAuth()`**

    Returns the authentication token.

-   **`->setTokenAuth()`**

    Sets the authentication token.

### Example {#example}

Dependent on the host name the current backend user is using we change the
site ID:

**EXT:your_extension/Classes/EventListener/BeforeMatomoApiRequestEventListener.php**

```php
<?php

declare(strict_types=1);

namespace YourVendor\YourExtension\EventListener;

use Brotkrueml\MatomoWidgets\Event\BeforeMatomoApiRequestEvent;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use YourVendor\YourExtension\Mapping\MatomoSiteMapper;

#[AsEventListener(
    identifier: 'your-extension/before-matomo-api-request',
)]
final readonly class BeforeMatomoApiRequestEventListener
{
    public function __construct(
        private 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'];
    }
}

```
