User feedback + expanded privacy-respecting admin stats

Feedback:
- feedback table; POST /api/feedback (anonymous-ok, optional category/email,
  honeypot + per-day flood cap) stores + emails the admin; GET /api/admin/feedback.
- Shared feedback store + FeedbackModal; a speech-bubble opens it from the desktop
  header, the mobile top bar (logo moves left), the footer, and /account. Feedback
  section in /admin.

Stats (additive, same privacy model — no IP/UA/referrer/raw terms):
- Event vocab: summary_viewed (fired on /a load), full_story (card → source),
  not_today/less_like_this/hide_topic, replace_used/replace_none, paywall_replace,
  paywalled_source_open. Card title/image opens /a (no double-count); history
  records via keepalive so it survives the nav.
- Dashboard: Accounts card (counts only), reading funnel (summary→source rate),
  emotional-mix & friction, paywall, returning-visitor buckets. (Health metrics
  deferred to a future monitoring dashboard.) 131 tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jay
2026-06-05 12:58:49 +00:00
parent cfde4e22db
commit 427210ac3e
16 changed files with 460 additions and 38 deletions
+64 -17
View File
@@ -222,35 +222,76 @@ def admin_stats(conn: sqlite3.Connection, days: int = 30) -> dict:
).fetchall()
loyalty = {r["g"]: r["n"] for r in rows}
# An "open" = opening the article via our summary page (legacy 'open' + 'summary_viewed').
OPEN = "e.kind IN ('open','summary_viewed')"
top_articles = [dict(r) for r in conn.execute(
"SELECT e.article_id AS id, a.title, src.name AS source, COUNT(*) AS opens "
"FROM events e JOIN articles a ON a.id=e.article_id JOIN sources src ON src.id=a.source_id "
"WHERE e.kind='open' AND e.article_id>0 AND e.day>=date('now',?) "
"GROUP BY e.article_id ORDER BY opens DESC LIMIT 12", (since,),
f"SELECT e.article_id AS id, a.title, src.name AS source, COUNT(*) AS opens "
f"FROM events e JOIN articles a ON a.id=e.article_id JOIN sources src ON src.id=a.source_id "
f"WHERE {OPEN} AND e.article_id>0 AND e.day>=date('now',?) "
f"GROUP BY e.article_id ORDER BY opens DESC LIMIT 12", (since,),
)]
top_groupings = [dict(r) for r in conn.execute(
"SELECT t.tag, COUNT(*) AS opens FROM events e JOIN article_tags t ON t.article_id=e.article_id "
"WHERE e.kind='open' AND e.day>=date('now',?) GROUP BY t.tag ORDER BY opens DESC LIMIT 12", (since,),
f"SELECT t.tag, COUNT(*) AS opens FROM events e JOIN article_tags t ON t.article_id=e.article_id "
f"WHERE {OPEN} AND e.day>=date('now',?) GROUP BY t.tag ORDER BY opens DESC LIMIT 12", (since,),
)]
top_topics = [dict(r) for r in conn.execute(
"SELECT s.topic, COUNT(*) AS opens FROM events e JOIN article_scores s ON s.article_id=e.article_id "
"WHERE e.kind='open' AND s.topic IS NOT NULL AND e.day>=date('now',?) "
"GROUP BY s.topic ORDER BY opens DESC", (since,),
f"SELECT s.topic, COUNT(*) AS opens FROM events e JOIN article_scores s ON s.article_id=e.article_id "
f"WHERE {OPEN} AND s.topic IS NOT NULL AND e.day>=date('now',?) "
f"GROUP BY s.topic ORDER BY opens DESC", (since,),
)]
share_rows = conn.execute(
"SELECT kind, COUNT(*) n FROM events "
"WHERE kind IN ('share_ub','copy_source','native_share','source_click') AND day>=date('now',?) "
"GROUP BY kind", (since,),
# Counts per event kind over the window (each = distinct visitor-days, by dedup).
kc_rows = conn.execute(
"SELECT kind, COUNT(*) n FROM events WHERE day>=date('now',?) GROUP BY kind", (since,)
).fetchall()
shares = {k: 0 for k in ("share_ub", "copy_source", "native_share", "source_click")}
shares.update({r["kind"]: r["n"] for r in share_rows})
kc = {r["kind"]: r["n"] for r in kc_rows}
shares = {k: kc.get(k, 0) for k in ("share_ub", "copy_source", "native_share", "source_click")}
summary_views = kc.get("summary_viewed", 0)
source_clicks = kc.get("source_click", 0)
funnel = {
"summary_viewed": summary_views,
"source_click": source_clicks,
"full_story": kc.get("full_story", 0),
"source_rate": round(100 * source_clicks / summary_views) if summary_views else 0,
}
emotional_mix = {
"not_today": kc.get("not_today", 0),
"less_like_this": kc.get("less_like_this", 0),
"hide_topic": kc.get("hide_topic", 0),
}
paywall = {
"paywall_replace": kc.get("paywall_replace", 0),
"paywalled_source_open": kc.get("paywalled_source_open", 0),
}
replace = {"used": kc.get("replace_used", 0), "none": kc.get("replace_none", 0)}
# Accounts — aggregate counts only (no emails, no per-user listing).
accounts = {
"total": scalar("SELECT COUNT(*) FROM users"),
"new_today": scalar("SELECT COUNT(*) FROM users WHERE date(created_at)=date('now')"),
"new_7d": scalar("SELECT COUNT(*) FROM users WHERE created_at>=date('now','-7 days')"),
"new_30d": scalar("SELECT COUNT(*) FROM users WHERE created_at>=date('now',?)", (since,)),
"active_7d": scalar("SELECT COUNT(DISTINCT user_id) FROM sessions WHERE last_seen_at>=date('now','-7 days')"),
}
# Returning-visitor buckets by distinct active days in the window.
bucket_rows = conn.execute(
"SELECT CASE WHEN d>=8 THEN '8+' WHEN d>=4 THEN '4-7' WHEN d>=2 THEN '2-3' ELSE 'new' END b, "
"COUNT(*) n FROM (SELECT visitor_hash, COUNT(DISTINCT day) d FROM events "
"WHERE kind='visit' AND visitor_hash!='' AND day>=date('now',?) GROUP BY visitor_hash) GROUP BY b",
(since,),
).fetchall()
buckets = {r["b"]: r["n"] for r in bucket_rows}
retention = {k: buckets.get(k, 0) for k in ("new", "2-3", "4-7", "8+")}
daily = [dict(r) for r in conn.execute(
"SELECT day, SUM(kind='open') AS opens, SUM(kind='visit') AS visits FROM events "
"WHERE day>=date('now',?) GROUP BY day ORDER BY day", (since,),
"SELECT day, SUM(kind IN ('open','summary_viewed')) AS opens, SUM(kind='visit') AS visits "
"FROM events WHERE day>=date('now',?) GROUP BY day ORDER BY day", (since,),
)]
return {
@@ -258,6 +299,12 @@ def admin_stats(conn: sqlite3.Connection, days: int = 30) -> dict:
"visitors": visitors,
"returning": loyalty.get("returning", 0),
"once": loyalty.get("once", 0),
"retention": retention,
"accounts": accounts,
"funnel": funnel,
"emotional_mix": emotional_mix,
"paywall": paywall,
"replace": replace,
"top_articles": top_articles,
"top_groupings": top_groupings,
"top_topics": top_topics,