Skip to content

Commit

Permalink
Add 10 second timeout to matomo requests
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Nov 21, 2023
1 parent c03eea7 commit 316b88c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions module/Core/src/Matomo/MatomoTrackerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
{
public const MATOMO_DEFAULT_TIMEOUT = 10; // Time in seconds

public function __construct(private readonly MatomoOptions $options)
{
}
Expand All @@ -27,12 +29,16 @@ public function buildMatomoTracker(): MatomoTracker

// 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');
$tracker
// Token required to set the IP and location
->setTokenAuth($this->options->apiToken)
// Ensure params are not sent in the URL, for security reasons
->setRequestMethodNonBulk('POST')
// Set a reasonable timeout
->setRequestTimeout(self::MATOMO_DEFAULT_TIMEOUT)
->setRequestConnectTimeout(self::MATOMO_DEFAULT_TIMEOUT)
// We don't want to bulk send, as every request to Shlink will create a new tracker
->disableBulkTracking();

return $tracker;
}
Expand Down
2 changes: 2 additions & 0 deletions module/Core/test/Matomo/MatomoTrackerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function trackerIsCreated(): void

self::assertEquals('api_token', $tracker->token_auth); // @phpstan-ignore-line
self::assertEquals(5, $tracker->idSite); // @phpstan-ignore-line
self::assertEquals(MatomoTrackerBuilder::MATOMO_DEFAULT_TIMEOUT, $tracker->getRequestTimeout());
self::assertEquals(MatomoTrackerBuilder::MATOMO_DEFAULT_TIMEOUT, $tracker->getRequestConnectTimeout());
}

private function builder(?MatomoOptions $options = null): MatomoTrackerBuilder
Expand Down

0 comments on commit 316b88c

Please sign in to comment.