Skip to content

Commit

Permalink
feat: add widget "Most viewed pages"
Browse files Browse the repository at this point in the history
resolves: #47
  • Loading branch information
brotkrueml committed Dec 15, 2023
1 parent 0fb19b0 commit 2a9dfcf
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Widget "Most viewed pages" (#47)

### Fixed
- Table content with numbers is right-aligned again in TYPO3 v12

Expand Down
1 change: 1 addition & 0 deletions Classes/Configuration/WidgetsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class WidgetsProvider
'countries' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.userCountry.country.title',
'javaScriptErrors' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.events.javaScriptErrors.title',
'linkMatomo' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.linkMatomo.title',
'mostViewedPages' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.mostViewedPages.title',
'osFamilies' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.devicesDetection.osFamilies.title',
'pagesNotFound' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.pagesNotFound.title',
'siteSearchKeywords' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.siteSearchKeywords.title',
Expand Down
125 changes: 125 additions & 0 deletions Classes/DependencyInjection/Widgets/MostViewedPagesRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "matomo_widgets" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\MatomoWidgets\DependencyInjection\Widgets;

use Brotkrueml\MatomoWidgets\Extension;
use Brotkrueml\MatomoWidgets\Widgets\Decorator\NumberDecorator;
use Brotkrueml\MatomoWidgets\Widgets\Provider\GenericTableDataProvider;
use Brotkrueml\MatomoWidgets\Widgets\TableWidget;
use Symfony\Component\DependencyInjection\Reference;

/**
* @internal
*/
final class MostViewedPagesRegistration extends AbstractRegistration
{
private const METHOD = 'Actions.getPageUrls';
private const PARAMETERS_PARAMETERS = 'matomo_widgets.mostViewedPages.parameters';

protected $serviceIdSuffix = 'actions.mostViewedPages';

public function register(): void
{
if (! $this->matomoConfiguration->isWidgetActive('mostViewedPages')) {
return;
}

$this->defineParameters();
$this->registerDataProvider();
$this->registerWidget();
}

private function defineParameters(): void
{
$this->parameters->set(
self::PARAMETERS_PARAMETERS,
[
'period' => 'month',
'date' => 'today',
'filter_limit' => '50',
'filter_sort_column' => 'nb_hits',
'filter_sort_order' => 'desc',
'flat' => 1,
],
);
}

private function registerDataProvider(): void
{
$this->services
->set($this->buildServiceDataProviderId(), GenericTableDataProvider::class)
->arg('$connectionConfiguration', new Reference($this->connectionConfigurationId))
->arg('$method', self::METHOD)
->arg(
'$columns',
[
[
'column' => 'label',
'header' => Extension::LANGUAGE_PATH_DASHBOARD . ':urls',
'classes' => 'matomo-widgets__break-word',
],
[
'column' => 'nb_hits',
'header' => Extension::LANGUAGE_PATH_DASHBOARD . ':hits',
'decorator' => new Reference(NumberDecorator::class),
'classes' => 'matomo-widgets__text-end',
],
],
)
->arg('$parameters', '%' . self::PARAMETERS_PARAMETERS . '%')
->call('addParameter', [
'showColumns', 'nb_hits',
]);
}

private function registerWidget(): void
{
$localisedTitle = Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.mostViewedPages.title';
$title = $this->matomoConfiguration->siteTitle !== ''
? \sprintf('%s: %s', $this->matomoConfiguration->siteTitle, 'Most viewed pages')
: $localisedTitle;

$this->services
->set($this->buildServiceWidgetId(), TableWidget::class)
->arg('$view', new Reference('dashboard.views.widget'))
->arg('$dataProvider', new Reference($this->buildServiceDataProviderId()))
->arg(
'$options',
[
'reportLink' => $this->buildReportLink(),
'siteTitle' => $this->matomoConfiguration->siteTitle,
'title' => $localisedTitle,
],
)
->tag(
'dashboard.widget',
[
'identifier' => $this->buildWidgetIdentifier(),
'groupNames' => 'matomo',
'title' => $title,
'description' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.mostViewedPages.description',
'iconIdentifier' => self::ICON_IDENTIFIER,
'height' => 'medium',
'width' => 'medium',
],
);
}

private function buildReportLink(): string
{
return \sprintf(
'%s?module=CoreHome&action=index&idSite=%d&period=month&date=today#?&General_Actions&subcategory=General_Pages',
$this->matomoConfiguration->url,
$this->matomoConfiguration->idSite,
);
}
}
2 changes: 2 additions & 0 deletions Classes/DependencyInjection/WidgetsRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\CustomDimensionsRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\JavaScriptErrorsRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\LinkMatomoRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\MostViewedPagesRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\OsFamiliesRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\PagesNotFoundRegistration;
use Brotkrueml\MatomoWidgets\DependencyInjection\Widgets\SiteSearchKeywordsRegistration;
Expand Down Expand Up @@ -55,6 +56,7 @@ final class WidgetsRegistration
CountriesRegistration::class,
JavaScriptErrorsRegistration::class,
LinkMatomoRegistration::class,
MostViewedPagesRegistration::class,
OsFamiliesRegistration::class,
PagesNotFoundRegistration::class,
SiteSearchKeywordsRegistration::class,
Expand Down
6 changes: 6 additions & 0 deletions Documentation/Changelog/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased <https://github.com/brotkrueml/typo3-matomo-widgets/compare/v2.2.1...HEAD>`_
--------------------------------------------------------------------------------------------

Added
^^^^^


* Widget "Most viewed pages" (#47)

Fixed
^^^^^

Expand Down
Binary file added Documentation/Images/WidgetMostViewedPages.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Documentation/Widgets/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,38 @@ Active widgets value in :file:`config.yaml`
linkMatomo


Most viewed pages
=================

.. versionadded:: 2.3.0

Show the most viewed pages of a site for the current month:

.. figure:: /Images/WidgetMostViewedPages.png
:alt: Widget Most viewed pages
:class: with-border

Widget *Most viewed pages*

Matomo module
Actions

Active widgets value in :file:`config.yaml`
mostViewedPages

Default configuration parameters in the :file:`Configuration/Services.yaml` file
matomo_widgets.mostViewedPages.parameters:
- period: month
- date: today
- filter_sort_column: nb_hits
- filter_sort_order: desc

.. tip::
When you see URLs with the addition "- Others" then Matomo groups pages
automatically after a given limit: You can `increase the limit`_. Maybe you
want then `invalidate the historical reports`_ to adopt the change.


Operating system families
=========================

Expand Down Expand Up @@ -605,3 +637,5 @@ Default configuration parameters in the :file:`Configuration/Services.yaml` file
.. _custom dimensions: https://matomo.org/docs/custom-dimensions/
.. _enableJSErrorTracking: https://matomo.org/faq/how-to/how-do-i-enable-basic-javascript-error-tracking-and-reporting-in-matomo-browser-console-error-messages/
.. _feature request: https://github.com/brotkrueml/typo3-matomo-widgets/issues
.. _increase the limit: https://matomo.org/faq/how-to/faq_54/
.. _invalidate the historical reports: https://matomo.org/faq/how-to/faq_155/
8 changes: 7 additions & 1 deletion Resources/Private/Language/Dashboard.xlf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="EXT:matomo_widgets/Resources/Private/Language/Dashboard.xlf" date="2022-07-20T17:31:48Z" product-name="matomo_widgets">
<file source-language="en" datatype="plaintext" original="EXT:matomo_widgets/Resources/Private/Language/Dashboard.xlf" date="2023-11-12T15:02:27Z" product-name="matomo_widgets">
<body>
<trans-unit id="actions" resname="actions">
<source><![CDATA[Actions]]></source>
Expand Down Expand Up @@ -113,6 +113,12 @@
<trans-unit id="visitsPercentage" resname="visitsPercentage">
<source><![CDATA[% Visits]]></source>
</trans-unit>
<trans-unit id="widgets.actions.mostViewedPages.description" resname="widgets.actions.mostViewedPages.description">
<source><![CDATA[List of the most viewed page URLs.]]></source>
</trans-unit>
<trans-unit id="widgets.actions.mostViewedPages.title" resname="widgets.actions.mostViewedPages.title">
<source><![CDATA[Most viewed pages]]></source>
</trans-unit>
<trans-unit id="widgets.actions.pagesNotFound.description" resname="widgets.actions.pagesNotFound.description">
<source><![CDATA[A list of the pages which cannot be found (404).]]></source>
</trans-unit>
Expand Down
5 changes: 5 additions & 0 deletions Tests/Unit/Configuration/WidgetsProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getItemsForTcaInV11(): void
self::assertContains([Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.siteSearchNoResultKeywords.title', 'siteSearchNoResultKeywords'], $actual);
self::assertContains([Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.visitsSummary.visitsPerDay.title', 'visitsPerDay'], $actual);
self::assertContains([Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.visitsSummary.visitsPerMonth.title', 'visitsPerMonth'], $actual);
self::assertContains([Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.mostViewedPages.title', 'mostViewedPages'], $actual);
}

#[Test]
Expand Down Expand Up @@ -133,5 +134,9 @@ public function getItemsForTca(): void
'label' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.visitsSummary.visitsPerMonth.title',
'value' => 'visitsPerMonth',
], $actual);
self::assertContains([
'label' => Extension::LANGUAGE_PATH_DASHBOARD . ':widgets.actions.mostViewedPages.title',
'value' => 'mostViewedPages',
], $actual);
}
}

0 comments on commit 2a9dfcf

Please sign in to comment.