Skip to content

Commit

Permalink
Merge pull request #2083 from acelaya-forks/feature/matomo-title
Browse files Browse the repository at this point in the history
Track short URL title as document title when sending visits to matomo
  • Loading branch information
acelaya authored Mar 31, 2024
2 parents 401046f + 284b28e commit cd43c1c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions module/Core/src/EventDispatcher/Matomo/SendVisitToMatomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
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,
) {
}

Expand Down Expand Up @@ -69,8 +69,8 @@ public function __invoke(VisitLocated $visitLocated): void
$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]);
Expand Down
5 changes: 5 additions & 0 deletions module/Core/src/ShortUrl/Entity/ShortUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ public function forwardQuery(): bool
return $this->forwardQuery;
}

public function title(): ?string
{
return $this->title;
}

public function reachedVisits(int $visitsAmount): bool
{
return count($this->visits) >= $visitsAmount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function visitIsSentWhenItExists(Visit $visit, ?string $originalIpAddress
$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([
Expand Down

0 comments on commit cd43c1c

Please sign in to comment.