matomoOptions->enabled) { return; } $visitId = $visitLocated->visitId; /** @var Visit|null $visit */ $visit = $this->em->find(Visit::class, $visitId); if ($visit === null) { $this->logger->warning('Tried to send visit with id "{visitId}" to matomo, but it does not exist.', [ 'visitId' => $visitId, ]); return; } try { $tracker = $this->trackerBuilder->buildMatomoTracker(); $tracker ->setUrl($this->resolveUrlToTrack($visit)) ->setCustomTrackingParameter('type', $visit->type->value) ->setUserAgent($visit->userAgent) ->setUrlReferrer($visit->referer); $location = $visit->getVisitLocation(); if ($location !== null) { $tracker ->setCity($location->cityName) ->setCountry($location->countryName) ->setLatitude($location->latitude) ->setLongitude($location->longitude); } // Set not obfuscated IP if possible, as matomo handles obfuscation itself $ip = $visitLocated->originalIpAddress ?? $visit->remoteAddr; if ($ip !== null) { $tracker->setIp($ip); } if ($visit->isOrphan()) { $tracker->setCustomTrackingParameter('orphan', 'true'); } // 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]); } } public function resolveUrlToTrack(Visit $visit): string { $shortUrl = $visit->shortUrl; if ($shortUrl === null) { return $visit->visitedUrl ?? ''; } return $this->shortUrlStringifier->stringify($shortUrl); } }