1
0
Fork 0
mirror of https://github.com/shlinkio/shlink.git synced 2025-05-05 06:45:27 +03:00

Merge pull request from acelaya-forks/feature/matomo-title

Track short URL title as document title when sending visits to matomo
This commit is contained in:
Alejandro Celaya 2024-03-31 13:53:58 +02:00 committed by GitHub
commit cd43c1c01f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 17 additions and 10 deletions
CHANGELOG.mddocker-compose.yml
module/Core
src
EventDispatcher/Matomo
ShortUrl/Entity
test/EventDispatcher/Matomo

View file

@ -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. 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 ### Changed
* [#2034](https://github.com/shlinkio/shlink/issues/2034) Modernize entities, using constructor property promotion and readonly wherever possible. * [#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: * [#2036](https://github.com/shlinkio/shlink/issues/2036) Deep performance improvement in some endpoints which involve counting visits:

View file

@ -169,7 +169,7 @@ services:
shlink_matomo: shlink_matomo:
container_name: shlink_matomo container_name: shlink_matomo
image: matomo:4.15-apache image: matomo:5.0-apache
ports: ports:
- "8003:80" - "8003:80"
volumes: volumes:

View file

@ -13,14 +13,14 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\Visit\Entity\Visit; use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Throwable; use Throwable;
class SendVisitToMatomo readonly class SendVisitToMatomo
{ {
public function __construct( public function __construct(
private readonly EntityManagerInterface $em, private EntityManagerInterface $em,
private readonly LoggerInterface $logger, private LoggerInterface $logger,
private readonly ShortUrlStringifier $shortUrlStringifier, private ShortUrlStringifier $shortUrlStringifier,
private readonly MatomoOptions $matomoOptions, private MatomoOptions $matomoOptions,
private readonly MatomoTrackerBuilderInterface $trackerBuilder, private MatomoTrackerBuilderInterface $trackerBuilder,
) { ) {
} }
@ -69,8 +69,8 @@ class SendVisitToMatomo
$tracker->setCustomTrackingParameter('orphan', 'true'); $tracker->setCustomTrackingParameter('orphan', 'true');
} }
// Send empty document title to avoid different actions to be created by matomo // Send the short URL title or an empty document title to avoid different actions to be created by matomo
$tracker->doTrackPageView(''); $tracker->doTrackPageView($visit->shortUrl?->title() ?? '');
} catch (Throwable $e) { } catch (Throwable $e) {
// Capture all exceptions to make sure this does not interfere with the regular execution // 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]); $this->logger->error('An error occurred while trying to send visit to Matomo. {e}', ['e' => $e]);

View file

@ -192,6 +192,11 @@ class ShortUrl extends AbstractEntity
return $this->forwardQuery; return $this->forwardQuery;
} }
public function title(): ?string
{
return $this->title;
}
public function reachedVisits(int $visitsAmount): bool public function reachedVisits(int $visitsAmount): bool
{ {
return count($this->visits) >= $visitsAmount; return count($this->visits) >= $visitsAmount;

View file

@ -72,7 +72,7 @@ class SendVisitToMatomoTest extends TestCase
$tracker->expects($this->once())->method('setUrl')->willReturn($tracker); $tracker->expects($this->once())->method('setUrl')->willReturn($tracker);
$tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker); $tracker->expects($this->once())->method('setUserAgent')->willReturn($tracker);
$tracker->expects($this->once())->method('setUrlReferrer')->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()) { if ($visit->isOrphan()) {
$tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([ $tracker->expects($this->exactly(2))->method('setCustomTrackingParameter')->willReturnMap([