Browse Source

Optimize tracking

imwald
Nuša Pukšič 2 months ago
parent
commit
554bd4cd2a
  1. 12
      src/EventListener/VisitTrackingListener.php

12
src/EventListener/VisitTrackingListener.php

@ -9,24 +9,22 @@ use App\Repository\VisitRepository;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Bundle\SecurityBundle\Security;
#[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest', priority: 0)] #[AsEventListener(event: KernelEvents::REQUEST, method: 'onKernelRequest', priority: 0)]
class VisitTrackingListener class VisitTrackingListener
{ {
private const EXCLUDED_ROUTES = [ private const EXCLUDED_ROUTES = [
'/api/',
'/_profiler', '/_profiler',
'/_wdt', '/_wdt',
'/service-worker.js', '/service-worker.js',
'/manifest.webmanifest',
'/robots.txt', '/robots.txt',
'/assets/', '/assets/',
'/icons/', '/icons/',
]; ];
public function __construct( public function __construct(
private readonly VisitRepository $visitRepository, private readonly VisitRepository $visitRepository
private readonly Security $security,
) { ) {
} }
@ -40,17 +38,11 @@ class VisitTrackingListener
$request = $event->getRequest(); $request = $event->getRequest();
$route = $request->getPathInfo(); $route = $request->getPathInfo();
// Exception: Always track article publish API calls
$isArticlePublish = str_starts_with($route, '/api/article/publish');
// Skip tracking for excluded routes (API, profiler, assets, etc.)
if (!$isArticlePublish) {
foreach (self::EXCLUDED_ROUTES as $excludedRoute) { foreach (self::EXCLUDED_ROUTES as $excludedRoute) {
if (str_starts_with($route, $excludedRoute)) { if (str_starts_with($route, $excludedRoute)) {
return; return;
} }
} }
}
// Get session ID for all visitors (both logged-in and anonymous) // Get session ID for all visitors (both logged-in and anonymous)
$sessionId = null; $sessionId = null;

Loading…
Cancel
Save