diff --git a/src/Controller/Api/VisitController.php b/src/Controller/Api/VisitController.php new file mode 100644 index 0000000..61f7d80 --- /dev/null +++ b/src/Controller/Api/VisitController.php @@ -0,0 +1,33 @@ +getContent(), true); + + if (!isset($data['route']) || empty($data['route'])) { + return new JsonResponse(['error' => 'Route is required'], Response::HTTP_BAD_REQUEST); + } + + $route = $data['route']; + $visit = new Visit($route); + + $visitRepository->save($visit); + + return new JsonResponse(['success' => true]); + } +} diff --git a/src/Entity/Visit.php b/src/Entity/Visit.php new file mode 100644 index 0000000..e50fb21 --- /dev/null +++ b/src/Entity/Visit.php @@ -0,0 +1,49 @@ +route = $route; + $this->visitedAt = new \DateTimeImmutable(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getRoute(): string + { + return $this->route; + } + + public function setRoute(string $route): self + { + $this->route = $route; + return $this; + } + + public function getVisitedAt(): \DateTimeImmutable + { + return $this->visitedAt; + } +} diff --git a/src/Repository/VisitRepository.php b/src/Repository/VisitRepository.php new file mode 100644 index 0000000..026a518 --- /dev/null +++ b/src/Repository/VisitRepository.php @@ -0,0 +1,37 @@ + + */ +class VisitRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Visit::class); + } + + public function save(Visit $visit, bool $flush = true): void + { + $this->getEntityManager()->persist($visit); + + if ($flush) { + $this->getEntityManager()->flush(); + } + } + + public function getVisitCountByRoute(): array + { + return $this->createQueryBuilder('v') + ->select('v.route, COUNT(v.id) as count') + ->groupBy('v.route') + ->orderBy('count', 'DESC') + ->getQuery() + ->getResult(); + } +} diff --git a/templates/admin/analytics.html.twig b/templates/admin/analytics.html.twig new file mode 100644 index 0000000..862f96f --- /dev/null +++ b/templates/admin/analytics.html.twig @@ -0,0 +1,81 @@ +{% extends 'base.html.twig' %} + +{% block title %}Visitor Analytics{% endblock %} + +{% block body %} +
| Route | +Visit Count | +
|---|---|
| {{ stat.route }} | +{{ stat.count }} | +
No visit data recorded yet.
+ {% endif %} +This data shows the number of page visits per route. No personal user data is collected.
+