From 27819c73ef41e100ebda60058ef502cccd26d004 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 13 Feb 2020 12:26:19 +0100 Subject: [PATCH] Add user agent to tracking To enable fine grained filtering, and detect further bots / crawler to block. --- Classes/Domain/Model/Pageview.php | 14 +++++++++++++- Classes/Domain/Pageview/Factory.php | 3 ++- Classes/Domain/Repository/Pageview.php | 1 + Configuration/TCA/tx_tracking_pageview.php | 11 +++++++++-- Resources/Private/Language/locallang_tca.xlf | 3 +++ ext_tables.sql | 1 + 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Classes/Domain/Model/Pageview.php b/Classes/Domain/Model/Pageview.php index 6f39b83..83404d4 100644 --- a/Classes/Domain/Model/Pageview.php +++ b/Classes/Domain/Model/Pageview.php @@ -50,18 +50,25 @@ class Pageview */ private $url; + /** + * @var string + */ + private $userAgent; + public function __construct( int $pageUid, SiteLanguage $language, \DateTimeImmutable $crdate, int $pageType, - string $url + string $url, + string $userAgent ) { $this->pageUid = $pageUid; $this->language = $language; $this->crdate = $crdate; $this->pageType = $pageType; $this->url = $url; + $this->userAgent = $userAgent; } public function getPageUid(): int @@ -88,4 +95,9 @@ class Pageview { return $this->url; } + + public function getUserAgent(): string + { + return $this->userAgent; + } } diff --git a/Classes/Domain/Pageview/Factory.php b/Classes/Domain/Pageview/Factory.php index cd5faea..f6a1ef3 100644 --- a/Classes/Domain/Pageview/Factory.php +++ b/Classes/Domain/Pageview/Factory.php @@ -35,7 +35,8 @@ class Factory implements FromRequest $request->getAttribute('language'), new \DateTimeImmutable(), static::getRouting($request)->getPageType(), - (string) $request->getUri() + (string) $request->getUri(), + $request->getHeader('User-Agent')[0] ?? '' ); } diff --git a/Classes/Domain/Repository/Pageview.php b/Classes/Domain/Repository/Pageview.php index b6b7793..069d2ff 100644 --- a/Classes/Domain/Repository/Pageview.php +++ b/Classes/Domain/Repository/Pageview.php @@ -47,6 +47,7 @@ class Pageview 'type' => $pageview->getPageType(), 'sys_language_uid' => $pageview->getLanguage()->getLanguageId(), 'url' => $pageview->getUrl(), + 'user_agent' => $pageview->getUserAgent(), ] ); } diff --git a/Configuration/TCA/tx_tracking_pageview.php b/Configuration/TCA/tx_tracking_pageview.php index 686b416..5ecc63b 100644 --- a/Configuration/TCA/tx_tracking_pageview.php +++ b/Configuration/TCA/tx_tracking_pageview.php @@ -14,11 +14,11 @@ return [ ], 'interface' => [ 'always_description' => 0, - 'showRecordFieldList' => 'url, type, sys_language_uid, crdate, tstamp, crdate, cruser_id' + 'showRecordFieldList' => 'url, user_agent, type, sys_language_uid, crdate, tstamp, crdate, cruser_id' ], 'types' => [ '0' => [ - 'showitem' => 'sys_language_uid, pid, url, type, crdate', + 'showitem' => 'sys_language_uid, pid, url, user_agent, type, crdate', ], ], 'columns' => [ @@ -50,6 +50,13 @@ return [ 'readOnly' => true, ] ], + 'user_agent' => [ + 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.user_agent', + 'config' => [ + 'type' => 'input', + 'readOnly' => true, + ], + ], 'type' => [ 'label' => 'LLL:EXT:tracking/Resources/Private/Language/locallang_tca.xlf:table.pageview.type', 'config' => [ diff --git a/Resources/Private/Language/locallang_tca.xlf b/Resources/Private/Language/locallang_tca.xlf index 69269df..03f191e 100644 --- a/Resources/Private/Language/locallang_tca.xlf +++ b/Resources/Private/Language/locallang_tca.xlf @@ -24,6 +24,9 @@ Pagetype + + User agent + diff --git a/ext_tables.sql b/ext_tables.sql index 866fccd..dc0b8fb 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -1,4 +1,5 @@ CREATE TABLE tx_tracking_pageview ( url text, + user_agent text, type int(11) unsigned DEFAULT '0' NOT NULL, );