Add bounce rate to admin Analytics dashboard

Bounce rate = visitors with only 1 pageview / total visitors.
Color-coded: green <40%, yellow <60%, red >=60%.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-26 12:13:08 +01:00
parent 3945fbb0ef
commit efe967389d
2 changed files with 15 additions and 2 deletions

View File

@@ -695,6 +695,18 @@ class AdminController extends AbstractController
->getQuery()
->getSingleScalarResult();
// Bounce rate: visitors with only 1 pageview
$bouncedVisitors = (int) $em->createQueryBuilder()
->select('COUNT(v.id)')
->from(AnalyticsUniqId::class, 'v')
->where('v.createdAt >= :since')
->andWhere('(SELECT COUNT(e2.id) FROM '.AnalyticsEvent::class.' e2 WHERE e2.visitor = v.id) = 1')
->setParameter('since', $since)
->getQuery()
->getSingleScalarResult();
$bounceRate = $visitors > 0 ? round($bouncedVisitors / $visitors * 100, 1) : 0;
$topPages = $em->createQueryBuilder()
->select('e.url, COUNT(e.id) AS hits')
->from(AnalyticsEvent::class, 'e')
@@ -755,6 +767,7 @@ class AdminController extends AbstractController
'period' => $period,
'visitors' => $visitors,
'pageviews' => $pageviews,
'bounce_rate' => $bounceRate,
'top_pages' => $topPages,
'top_referrers' => $topReferrers,
'devices' => $devices,

View File

@@ -30,8 +30,8 @@
<p class="text-3xl font-black">{{ visitors > 0 ? (pageviews / visitors)|number_format(1) : '0' }}</p>
</div>
<div class="admin-card text-center">
<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">Periode</p>
<p class="text-xl font-black">{{ {today: "Aujourd'hui", '7d': '7 derniers jours', '30d': '30 derniers jours', all: 'Depuis le debut'}[period] }}</p>
<p class="text-[10px] font-black uppercase tracking-widest text-gray-400 mb-1">Bounce Rate</p>
<p class="text-3xl font-black {% if bounce_rate < 40 %}text-green-600{% elseif bounce_rate < 60 %}text-yellow-600{% else %}text-red-600{% endif %}">{{ bounce_rate }}%</p>
</div>
</div>