Skip to content

Reproducible insights page from PostHog data - #76

Open
liladas wants to merge 3 commits into
mainfrom
insights-script
Open

liladas wants to merge 3 commits into
mainfrom
insights-script

Conversation

@liladas

@liladas liladas commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Makes the analytics page from the first-month writeup reproducible, so it can be rebuilt against fresh data instead of being a one-off.

Usage

npm run insights              # query PostHog, write insights/index.html
npm run insights -- --check   # also render it headlessly and fail on problems
npm run insights -- --offline # rebuild from the last cached query results

Needs POSTHOG_API_KEY in .env. Unlike the VITE_* values this one is a real secret, so .env.example now documents it with that warning. Output is gitignored.

Layout

  • scripts/insights.mjs — query, shape, interpolate, render check.
  • scripts/insights/queries.mjs — named HogQL queries.
  • scripts/insights/notes.json — the prose.
  • scripts/insights/template.html — the page, with {{token}} slots.

The page is a single self-contained file: fonts are inlined from @fontsource as data URIs and the charts are hand-rolled SVG (about 150 lines), because it is published as an Artifact where a strict CSP blocks every external request. No chart library is reachable there.

Two things worth reviewing

Prose goes stale in a way numbers do not. A rerun refreshes every figure but cannot refresh a sentence like "three spikes carry the month". So the narrative lives in notes.json with a writtenFor range, and the build warns when the data has moved past it:

[insights] NOTE: the narrative in scripts/insights/notes.json was written for
2026-06-27 → 2026-07-14, but the data now covers 2026-06-27 → 2026-07-30.
[insights] Numbers regenerated; sentences did not.

Claims that are really derived stats use {{tokens}} instead and update themselves. That immediately caught one of my own errors: I had hand-written the spike days as "July 24, 26 and 27", but the top three days by completed conversions are July 26, 27 and 29. I had misread the started column. The generated text is correct.

Query correctness. PostHog applies an implicit LIMIT 100 to HogQL when the query does not set one, which silently truncates group-by results; that bit me while exploring, so every query here sets an explicit limit. The self and datacenter city lists are now defined once and shared, which fixes a real inconsistency in the published page, where the daily series excluded 3 datacenter cities and the profile breakdown excluded 4. One Ashburn profile was counted differently by the two. The artifact has been republished from this script's output.

Verification

npm run insights -- --check from a clean checkout reproduces 34 days, 624 conversions and 137 profiles, and passes all three render checks (light, dark, 420px) with zero console errors and no horizontal overflow. Lint, format, typecheck and the 123 unit tests all pass. Verified the API key does not appear in the generated HTML.

The eslint change adds browser globals to the existing Node-side block, since these files author page.evaluate callbacks that run in the browser. The e2e specs already did this and only escaped the rule because no-undef is off for TypeScript.

liladas added 3 commits July 30, 2026 21:11
npm run insights queries PostHog, rebuilds insights/index.html, and (with
--check) renders it headlessly in light, dark and mobile, failing on console
errors, horizontal overflow, or a missing chart.

The page is one self-contained file: fonts inlined from @fontsource as data
URIs and hand-rolled SVG charts, because it is published as an Artifact where
a strict CSP blocks every external request.

- scripts/insights/queries.mjs holds named HogQL queries, each with an explicit
  LIMIT (PostHog silently caps ungoverned results at 100 rows, which truncates
  group-by output) and one shared self/datacenter city list, so the daily
  series and the profile breakdown can no longer disagree
- scripts/insights/notes.json holds the interpretive prose separately from the
  data, with a writtenFor range; the build warns when the data moves past it,
  since a rerun refreshes numbers but never sentences
- prose can embed {{tokens}} (topDays, profiles, failed, ...) so claims that
  are really derived stats update themselves
- output is gitignored; --offline rebuilds from the cached query results
- POSTHOG_API_KEY documented in .env.example, flagged as a real secret unlike
  the publishable VITE_* values

eslint: the Node-side block now also declares browser globals, since these
files author page.evaluate callbacks that execute in the browser.
prettier --check covers it now that the gitignore rule is anchored to
/insights/; before that the unanchored pattern also matched scripts/insights/
and prettier skips gitignored files, so it was never checked locally.

Whitespace only: the generated page's data, prose and render checks are
unchanged.
The page was half dashboard, half write-up: the callout described one
specific machine's failures, and other copy named users and called out
particular days. None of that survives a rerun, so every rebuild needed
prose review before it was publishable.

Now every string in notes.json explains what a number measures and how to
read it, and nothing describes a particular week. The convention is stated
at the top of that file. Incident and trend analysis belongs in a blog post
or an issue, where it can be dated.

- the hand-written callout is replaced by a failure-reasons table built from
  the failures query, which was already being fetched and never displayed
- {{topDay}} replaces a typed-in list of spike dates
- writtenFor and the staleness warning are gone; they existed to guard prose
  that no longer exists
- the self bucket label no longer hardcodes a city from queries.mjs

Any rerun is now publishable as-is.
@liladas

liladas commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Updated: the page is now definitional rather than dated, so any rerun is publishable without reviewing prose.

The first version was half dashboard, half write-up. It named specific users, called out particular days, and had a callout describing one machine's SIMD failures. None of that survives a rerun, which is why the build had to warn about stale narrative.

Rather than warn about the problem, the prose that caused it is gone. Every string in notes.json now explains what a number measures and how to read it:

Every completed conversion since tracking began. Read the slope, not the height: a near-vertical stretch is usually one person working through a disc collection rather than a jump in how many people are using the tool.

Incident and trend analysis belongs in a blog post or an issue, where it can carry a date. The convention is stated at the top of notes.json.

Consequent changes:

  • The hand-written callout is replaced by a failure-reasons table built from the failures query, which the script was already fetching and never displaying. It shows reason, stage, failure count and affected profile count, so a failure cluster can be diagnosed from the page instead of described in it.
  • {{topDay}} replaces a typed-in list of spike dates.
  • writtenFor and the staleness warning are removed. They guarded prose that no longer exists.
  • The "my own testing" bucket label no longer hardcodes a city that is configured in queries.mjs.

The earlier CI failure is fixed: prettier --check covers scripts/insights/template.html now that the gitignore rule is anchored to /insights/. Before that the unanchored insights/ pattern also matched scripts/insights/, and prettier skips gitignored files, so the template was never checked locally. All five CI steps now run clean locally before each push.

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.

1 participant