Skip to content

fix(web): send anonymous PostHog events as personless#1367

Merged
brendan-kellam merged 2 commits into
mainfrom
brendan/posthog-anonymous-events
Jun 24, 2026
Merged

fix(web): send anonymous PostHog events as personless#1367
brendan-kellam merged 2 commits into
mainfrom
brendan/posthog-anonymous-events

Conversation

@brendan-kellam

@brendan-kellam brendan-kellam commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Problem

In posthog.ts, unauthenticated server-side captures shipped with an empty distinctId. PostHog treated each such request as a new person, inflating person counts and billing.

Fix

tryGetPostHogDistinctId now resolves identity by mirroring the client's identified_only contract:

  • Cookie present → use its distinct_id; identified iff $user_state === 'identified'.
  • No cookie, authenticated → use the user id (identified).
  • No cookie, unauthenticated → key the event to the install id.

Non-identified events are sent personless ($process_person_profile: false), so logged-out visitors and anonymous API calls no longer create a person profile per request. Identified users are unchanged.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Anonymous user events are now processed as personless, preventing unauthenticated requests from inflating analytics person counts and ensuring more accurate user metrics.

brendan-kellam and others added 2 commits June 23, 2026 12:40
Unauthenticated server-side captures previously shipped with an empty
distinctId, so PostHog treated each request as a brand-new person —
inflating person counts and billing. Resolve identity by mirroring the
client's `identified_only` contract: read `$user_state` from the PostHog
cookie, fall back to the authenticated user id, and otherwise key the
event to the install id with `$process_person_profile: false`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e19992f1-6351-4a53-b9c5-6adae7e017f7

📥 Commits

Reviewing files that changed from the base of the PR and between 889e2b1 and 7ed0a41.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/web/src/features/chat/llm.server.ts
  • packages/web/src/lib/posthog.ts

Walkthrough

PostHogCookie gains an optional $user_state field. tryGetPostHogDistinctId is refactored to return { distinctId, isIdentified } instead of a bare string. captureEvent uses isIdentified to inject $process_person_profile: false for anonymous events and falls back distinctId to SOURCEBOT_INSTALL_ID. llm.server.ts adopts the same destructuring and fallback.

Changes

PostHog Personless Event Handling

Layer / File(s) Summary
PostHogCookie type and tryGetPostHogDistinctId refactor
packages/web/src/lib/posthog.ts
PostHogCookie adds optional $user_state?: 'anonymous' | 'identified'. tryGetPostHogDistinctId return type changes from string | undefined to Promise<{ distinctId: string | undefined; isIdentified: boolean }>, deriving isIdentified from the cookie's $user_state or from the presence of an authenticated user id.
captureEvent personless mode and llm.server.ts wiring
packages/web/src/lib/posthog.ts, packages/web/src/features/chat/llm.server.ts, CHANGELOG.md
captureEvent destructures the new return shape, conditionally injects $process_person_profile: false when isIdentified is false, and falls back distinctId to env.SOURCEBOT_INSTALL_ID. llm.server.ts applies the same destructuring and fallback when supplying posthogDistinctId to withTracing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • sourcebot-dev/sourcebot#882: Modifies packages/web/src/lib/posthog.ts by refactoring the PostHog distinct-id helper and how captureEvent derives distinctId, which is the same file and function this PR extends with the isIdentified / personless-mode logic.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(web): send anonymous PostHog events as personless' directly and specifically describes the main change—making anonymous PostHog events personless to prevent person count inflation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch brendan/posthog-anonymous-events

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@brendan-kellam brendan-kellam merged commit 3863a29 into main Jun 24, 2026
10 checks passed
@brendan-kellam brendan-kellam deleted the brendan/posthog-anonymous-events branch June 24, 2026 01:07
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.

2 participants