Skip to content

feat: add PostHog analytics integration - #1

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog/instrumentation-80246b
Draft

posthog[bot] wants to merge 1 commit into
mainfrom
posthog/instrumentation-80246b

Conversation

@posthog

@posthog posthog Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

Summary

This PR ships the PostHog setup wizard's integration, adding product analytics via posthog-js (browser) and posthog-node (server).

  • lib/posthog-server.ts - server-side PostHog client (capturePostHogEvent, getPostHogSessionId), reads NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN / NEXT_PUBLIC_POSTHOG_HOST
  • instrumentation-client.ts - browser PostHog client initialization
  • app/global-error.tsx - global exception capture
  • components/dashboard/app-shell.tsx - persisted Clerk-backed user identification at the authenticated root boundary
  • app/(app)/career/actions.ts - server-side captures for career_plan_generated, achievement_created, compensation_entry_created
  • app/(app)/tribes/actions.ts - server-side capture for tribe_post_created

Insights and dashboards created

Type Name Link
Dashboard Analytics basics (wizard) https://us.posthog.com/project/546189/dashboard/1972040
Insight Career plans generated (wizard) https://us.posthog.com/project/546189/insights/RVJIXdMY
Insight Achievements created (wizard) https://us.posthog.com/project/546189/insights/noPjlZMc
Insight Compensation entries created (wizard) https://us.posthog.com/project/546189/insights/Z15GECvk
Insight Tribe posts created (wizard) https://us.posthog.com/project/546189/insights/ABKJYwhT
Insight Career engagement actions (wizard) https://us.posthog.com/project/546189/insights/pAKQbhe5

How to verify

  1. Deploy this branch (or run it locally after setting the environment variables below).
  2. Sign in and click around — generate a career plan, add an achievement or compensation entry, or create a tribe post.
  3. In PostHog, open Activity — you should see career_plan_generated, achievement_created, compensation_entry_created, and tribe_post_created events arriving within a minute, plus $pageview/browser events from the client SDK.
  4. Open the Analytics basics (wizard) dashboard to see the trends populate.

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:

Name Value
NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN phc_n9HueS97MiWZazRpsiuvnnwa9z92r6QVqWnRawaBmEC4
NEXT_PUBLIC_POSTHOG_HOST https://us.i.posthog.com

To set them via the Netlify dashboard:

  1. Open the Netlify dashboard for this site and go to Site settings → Environment variables.
  2. Add each row from the table above (Key = Name, Value = Value), leaving all deploy contexts checked.
  3. Redeploy the site (Deploys tab → Trigger deploy) so the new variables take effect.

Or via the Netlify CLI (already used by this project per docs/deploy/netlify.md):

echo "phc_n9HueS97MiWZazRpsiuvnnwa9z92r6QVqWnRawaBmEC4" | npx netlify env:set NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN --context production
echo "https://us.i.posthog.com" | npx netlify env:set NEXT_PUBLIC_POSTHOG_HOST --context production

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:

  • Track PostHog events when users generate career plans, create achievements, and add compensation entries.
  • Track PostHog events when users create tribe posts, including association with their session and identity.

Enhancements:

  • Attach PostHog session identifiers derived from request headers to captured events for better analytics attribution.

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
@sourcery-ai

sourcery-ai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds 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 actions

sequenceDiagram
  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 } }
Loading

File-Level Changes

Change Details Files
Instrument career-related server actions to emit PostHog analytics events for key user flows.
  • Import PostHog server helpers and Next.js headers API into career server actions module.
  • After generating a 30-60-90 plan, send a PostHog event with the Clerk user ID as distinctId and a derived sessionId.
  • After creating an achievement, send a corresponding PostHog event tied to the authenticated user and current session.
  • After creating a compensation entry, send a PostHog event including the currency property and session association.
app/(app)/career/actions.ts
Instrument tribe post creation to emit a PostHog analytics event when a new tribe post is created.
  • Import PostHog server helpers and Next.js headers API into tribes server actions module.
  • After successfully creating a tribe post, send a PostHog event keyed by the Clerk user ID and current session before revalidating paths.
app/(app)/tribes/actions.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@posthog

posthog Bot commented Aug 8, 2026

Copy link
Copy Markdown
Author

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.

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.

0 participants