Skip to content

Add in-house cookieless website analytics - #174

Merged
calebyhan merged 3 commits into
mainfrom
164/web-analytics
Nov 30, 2026
Merged

calebyhan merged 3 commits into
mainfrom
164/web-analytics

Conversation

@yewonsongg

@yewonsongg yewonsongg commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Peer2Peer wants to know how students find the public site (visitors, sources, time on site) without tracking anyone (TDD 4.6.9). Instead of adding Plausible or Umami, this stores page views in our own Postgres, so there is no third party, no extra service to run or hand off, and no cost. This resolves TDD open question 14.

Changes:

  • Added POST /api/pageviews. It accepts only the five public paths (/, /about, /contact, /faq, /resources) and the referring site's hostname, and is rate limited per IP and globally.
  • Store each view's path, referrer hostname, time, and a visitor hash. The hash is an HMAC of the IP and user agent under a random salt kept only in backend memory and replaced at local midnight, so it can't be reversed or linked across days. No cookies, IPs, or user agents are stored.
  • Added GET /api/admin/analytics/website for coordinators. It returns page views, visitors (once per day), visits (30-minute gap), average time on site, daily page views, page counts, and sources. Sources with fewer than 5 visitors are hidden, matching line analytics.
  • The retention job deletes page views older than 90 days.
  • The public layout renders a small beacon when WEBSITE_ANALYTICS_ENABLED=true. It sends nothing on /text-us, /portal, /dev, or any other path, or when the browser has Global Privacy Control or Do Not Track on. Only the first view of a page load sends a referrer.
  • Replaced the external dashboard link on /portal/admin/analytics with a Website section, and removed NEXT_PUBLIC_WEBSITE_ANALYTICS_URL.
  • Updated the FAQ disclosure and TDD 4.6.9, the deploy section, and open question 14.

Notes:

  • Visitor counts depend on the real client IP. client_ip() still reads the proxy address, the same pending deploy fix the chat and feedback rate limits need.
  • A backend restart starts a new salt, so that day's returning visitors count twice.

Verified with make check: 730 backend tests, 370 frontend tests, mypy, tsc, lint, and alembic check.

Closes #164

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Backend Test Results

730 tests  +17   730 ✅ +17   18s ⏱️ ±0s
  1 suites ± 0     0 💤 ± 0 
  1 files   ± 0     0 ❌ ± 0 

Results for commit 05314a4. ± Comparison against base commit f435caa.

♻️ This comment has been updated with latest results.

Store public page views in our own Postgres instead of a third-party
analytics service, resolving TDD open question 14.

- POST /api/pageviews accepts only allowlisted public paths and a
  referrer hostname, rate limited per IP and globally
- Visitors are an HMAC of IP and user agent under an in-memory salt that
  rotates at local midnight; no IP, user agent, or cookie is stored
- GET /api/admin/analytics/website reports page views, visitors, visits,
  average time on site, daily views, pages, and sources (sources under
  the suppression threshold are hidden)
- The retention job deletes page views older than 90 days
- The public layout renders a beacon when WEBSITE_ANALYTICS_ENABLED=true;
  it skips /text-us, /portal, /dev, and browsers with GPC or DNT
- The admin analytics page shows a Website section in place of the
  external dashboard link
@calebyhan calebyhan changed the title Add privacy-safe website analytics Add in-house cookieless website analytics Nov 30, 2026

@calebyhan calebyhan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@calebyhan
calebyhan merged commit 50cfa43 into main Nov 30, 2026
4 checks passed
@calebyhan
calebyhan deleted the 164/web-analytics branch November 30, 2026 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add privacy-safe website analytics (W7-11)

2 participants