Add public PageView tracking for portal analytics (#4)
Deploy Beta / unit-tests (push) Successful in 12s
Deploy Beta / docker (push) Successful in 22s
Deploy Beta / deploy-beta (push) Successful in 5m40s

## Summary
Closes #3.

- Add `PageView` model + migration for successful public marketing GET hits
- Wire `PublicPageViewMiddleware` (skips portal/admin/accounts/api/static/health/etc.; fails soft on DB errors)
- Portal analytics report: last-30-day views + top pages table
- Admin registration and tests

## Test plan
- [ ] Apply migration `analytics.0002_pageview`
- [ ] Hit `/` and `/about/` → rows in `PageView` / admin
- [ ] Hit `/healthz/`, `/portal/`, `/admin/` → no new pageviews
- [ ] Open portal Analytics → views count + Top pages look right
- [ ] `uv run python site/manage.py test analytics.tests`

Reviewed-on: #4
This commit was merged in pull request #4.
This commit is contained in:
2026-08-20 03:34:56 -07:00
parent 30ce58e610
commit 46abaa6917
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)",