diff --git a/Classes/Controller/FrontendController.php b/Classes/Controller/FrontendController.php index 097e5e16..2f3d47de 100644 --- a/Classes/Controller/FrontendController.php +++ b/Classes/Controller/FrontendController.php @@ -111,7 +111,7 @@ public function pageRequestAction(string $identificator, array $arguments): Resp $newsTracker = GeneralUtility::makeInstance(NewsTracker::class); $newsTracker->track($visitor, $arguments, $pagevisit); $searchTracker = GeneralUtility::makeInstance(SearchTracker::class); - $searchTracker->track($visitor, $arguments); + $searchTracker->track($visitor, $arguments, $pagevisit); return $this->jsonResponse(json_encode($this->afterAction($visitor))); } catch (Throwable $exception) { return $this->jsonResponse(json_encode($this->getError($exception))); diff --git a/Classes/Domain/Service/SiteService.php b/Classes/Domain/Service/SiteService.php index 03d982c6..eb005b59 100644 --- a/Classes/Domain/Service/SiteService.php +++ b/Classes/Domain/Service/SiteService.php @@ -43,10 +43,23 @@ public function getDefaultSite(): Site return current($sites); } - public function getSiteFromPageIdentifier(int $pageIdentifier): Site + public function getSiteFromPageIdentifier(int $pageIdentifier): ?Site { - $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); - return $siteFinder->getSiteByPageId($pageIdentifier); + try { + $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); + return $siteFinder->getSiteByPageId($pageIdentifier); + } catch (SiteNotFoundException $exception) { + return null; + } + } + + public function getSiteIdentifierFromPageIdentifier(int $pageIdentifier): string + { + $site = $this->getSiteFromPageIdentifier($pageIdentifier); + if ($site !== null) { + return $site->getIdentifier(); + } + return ''; } public function getFirstDomain(): string diff --git a/Classes/Domain/Tracker/DownloadTracker.php b/Classes/Domain/Tracker/DownloadTracker.php index 26fcc06e..59b478ee 100644 --- a/Classes/Domain/Tracker/DownloadTracker.php +++ b/Classes/Domain/Tracker/DownloadTracker.php @@ -9,6 +9,7 @@ use In2code\Lux\Domain\Repository\PageRepository; use In2code\Lux\Domain\Repository\VisitorRepository; use In2code\Lux\Domain\Service\FileService; +use In2code\Lux\Domain\Service\SiteService; use In2code\Lux\Events\Log\DownloadEvent; use In2code\Lux\Utility\FileUtility; use In2code\Lux\Utility\ObjectUtility; @@ -21,12 +22,14 @@ class DownloadTracker { protected ?Visitor $visitor = null; + protected SiteService $siteService; protected ?VisitorRepository $visitorRepository = null; private EventDispatcherInterface $eventDispatcher; public function __construct(Visitor $visitor) { $this->visitor = $visitor; + $this->siteService = GeneralUtility::makeInstance(SiteService::class); $this->visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class); $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); } @@ -67,7 +70,10 @@ protected function getAndPersistNewDownload(string $href, int $pageIdentifier): $pageRepository = GeneralUtility::makeInstance(PageRepository::class); $page = $pageRepository->findByIdentifier($pageIdentifier); $download = GeneralUtility::makeInstance(Download::class) - ->setHref($href)->setPage($page)->setDomainAutomatically(); + ->setHref($href) + ->setPage($page) + ->setSite($this->siteService->getSiteIdentifierFromPageIdentifier($pageIdentifier)) + ->setDomainAutomatically(); if ($file !== null) { $download->setFile($file); } diff --git a/Classes/Domain/Tracker/LinkClickTracker.php b/Classes/Domain/Tracker/LinkClickTracker.php index f1fe4bf6..57cabd1e 100644 --- a/Classes/Domain/Tracker/LinkClickTracker.php +++ b/Classes/Domain/Tracker/LinkClickTracker.php @@ -10,6 +10,7 @@ use In2code\Lux\Domain\Repository\LinkclickRepository; use In2code\Lux\Domain\Repository\LinklistenerRepository; use In2code\Lux\Domain\Repository\PageRepository; +use In2code\Lux\Domain\Service\SiteService; use In2code\Lux\Events\Log\LinkClickEvent; use Psr\EventDispatcher\EventDispatcherInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -20,41 +21,21 @@ */ class LinkClickTracker { - /** - * @var Visitor - */ - protected $visitor = null; - - /** - * @var LinkclickRepository - */ - protected $linkclickRepository = null; - - /** - * @var LinklistenerRepository - */ - protected $linklistenerRepository = null; - - /** - * @var PageRepository - */ - protected $pageRepository = null; - - /** - * @var EventDispatcherInterface - */ - private $eventDispatcher; + protected ?Visitor $visitor = null; + protected ?SiteService $siteService = null; + protected ?LinkclickRepository $linkclickRepository = null; + protected ?LinklistenerRepository $linklistenerRepository = null; + protected ?PageRepository $pageRepository = null; + private EventDispatcherInterface $eventDispatcher; - /** - * @param Visitor $visitor - */ public function __construct(Visitor $visitor) { $this->visitor = $visitor; - $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); + $this->siteService = GeneralUtility::makeInstance(SiteService::class); $this->linkclickRepository = GeneralUtility::makeInstance(LinkclickRepository::class); $this->linklistenerRepository = GeneralUtility::makeInstance(LinklistenerRepository::class); $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class); + $this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class); } /** @@ -71,7 +52,11 @@ public function addLinkClick(int $linkclickIdentifier, int $pageUid): void $page = $this->pageRepository->findByIdentifier($pageUid); if ($linklistener !== null && $page !== null) { $linkclick = GeneralUtility::makeInstance(Linkclick::class); - $linkclick->setPage($page)->setVisitor($this->visitor)->setLinklistener($linklistener); + $linkclick + ->setPage($page) + ->setSite($this->siteService->getSiteIdentifierFromPageIdentifier($pageUid)) + ->setVisitor($this->visitor) + ->setLinklistener($linklistener); $this->linkclickRepository->add($linkclick); $this->linkclickRepository->persistAll(); diff --git a/Classes/Domain/Tracker/PageTracker.php b/Classes/Domain/Tracker/PageTracker.php index 5b6f7610..afc10eb8 100644 --- a/Classes/Domain/Tracker/PageTracker.php +++ b/Classes/Domain/Tracker/PageTracker.php @@ -8,6 +8,7 @@ use In2code\Lux\Domain\Model\Visitor; use In2code\Lux\Domain\Repository\PageRepository; use In2code\Lux\Domain\Repository\VisitorRepository; +use In2code\Lux\Domain\Service\SiteService; use In2code\Lux\Events\PageTrackerEvent; use In2code\Lux\Utility\ObjectUtility; use Psr\EventDispatcher\EventDispatcherInterface; @@ -19,11 +20,16 @@ class PageTracker { protected VisitorRepository $visitorRepository; + protected SiteService $siteService; private EventDispatcherInterface $eventDispatcher; - public function __construct(VisitorRepository $visitorRepository, EventDispatcherInterface $eventDispatcher) - { + public function __construct( + VisitorRepository $visitorRepository, + SiteService $siteService, + EventDispatcherInterface $eventDispatcher + ) { $this->visitorRepository = $visitorRepository; + $this->siteService = $siteService; $this->eventDispatcher = $eventDispatcher; } @@ -70,6 +76,7 @@ protected function getPageVisit(int $pageUid, int $languageUid, string $referrer $page = $pageRepository->findByUid($pageUid); $pageVisit ->setPage($page) + ->setSite($this->siteService->getSiteIdentifierFromPageIdentifier($pageUid)) ->setLanguage($languageUid) ->setReferrer($referrer) ->setDomainAutomatically() diff --git a/Classes/Domain/Tracker/SearchTracker.php b/Classes/Domain/Tracker/SearchTracker.php index 24b3d608..8bc89ed1 100644 --- a/Classes/Domain/Tracker/SearchTracker.php +++ b/Classes/Domain/Tracker/SearchTracker.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace In2code\Lux\Domain\Tracker; -use Doctrine\DBAL\DBALException; +use In2code\Lux\Domain\Model\Pagevisit; use In2code\Lux\Domain\Model\Search; use In2code\Lux\Domain\Model\Visitor; use In2code\Lux\Domain\Repository\VisitorRepository; @@ -37,10 +37,10 @@ public function __construct(VisitorRepository $visitorRepository, EventDispatche /** * @param Visitor $visitor * @param array $arguments + * @param Pagevisit|null $pagevisit * @return void - * @throws DBALException */ - public function track(Visitor $visitor, array $arguments): void + public function track(Visitor $visitor, array $arguments, Pagevisit $pagevisit = null): void { if ($this->isTrackingActivated($visitor, $arguments)) { $searchTerm = $this->getSearchTerm($arguments['currentUrl']); @@ -51,6 +51,9 @@ public function track(Visitor $visitor, array $arguments): void 'crdate' => time(), 'tstamp' => time(), ]; + if ($pagevisit !== null) { + $properties['pagevisit'] = $pagevisit->getUid(); + } $queryBuilder->insert(Search::TABLE_NAME)->values($properties)->executeStatement(); $searchUid = $queryBuilder->getConnection()->lastInsertId(); $this->eventDispatcher->dispatch( diff --git a/Classes/Domain/Tracker/StopTracking.php b/Classes/Domain/Tracker/StopTracking.php index d4ab2c3c..ee217e8e 100644 --- a/Classes/Domain/Tracker/StopTracking.php +++ b/Classes/Domain/Tracker/StopTracking.php @@ -25,7 +25,7 @@ class StopTracking * * @var array */ - protected $blacklistedBrowsers = [ + protected array $blacklistedBrowsers = [ 'Googlebot', ]; @@ -34,7 +34,7 @@ class StopTracking * * @var array */ - protected $blacklistedUa = [ + protected array $blacklistedUa = [ 'googlebot', 'pinterestbot', 'linkedinbot',