Skip to content

Commit

Permalink
[TASK] Use Matomo "VisitsSummary" API
Browse files Browse the repository at this point in the history
The used API "Live.getCounters" has been limited to 2880 Minutes
with Matomo 5. Therefore, the extension now uses the "VisitsSummary"
API to retrieve data from Matomo.
  • Loading branch information
derhansen committed Mar 4, 2025
1 parent cbd7b09 commit 5eca77e
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PER' => true,
'@PER-CS' => true,
'array_syntax' => ['syntax' => 'short'],
'cast_spaces' => ['space' => 'none'],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_parentheses' => true,
'dir_constant' => true,
'function_to_constant' => ['functions' => ['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']],
'function_typehint_space' => true,
'type_declaration_spaces' => true,
'modernize_strpos' => true,
'modernize_types_casting' => true,
'native_function_casing' => true,
Expand Down
54 changes: 32 additions & 22 deletions Classes/Service/MatomoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MatomoService
Expand All @@ -40,47 +41,52 @@ class MatomoService
protected FrontendInterface $cache;
protected array $extensionConfiguration = [];

public function __construct()
{
public function __construct(
protected readonly RequestFactory $requestFactory
) {
$this->cache = GeneralUtility::makeInstance(CacheManager::class)->getCache(self::EXT_KEY);
$this->extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class)
->get(self::EXT_KEY);
}

public function getMatomoData(string $data, bool $absolute = false): array
public function getMatomoData(string $method, bool $absolute = false): array
{
$content30days = $this->matomoApiRequest(['lastMinutes' => 43200]);
$content7days = $this->matomoApiRequest(['lastMinutes' => 10080]);
$content1day = $this->matomoApiRequest(['lastMinutes' => 1440]);
$content30days = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'last30']);
$content7days = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'last7']);
$content1day = $this->matomoApiRequest($method, ['period' => 'day', 'date' => 'today']);

if ($content1day === '' || $content7days === '' || $content30days === '') {
return [0, 0, 0];
}

if ($absolute) {
$actions30days = json_decode($content30days)[0]->$data;
$actions7days = json_decode($content7days)[0]->$data;
} else {
$actions30days = number_format(json_decode($content30days)[0]->$data / 30, 2);
$actions7days = number_format(json_decode($content7days)[0]->$data / 7, 2);
$actions30days = array_sum(array_values(json_decode($content30days, true)));
$actions7days = array_sum(array_values(json_decode($content7days, true)));
$actions1day = array_sum(array_values(json_decode($content1day, true)));
if (!$absolute) {
$actions30days = number_format($actions30days / 30, 2);
$actions7days = number_format($actions7days / 7, 2);
}
$actions1day = json_decode($content1day)[0]->$data;

return [$actions1day, $actions7days, $actions30days];
}

protected function matomoApiRequest(array $arguments = []): string
protected function matomoApiRequest(string $method, array $arguments = []): string
{
$siteId = $this->extensionConfiguration['matomoSiteId'] ?? 1;
$apiToken = $this->extensionConfiguration['matomoToken'] ?? '';
$baseUrl = $this->getBaseUrl();

$apiArguments = [
'module' => 'API',
'method' => 'Live.getCounters',
'method' => $method,
'idSite' => $siteId,
'format' => 'JSON',
'token_auth' => $apiToken,
];

$requestOptions = [
'form_params' => [
'token_auth' => $apiToken,
],
];

$apiArguments += $arguments;
Expand All @@ -89,14 +95,18 @@ protected function matomoApiRequest(array $arguments = []): string

$result = $this->cache->get($cacheHash);
if ($result === false || $result === null) {
$result = GeneralUtility::getUrl($url);
}

if ($result === false || !$this->isJson($result)) {
return '';
$response = $this->requestFactory->request($url, 'POST', $requestOptions);
if ($response->getStatusCode() !== 200) {
return '';
}
$result = (string)$response->getBody();

if (!$this->isJson($result)) {
return '';
}
$this->cache->set($cacheHash, $result, [], self::CACHE_LIFETIME);
}

$this->cache->set($cacheHash, $result, [], self::CACHE_LIFETIME);
return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion Classes/Widgets/AbstractMatomoWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getChartData(): array
{
return [
'labels' => [
'24 ' . $this->getLanguageService()->sL(self::LLL . 'label.hours'),
$this->getLanguageService()->sL(self::LLL . 'label.today'),
'7 ' . $this->getLanguageService()->sL(self::LLL . 'label.days'),
'30 ' . $this->getLanguageService()->sL(self::LLL . 'label.days'),
],
Expand Down
2 changes: 1 addition & 1 deletion Classes/Widgets/PageViewAbsoluteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class PageViewAbsoluteWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('actions', true);
return $this->matomoService->getMatomoData('VisitsSummary.getActions', true);
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/PageViewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class PageViewWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('actions');
return $this->matomoService->getMatomoData('VisitsSummary.getActions');
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/VisitorCountAbsoluteWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class VisitorCountAbsoluteWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('visitors', true);
return $this->matomoService->getMatomoData('VisitsSummary.getUniqueVisitors', true);
}
}
2 changes: 1 addition & 1 deletion Classes/Widgets/VisitorCountWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class VisitorCountWidget extends AbstractMatomoWidget
{
protected function getData(): array
{
return $this->matomoService->getMatomoData('visitors');
return $this->matomoService->getMatomoData('VisitsSummary.getUniqueVisitors');
}
}
Binary file modified Documentation/Images/mw-matomo-widget.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Mittwald Matomo Widget is an extension for TYPO3 CMS to show visitor and page vi
The extension fetches statistics via the Matomo API and uses the TYPO3 caching framework to cache results
for one hour.

## Requirements

The extension is only compatible with Matomo version 5 or greater.

## Screenshots

![Screenshot of the dashboard widgets](Documentation/Images/mw-matomo-widget.png "Screenshot of the dashboard widgets")
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Language/de.locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<target>Zeigt die Seitenaufrufe aus Ihrer Matomo Instanz an.</target>
</trans-unit>

<trans-unit id="label.today" xml:space="preserve">
<source>Today</source>
<target>Heute</target>
</trans-unit>
<trans-unit id="label.hours" xml:space="preserve">
<source>hours</source>
<target>Stunden</target>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<source>Shows visitor statistics from your Matomo instance.</source>
</trans-unit>

<trans-unit id="label.today" xml:space="preserve">
<source>Today</source>
</trans-unit>
<trans-unit id="label.hours" xml:space="preserve">
<source>hours</source>
</trans-unit>
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'author_company' => 'Mittwald CM Service GmbH',
'state' => 'stable',
'clearCacheOnLoad' => true,
'version' => '3.0.0',
'version' => '3.1.0',
'uploadfolder' => false,
'autoload' => [
'psr-4' => [
Expand Down

0 comments on commit 5eca77e

Please sign in to comment.