From 284b28e8d97c5630d4fdadca28aa3baef63089cc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Mar 2024 13:51:03 +0200 Subject: [PATCH] Track short URL title as document title when sending visits to matomo --- CHANGELOG.md | 2 ++ docker-compose.yml | 2 +- .../EventDispatcher/Matomo/SendVisitToMatomo.php | 16 ++++++++-------- module/Core/src/ShortUrl/Entity/ShortUrl.php | 5 +++++ .../Matomo/SendVisitToMatomoTest.php | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ec4f68..07d4e3ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this Previously, this was exposed only for orphan visits, since this can be an arbitrary value for those. +* [#2077](https://github.com/shlinkio/shlink/issues/2077) When sending visits to Matomo, the short URL title is now used as document title in matomo. + ### Changed * [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible. * [#2036](https://github.com/shlinkio/shlink/issues/2036) Deep performance improvement in some endpoints which involve counting visits: diff --git a/docker-compose.yml b/docker-compose.yml index ccc5fc2d..5bccfd48 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -169,7 +169,7 @@ services: shlink_matomo: container_name: shlink_matomo - image: matomo:4.15-apache + image: matomo:5.0-apache ports: - "8003:80" volumes: diff --git a/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php b/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php index be288fd0..d0fa1035 100644 --- a/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php +++ b/module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php @@ -13,14 +13,14 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Throwable; -class SendVisitToMatomo +readonly class SendVisitToMatomo { public function __construct( - private readonly EntityManagerInterface $em, - private readonly LoggerInterface $logger, - private readonly ShortUrlStringifier $shortUrlStringifier, - private readonly MatomoOptions $matomoOptions, - private readonly MatomoTrackerBuilderInterface $trackerBuilder, + private EntityManagerInterface $em, + private LoggerInterface $logger, + private ShortUrlStringifier $shortUrlStringifier, + private MatomoOptions $matomoOptions, + private MatomoTrackerBuilderInterface $trackerBuilder, ) { } @@ -69,8 +69,8 @@ class SendVisitToMatomo $tracker->setCustomTrackingParameter('orphan', 'true'); } - // Send empty document title to avoid different actions to be created by matomo - $tracker->doTrackPageView(''); + // Send the short URL title or an empty document title to avoid different actions to be created by matomo + $tracker->doTrackPageView($visit->shortUrl?->title() ?? ''); } catch (Throwable $e) { // Capture all exceptions to make sure this does not interfere with the regular execution $this->logger->error('An error occurred while trying to send visit to Matomo. {e}', ['e' => $e]); diff --git a/module/Core/src/ShortUrl/Entity/ShortUrl.php b/module/Core/src/ShortUrl/Entity/ShortUrl.php index ac21b34b..ed97f9f5 100644 --- a/module/Core/src/ShortUrl/Entity/ShortUrl.php +++ b/module/Core/src/ShortUrl/Entity/ShortUrl.php @@ -192,6 +192,11 @@ class ShortUrl extends AbstractEntity return $this->forwardQuery; } + public function title(): ?string + { + return $this->title; + } + public function reachedVisits(int $visitsAmount): bool { return count($this->visits) >= $visitsAmount; diff --git a/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php b/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php index d821bcbb..cf9f4005 100644 --- a/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php +++ b/module/Core/test/EventDispatcher/Matomo/SendVisitToMatomoTest.php @@ -72,7 +72,7 @@ class SendVisitToMatomoTest extends TestCase $tracker->expects($this->once())->method('setUrl')->willReturn($tracker); $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); $tracker->expects($this->once())->method('setUrlReferrer')->willReturn($tracker); - $tracker->expects($this->once())->method('doTrackPageView')->with(''); + $tracker->expects($this->once())->method('doTrackPageView')->with($visit->shortUrl?->title() ?? ''); if ($visit->isOrphan()) { $tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([