Set referrer when sending visits to Matomo

This commit is contained in:
Alejandro Celaya 2023-11-21 10:01:27 +01:00
parent 316b88cea6
commit e783bdc456
4 changed files with 15 additions and 4 deletions

View File

@ -47,7 +47,8 @@ class SendVisitToMatomo
$tracker $tracker
->setUrl($this->resolveUrlToTrack($visit)) ->setUrl($this->resolveUrlToTrack($visit))
->setCustomTrackingParameter('type', $visit->type()->value) ->setCustomTrackingParameter('type', $visit->type()->value)
->setUserAgent($visit->userAgent()); ->setUserAgent($visit->userAgent())
->setUrlReferrer($visit->referer());
$location = $visit->getVisitLocation(); $location = $visit->getVisitLocation();
if ($location !== null) { if ($location !== null) {

View File

@ -36,9 +36,12 @@ class MatomoTrackerBuilder implements MatomoTrackerBuilderInterface
->setRequestMethodNonBulk('POST') ->setRequestMethodNonBulk('POST')
// Set a reasonable timeout // Set a reasonable timeout
->setRequestTimeout(self::MATOMO_DEFAULT_TIMEOUT) ->setRequestTimeout(self::MATOMO_DEFAULT_TIMEOUT)
->setRequestConnectTimeout(self::MATOMO_DEFAULT_TIMEOUT) ->setRequestConnectTimeout(self::MATOMO_DEFAULT_TIMEOUT);
// We don't want to bulk send, as every request to Shlink will create a new tracker
->disableBulkTracking(); // We don't want to bulk send, as every request to Shlink will create a new tracker
$tracker->disableBulkTracking();
// Disable cookies, as they are ignored anyway
$tracker->disableCookieSupport();
return $tracker; return $tracker;
} }

View File

@ -193,6 +193,11 @@ class Visit extends AbstractEntity implements JsonSerializable
return $this->userAgent; return $this->userAgent;
} }
public function referer(): string
{
return $this->referer;
}
public function jsonSerialize(): array public function jsonSerialize(): array
{ {
return [ return [

View File

@ -71,6 +71,7 @@ class SendVisitToMatomoTest extends TestCase
$tracker = $this->createMock(MatomoTracker::class); $tracker = $this->createMock(MatomoTracker::class);
$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('doTrackPageView')->with(''); $tracker->expects($this->once())->method('doTrackPageView')->with('');
if ($visit->isOrphan()) { if ($visit->isOrphan()) {
@ -125,6 +126,7 @@ class SendVisitToMatomoTest extends TestCase
$tracker = $this->createMock(MatomoTracker::class); $tracker = $this->createMock(MatomoTracker::class);
$tracker->expects($this->once())->method('setUrl')->with($expectedTrackedUrl)->willReturn($tracker); $tracker->expects($this->once())->method('setUrl')->with($expectedTrackedUrl)->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->any())->method('setCustomTrackingParameter')->willReturn($tracker); $tracker->expects($this->any())->method('setCustomTrackingParameter')->willReturn($tracker);
$tracker->expects($this->once())->method('doTrackPageView'); $tracker->expects($this->once())->method('doTrackPageView');