PHP code example of pagemachine / matomo-tracking

1. Go to this page and download the library: Download pagemachine/matomo-tracking library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pagemachine / matomo-tracking example snippets


use Pagemachine\MatomoTracking\Matomo;

final class Example
{
    public function __construct(
        private readonly Matomo $matomo,
    ) {}

    public function someAction(): void
    {
        $this->matomo->track(...);
    }
}

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\RequestOptions;
use Pagemachine\MatomoTracking\Matomo;
use Pagemachine\MatomoTracking\Tracking\ActionFactory;
use Pagemachine\MatomoTracking\Tracking\ActionTracker;
use Psr\Log\NullLogger;

$matomoUrl = 'https://...';
$httpFactory = new HttpFactory();

$matomo = new Matomo(
    $matomoUrl,
    new ActionFactory(),
    new ActionTracker(
        $httpFactory,
        $httpFactory,
        new Client([
            RequestOptions::TIMEOUT => 3,
        ]),
        new NullLogger(),
    ),
    new NullLogger(),
);

$matomo->track(...);

use Pagemachine\MatomoTracking\Matomo;
use Pagemachine\MatomoTracking\Tracking\Attributes\ActionName;
use Pagemachine\MatomoTracking\Tracking\Attributes\SiteId;
use Psr\Http\Message\ServerRequestInterface;

final class Example
{
    public function __construct(
        private readonly Matomo $matomo,
    ) {}

    public function someAction(ServerRequestInterface $request): void
    {
        // Url attribute is determined from request
        $request = $request->withAttribute('matomo.attributes', [
            new SiteId(1),
            new ActionName('Some action'),
        ]));

        $this->matomo->track($request);
    }
}

use Pagemachine\MatomoTracking\Matomo;
use Pagemachine\MatomoTracking\Tracking\Attributes\ActionName;
use Pagemachine\MatomoTracking\Tracking\Attributes\SiteId;
use Pagemachine\MatomoTracking\Tracking\Attributes\Url;

final class Example
{
    public function __construct(
        private readonly Matomo $matomo,
        private readonly ActionFactoryInterface $actionFactory,
    ) {}

    public function someAction(): void
    {
        $action = $this->actionFactory->createAction()
            ->withAttribute(new SiteId(1))
            ->withAttribute(new Url('https://example.org/demo'))
            ->withAttribute(new ActionName('Demo Page'));

        $this->matomo->track($action);
    }
}

use Pagemachine\MatomoTracking\Matomo;
use Pagemachine\MatomoTracking\Tracking\Attributes\ActionName;
use Pagemachine\MatomoTracking\Tracking\Attributes\SiteId;
use Psr\Http\Message\ServerRequestInterface;

final class Example
{
    public function __construct(
        private readonly Matomo $matomo,
        private readonly ActionFactoryInterface $actionFactory,
    ) {}

    public function someAction(ServerRequestInterface $request): void
    {
        // Url attribute is determined from request
        $action = $this->actionFactory->createActionFromRequest($request)
            ->withAttribute(new SiteId(1))
            ->withAttribute(new ActionName('Demo Page'));

        $this->matomo->track($action);
    }
}

use Pagemachine\MatomoTracking\Tracking\ActionFactoryInterface;
use Pagemachine\MatomoTracking\Tracking\ActionInterface;
use Psr\Http\Message\ServerRequestInterface;

final class ExampleActionFactory implements ActionFactoryInterface
{
    public function __construct(
        private readonly ActionFactoryInterface $decorated,
    ) {}

    public function createAction(): ActionInterface
    {
        $action = $this->decorated->createAction()
            ->withAttribute(...);

        return $action;
    }

    public function createActionFromRequest(ServerRequestInterface $serverRequest): ActionInterface
    {
        $action = $this->decorated->createActionFromRequest($serverRequest)
            ->withAttribute(...);

        return $action;
    }
}

use Pagemachine\MatomoTracking\Tracking\AttributeInterface;

final class ExampleDimension implements AttributeInterface
{
    public function __construct(private string $example) {}

    public function toParameters(): iterable
    {
        return ['dimension1' => $this->example];
    }
}