feat(web): wire PostHog analytics into sqlritedb.com (SQLR-7)#137
Merged
Conversation
- 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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Wires up product analytics on sqlritedb.com via
@posthog/next(v0.4.47). Resolves SQLR-7.PostHogProvider+PostHogPageViewmounted inweb/src/app/layout.tsxfor pageviews + autocapture.web/src/middleware.tsrunspostHogMiddleware({ proxy: true })to seed the anonymous identity cookie on first visit and reverse-proxy/ingest/*tohttps://us.i.posthog.com(dodges ad-blockers).NEXT_PUBLIC_POSTHOG_KEYso preview deploys that haven't configured the secret still build and serve. Reads optionalNEXT_PUBLIC_POSTHOG_HOST(defaults tohttps://us.i.posthog.com).PostHogProviderruns withoutbootstrapFlags, so all 49 prerendered routes (/,/docs,/blog,/blog/[slug], blog tags, RSS, OG images, sitemap, robots) keep static generation.Matcher
Middleware matcher uses two entries (matchers OR together):
/ingest/:path*— always run on PostHog proxy paths. Required because the file-extension exclusion below would otherwise drop/ingest/static/array.js./((?!_next/static|_next/image|favicon\.ico|.*\..*|.*opengraph-image.*|.*twitter-image.*).*)— page navigations only. Excludes Next internals, the favicon, any path with a.(sitemap, robots, rss, images, fonts), and the dynamic OG/Twitter image metadata routes to keep Vercel middleware invocations bounded.Out of scope (separate tickets if/when needed)
clientOptionstopersistence: 'memory'until consent is given.us.i.posthog.com(andeu.i.posthog.comif the host env var changes).Verification
npm run typecheck(inweb/) — clean.npm run lint— no warnings or errors.npm run buildwithoutNEXT_PUBLIC_POSTHOG_KEY— succeeds; 49 routes prerendered (preview-deploy-without-secret path).npm run buildwithNEXT_PUBLIC_POSTHOG_KEY=phc_test_dummy_key— succeeds; same 49 routes still prerender (PostHogProvideris static-render-safe withoutbootstrapFlags).GET /docs→set-cookie: ph_phc_..._posthog=...(middleware seeded identity cookie). ✓GET /sitemap.xml,/robots.txt,/blog/rss.xml,/opengraph-image,/blog/<slug>/opengraph-image→ noset-cookie(matcher correctly excluded). ✓GET /ingest/static/array.js→200+x-middleware-rewrite: https://us.i.posthog.com/static/array.js. ✓Test plan
web/builds withoutNEXT_PUBLIC_POSTHOG_KEYsetweb/builds withNEXT_PUBLIC_POSTHOG_KEYsetnpm run typecheck+npm run lintclean inweb/next build/ingest/*proxies to PostHog ingest hostNEXT_PUBLIC_POSTHOG_KEYset in env, events appear in PostHog within ~1 min of visiting sqlritedb.comNotes for reviewer
NEXT_PUBLIC_*env vars are inlined atnext buildtime — after setting the key in Vercel you must redeploy for it to take effect. Documented in the updatedweb/README.md"Deploying" section.@posthog/nextis at v0.4.x (the docs label it pre-release). API is stable for the surface we use; if a 1.x lands later,PostHogProvider/PostHogPageView/postHogMiddlewareare the documented stable entry points.🤖 Generated with Claude Code