Add public PageView tracking for portal analytics.
CI / test (pull_request) Successful in 13s

Closes #3 — record successful public GET hits, show last-30-day views and top pages on the analytics report, and keep portal/admin/API paths out of the counts.
This commit is contained in:
2026-08-20 05:32:09 -05:00
parent 30ce58e610
commit b054097f3d
9 changed files with 234 additions and 7 deletions
+8 -3
View File
@@ -5,7 +5,7 @@ from django.db.models import Count
from django.shortcuts import render
from django.utils import timezone
from analytics.models import Attribution, UTMVisit
from analytics.models import Attribution, PageView, UTMVisit
def _bar_pct(rows, key="count"):
@@ -18,7 +18,11 @@ def _bar_pct(rows, key="count"):
@login_required
def report(request):
since_30d = timezone.now() - timedelta(days=30)
visits_last_30_days = UTMVisit.objects.filter(created_at__gte=since_30d).count()
pageviews_qs = PageView.objects.filter(created_at__gte=since_30d)
pageviews_last_30_days = pageviews_qs.count()
top_pages = list(
pageviews_qs.values("path").annotate(count=Count("id")).order_by("-count")[:20]
)
visits_by_source = _bar_pct(
list(
@@ -60,7 +64,8 @@ def report(request):
request,
"analytics/report.html",
{
"visits_last_30_days": visits_last_30_days,
"pageviews_last_30_days": pageviews_last_30_days,
"top_pages": top_pages,
"total_visits": UTMVisit.objects.count(),
"total_attributed": Attribution.objects.count(),
"top_source": (top_visit or {}).get("utm_source") or "(direct)",