Skip to content

Commit

Permalink
[FEATURE] Add site and pagevisit to tracking methods
Browse files Browse the repository at this point in the history
From now on sites are also added to records by frontend visits
  • Loading branch information
einpraegsam committed Feb 21, 2024
1 parent b49c34a commit 9760403
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
19 changes: 16 additions & 3 deletions Classes/Domain/Service/SiteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Classes/Domain/Tracker/DownloadTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
43 changes: 14 additions & 29 deletions Classes/Domain/Tracker/LinkClickTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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();

Expand Down
11 changes: 9 additions & 2 deletions Classes/Domain/Tracker/PageTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions Classes/Domain/Tracker/SearchTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Classes/Domain/Tracker/StopTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class StopTracking
*
* @var array
*/
protected $blacklistedBrowsers = [
protected array $blacklistedBrowsers = [
'Googlebot',
];

Expand All @@ -34,7 +34,7 @@ class StopTracking
*
* @var array
*/
protected $blacklistedUa = [
protected array $blacklistedUa = [
'googlebot',
'pinterestbot',
'linkedinbot',
Expand Down

0 comments on commit 9760403

Please sign in to comment.