mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Add logic to send visits to a matomo instance
This commit is contained in:
27
module/Core/src/Matomo/MatomoOptions.php
Normal file
27
module/Core/src/Matomo/MatomoOptions.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
class MatomoOptions
|
||||
{
|
||||
public function __construct(
|
||||
public readonly bool $enabled,
|
||||
public readonly ?string $baseUrl,
|
||||
/** @var numeric-string|int|null */
|
||||
private readonly string|int|null $siteId,
|
||||
public readonly ?string $apiToken,
|
||||
) {
|
||||
}
|
||||
|
||||
public function siteId(): ?int
|
||||
{
|
||||
if ($this->siteId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We enforce site ID to be hydrated as a numeric string or int, so it's safe to cast to int here
|
||||
return (int) $this->siteId;
|
||||
}
|
||||
}
|
||||
39
module/Core/src/Matomo/MatomoTrackerBuilder.php
Normal file
39
module/Core/src/Matomo/MatomoTrackerBuilder.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
use MatomoTracker;
|
||||
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||
|
||||
class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
|
||||
{
|
||||
public function __construct(private readonly MatomoOptions $options)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException If there's any missing matomo parameter
|
||||
*/
|
||||
public function buildMatomoTracker(): MatomoTracker
|
||||
{
|
||||
$siteId = $this->options->siteId();
|
||||
if ($siteId === null || $this->options->baseUrl === null || $this->options->apiToken === null) {
|
||||
throw new RuntimeException(
|
||||
'Cannot create MatomoTracker. Either site ID, base URL or api token are not defined',
|
||||
);
|
||||
}
|
||||
|
||||
// Create a new MatomoTracker on every request, because it infers request info during construction
|
||||
$tracker = new MatomoTracker($siteId, $this->options->baseUrl);
|
||||
// Token required to set the IP and location
|
||||
$tracker->setTokenAuth($this->options->apiToken);
|
||||
// We don't want to bulk send, as every request to Shlink will create a new tracker
|
||||
$tracker->disableBulkTracking();
|
||||
// Ensure params are not sent in the URL, for security reasons
|
||||
$tracker->setRequestMethodNonBulk('POST');
|
||||
|
||||
return $tracker;
|
||||
}
|
||||
}
|
||||
16
module/Core/src/Matomo/MatomoTrackerBuilderInterface.php
Normal file
16
module/Core/src/Matomo/MatomoTrackerBuilderInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Matomo;
|
||||
|
||||
use MatomoTracker;
|
||||
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||
|
||||
interface MatomoTrackerBuilderInterface
|
||||
{
|
||||
/**
|
||||
* @throws RuntimeException If there's any missing matomo parameter
|
||||
*/
|
||||
public function buildMatomoTracker(): MatomoTracker;
|
||||
}
|
||||
Reference in New Issue
Block a user