feat: add PostHog analytics integration - #1
posthog[bot] wants to merge 1 commit into
Conversation
Instrument career and tribes server actions with PostHog event capture (career_plan_generated, achievement_created, compensation_entry_created, tribe_post_created), added by the PostHog setup wizard. Generated-By: PostHog Code Task-Id: 6b492a56-ecff-458c-8815-5a36243e3859
Reviewer's GuideAdds PostHog product analytics integration across the app, wiring both server-side event capture and client-side initialization, and instrumenting key user actions with tracked events and session IDs. Sequence diagram for server-side PostHog event capture on career actionssequenceDiagram
actor User
participant Browser
participant NextServer
participant PostHogServerClient
participant PostHog
User->>Browser: submit createCompensationAction
Browser->>NextServer: createCompensationAction(input)
NextServer->>NextServer: requireAppUser()
NextServer->>NextServer: compensationSchema.parse(input)
NextServer->>NextServer: createCompensationEntry(appUser.id, values)
NextServer->>NextServer: headers()
NextServer->>PostHogServerClient: getPostHogSessionId(headers)
NextServer->>PostHogServerClient: capturePostHogEvent({distinctId, event, properties, sessionId})
PostHogServerClient->>PostHog: send event
NextServer->>NextServer: revalidatePath("/career")
NextServer-->>Browser: { success, data: { entryId } }
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Heads up: the check CI job fails on this PR at the `Secret scan (basic)` step, but this is pre-existing on `main` and unrelated to this PostHog integration — the same failure occurs on the latest `main` runs (e.g. run 31146703673). The `git grep` in .github/workflows/ci.yml matches its own pattern text (`sk_test_[A-Za-z0-9]{20,}`) inside the workflow file itself, so the step always self-triggers regardless of branch content. Since this isn't caused by the PostHog changes here and fixing it would mean editing `.github/workflows/**` (out of scope for this PR), I'm leaving it as-is and flagging it here instead. All PostHog-specific checks pass locally: `pnpm lint`, `pnpm typecheck`, `pnpm test`, and `pnpm build` (with the CI env vars) all exit 0. |
Summary
This PR ships the PostHog setup wizard's integration, adding product analytics via
posthog-js(browser) andposthog-node(server).lib/posthog-server.ts- server-side PostHog client (capturePostHogEvent,getPostHogSessionId), readsNEXT_PUBLIC_POSTHOG_PROJECT_TOKEN/NEXT_PUBLIC_POSTHOG_HOSTinstrumentation-client.ts- browser PostHog client initializationapp/global-error.tsx- global exception capturecomponents/dashboard/app-shell.tsx- persisted Clerk-backed user identification at the authenticated root boundaryapp/(app)/career/actions.ts- server-side captures forcareer_plan_generated,achievement_created,compensation_entry_createdapp/(app)/tribes/actions.ts- server-side capture fortribe_post_createdInsights and dashboards created
How to verify
career_plan_generated,achievement_created,compensation_entry_created, andtribe_post_createdevents arriving within a minute, plus$pageview/browser events from the client SDK.Environment variables (action needed before PostHog works in production)
This app deploys to Netlify (
netlify.toml,docs/deploy/netlify.md), which keeps environment variables in Site settings rather than in a file committed to this repo — and the project's own deploy docs say not to commit them there. So these were not set automatically:NEXT_PUBLIC_POSTHOG_PROJECT_TOKENphc_n9HueS97MiWZazRpsiuvnnwa9z92r6QVqWnRawaBmEC4NEXT_PUBLIC_POSTHOG_HOSThttps://us.i.posthog.comTo set them via the Netlify dashboard:
Or via the Netlify CLI (already used by this project per
docs/deploy/netlify.md):Created with PostHog Code
Summary by Sourcery
Integrate PostHog analytics tracking into key career and tribes server actions to capture user engagement events.
New Features:
Enhancements: