Skip to content

Commit b574546

Browse files
joaoh82claude
andauthored
feat(web): wire PostHog analytics into sqlritedb.com (SQLR-7) (#137)
- Add `@posthog/next` v0.4.x with `PostHogProvider` + `PostHogPageView` in the root layout for autocapture and pageview tracking. - Add `src/middleware.ts` running `postHogMiddleware({ proxy: true })` to seed the anonymous identity cookie and reverse-proxy `/ingest/*` to the PostHog ingest host (dodges ad-blockers). - Gate both the provider and middleware on `NEXT_PUBLIC_POSTHOG_KEY` so preview deploys without the secret still build and serve. - Matcher includes `/ingest/:path*` explicitly (the file-extension exclusion would otherwise block proxy paths like `/ingest/static/array.js`) and excludes Next internals, dotted paths (sitemap, robots, rss, images, fonts), and the dynamic OG/Twitter image metadata routes to keep Vercel middleware invocations bounded. - `PostHogProvider` runs without `bootstrapFlags`, so it stays static-render-safe: all 49 prerendered routes (`/`, `/docs`, `/blog`, blog tags, RSS, OG images, sitemap, robots) keep static generation under `next build`. - README's "Deploying" section now documents the env vars, the `NEXT_PUBLIC_*` build-time-baking gotcha, the analytics surface, and the open EU-consent follow-up. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 5551555 commit b574546

5 files changed

Lines changed: 591 additions & 4 deletions

File tree

web/README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,49 @@ box. Point Vercel at the `web/` directory:
225225

226226
- **Root Directory:** `web`
227227
- **Framework Preset:** Next.js (auto-detected)
228-
- No environment variables required.
228+
229+
### Environment variables
230+
231+
| Name | Required | Default | Purpose |
232+
| ---- | -------- | ------- | ------- |
233+
| `NEXT_PUBLIC_POSTHOG_KEY` | optional || PostHog project API key (`phc_…`). When set, the site loads `@posthog/next` and captures pageviews + autocapture events. When unset, the PostHog provider and middleware short-circuit to a no-op so builds still succeed. |
234+
| `NEXT_PUBLIC_POSTHOG_HOST` | optional | `https://us.i.posthog.com` | PostHog ingest host. Set to `https://eu.i.posthog.com` for the EU region. |
235+
236+
**Build-time baking gotcha.** `NEXT_PUBLIC_*` env vars are inlined into the
237+
client bundle at `next build` time, not read at deploy time. After adding
238+
or rotating the key in Vercel you **must redeploy** for the change to take
239+
effect — toggling the env var alone won't propagate to the already-built
240+
artifact.
241+
242+
### Analytics surface
243+
244+
PostHog is wired up in two places:
245+
246+
- [`src/app/layout.tsx`](src/app/layout.tsx)`PostHogProvider` +
247+
`PostHogPageView` from `@posthog/next`. The provider is rendered only when
248+
`NEXT_PUBLIC_POSTHOG_KEY` is set and runs without `bootstrapFlags`, so it
249+
stays static-render-safe (the ~43 prerendered routes — `/`, `/docs`,
250+
`/blog`, blog tags, RSS, OG images, sitemap, robots — keep their static
251+
generation).
252+
- [`src/middleware.ts`](src/middleware.ts) — `postHogMiddleware({ proxy:
253+
true })` seeds the anonymous identity cookie on first visit and reverse-
254+
proxies `/ingest/*` to the PostHog ingest host (dodges ad-blockers). The
255+
middleware matcher excludes `_next/static`, `_next/image`, `favicon.ico`,
256+
any path with a file extension (sitemap, robots, rss, images, fonts…),
257+
and the OG/Twitter image metadata routes to keep Vercel middleware
258+
invocations bounded.
259+
260+
If the env var is absent the provider is omitted and the middleware
261+
falls through to `NextResponse.next()`.
262+
263+
### Privacy / compliance follow-ups
264+
265+
The current wiring uses PostHog defaults — autocapture + cookies — and
266+
**does not** ship a consent banner. For EU visitors that is a GDPR /
267+
ePrivacy sharp edge: either add a consent banner before going live in the
268+
EU, or switch `PostHogProvider`'s `clientOptions` to `persistence:
269+
'memory'` and disable autocapture until consent is collected. Tracked
270+
separately from this initial wiring.
229271

230272
For other hosts, `next build` produces a standard Next.js output suitable
231273
for any Node-friendly runtime.

0 commit comments

Comments
 (0)