diff --git a/Classes/Middleware/Pageview.php b/Classes/Middleware/Pageview.php index d251d90..276ae07 100644 --- a/Classes/Middleware/Pageview.php +++ b/Classes/Middleware/Pageview.php @@ -27,6 +27,8 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; +use TYPO3\CMS\Core\Context\Context; class Pageview implements MiddlewareInterface { @@ -35,17 +37,41 @@ class Pageview implements MiddlewareInterface */ private $repository; - public function __construct(Repository $repository) + /** + * @var Context + */ + private $context; + + /** + * @var string + */ + private $rule = ''; + + public function __construct(Repository $repository, Context $context, string $rule) { $this->repository = $repository; + $this->context = $context; + $this->rule = $rule; } public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { - $this->repository->add(Factory::fromRequest($request)); + if ($this->shouldTrack($request, $this->context)) { + $this->repository->add(Factory::fromRequest($request)); + } return $handler->handle($request); } + + private function shouldTrack( + ServerRequestInterface $request, + Context $context + ): bool { + return (bool) (new ExpressionLanguage())->evaluate($this->rule, [ + 'request' => $request, + 'context' => $context, + ]); + } } diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 80bf2ef..5d94c1c 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -8,13 +8,21 @@ services: resource: '../Classes/*' DanielSiepmann\DI\DatabaseConnection\Pageview: - factory: - - '@TYPO3\CMS\Core\Database\ConnectionPool' - - 'getConnectionForTable' - arguments: - - 'tx_tracking_pageview' + factory: + - '@TYPO3\CMS\Core\Database\ConnectionPool' + - 'getConnectionForTable' + arguments: + - 'tx_tracking_pageview' DanielSiepmann\Tracking\Domain\Repository\Pageview: public: true arguments: - - '@DanielSiepmann\DI\DatabaseConnection\Pageview' + - '@DanielSiepmann\DI\DatabaseConnection\Pageview' + + DanielSiepmann\Tracking\Middleware\Pageview: + public: true + arguments: + $rule: > + not (context.getAspect("backend.user").isLoggedIn()) + and not (request.getHeader("User-Agent")[0] matches "/^Wget/") + and not (request.getHeader("User-Agent")[0] matches "/Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot/")