Skip to content

computed-posthog-events: fall back to the course registration anonymous distinct id for participants who never log in#2807

Merged
Will-Howard merged 4 commits into
masterfrom
wh-2713-participant-anchor-fallback
Jul 19, 2026
Merged

computed-posthog-events: fall back to the course registration anonymous distinct id for participants who never log in#2807
Will-Howard merged 4 commits into
masterfrom
wh-2713-participant-anchor-fallback

Conversation

@Will-Howard

Copy link
Copy Markdown
Collaborator

Description

When spot-checking the data in PostHog I noticed that there were some events like certificate_issued that don't get joined up to a user. It turns out that this is legitimate, and a small fraction of people complete courses without ever logging into the website.

This PR makes it so these will at least be joined up to the same anonymous user by getting a consistent distinct ID off the linked course registration.

Issue

#2713

Developer checklist

@coderabbitai

coderabbitai Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Will-Howard, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 42 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 60b5a203-cc76-493b-9405-2b3fe4f79078

📥 Commits

Reviewing files that changed from the base of the PR and between 259b14a and 9645cc3.

📒 Files selected for processing (2)
  • libraries/computed-posthog-events/src/definitions.test.ts
  • libraries/computed-posthog-events/src/definitions.ts
📝 Walkthrough

Walkthrough

Adds a shared preferredDistinctId resolver that prefers Keycloak identifiers, then registration-based anonymous identifiers, and otherwise returns null. Discussion attendance now loads linked registrations and skips events without a resolved identifier. Dropout, application lifecycle, certificate, and project submission events use the shared resolver. Project registrations now include posthogDistinctId. Tests cover anonymous fallback behaviour for certificate issuance, discussion attendance, and project submission.

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarises the main change: falling back to the registration anonymous distinct id for never-logged-in participants.
Description check ✅ Passed The description includes the required Description and Issue sections, and the checklist is completed appropriately for a non-visual change.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch wh-2713-participant-anchor-fallback

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.

@Will-Howard
Will-Howard marked this pull request as ready for review July 19, 2026 14:43
@greptile-apps

greptile-apps Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces a preferredDistinctId helper that resolves a PostHog distinct ID for computed events, preferring the user's Keycloak sub (set at first login) but falling back to the course registration's anonymous anchor when the user never logged in. Previously, events like certificate_issued, discussion_attended/absent, and project_submitted emitted null as the distinct ID for never-logged-in participants, making them unattributable in PostHog.

  • New preferredDistinctId helper replaces three copies of inline (userId ? keycloakIdentifierByUserId.get(userId) : undefined) ?? ... logic with a single consistent function.
  • discussion_attended/absent gains an extra DB query to load course registrations linked to meet-persons, enabling the anonymous fallback for participants without a Keycloak account.
  • Tests added for all three event types (certificate_issued, discussion_attended, project_submitted) covering the never-logged-in participant case.

Confidence Score: 4/5

Safe to merge — the change is additive, only making previously-dropped events attributable to an anonymous registration anchor, and all three new behaviors are covered by targeted tests.

The logic is sound and the new preferredDistinctId helper is a clean consolidation of three identical inline patterns. The only findings are a stale inline test comment and an overly long function signature, both cosmetic. No behavioral regressions were found.

No files require special attention; the test file has one stale comment worth updating.

Important Files Changed

Filename Overview
libraries/computed-posthog-events/src/definitions.ts Introduces preferredDistinctId helper and threads it through all event projection rules; also adds a new registration lookup in calculateDiscussionAttendanceEvents. Logic is correct; self-serve certificate path intentionally unchanged.
libraries/computed-posthog-events/src/definitions.test.ts Adds three new tests covering the anonymous fallback; one existing test retains a now-stale inline comment that says 'no keycloakIdentifier -> skipped' when the real skip condition is the missing applicationsBaseRecordId.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Resolve PostHog distinct_id] --> B{userId set?}
    B -- yes --> C{keycloakIdentifier in map?}
    B -- no --> E
    C -- yes --> D[Use Keycloak sub]
    C -- no --> E{registration available?}
    E -- yes --> F{posthogDistinctId on registration?}
    E -- no --> G[null — event skipped]
    F -- yes --> H[Use captured browser anon ID]
    F -- no --> I[Use registration record ID as fallback]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[Resolve PostHog distinct_id] --> B{userId set?}
    B -- yes --> C{keycloakIdentifier in map?}
    B -- no --> E
    C -- yes --> D[Use Keycloak sub]
    C -- no --> E{registration available?}
    E -- yes --> F{posthogDistinctId on registration?}
    E -- no --> G[null — event skipped]
    F -- yes --> H[Use captured browser anon ID]
    F -- no --> I[Use registration record ID as fallback]
Loading

Comments Outside Diff (1)

  1. libraries/computed-posthog-events/src/definitions.test.ts, line 410 (link)

    P2 The inline comment is stale after this PR's change. mpNoSub is now skipped because it has no applicationsBaseRecordId (no registration link to fall back to), not simply because it has no keycloakIdentifier. A future developer following the comment might incorrectly expect any user without a Keycloak sub to be silently dropped, missing that the registration anchor is now a valid fallback.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "De-AI comments" | Re-trigger Greptile

Comment thread libraries/computed-posthog-events/src/definitions.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@libraries/computed-posthog-events/src/definitions.ts`:
- Around line 36-38: Reformat the preferredDistinctId function declaration and
its destructured parameter in definitions.ts to match the project’s linting and
indentation conventions, removing the unexpected line breaks and correcting
spacing without changing its signature or behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6aa6f487-a808-4790-b6c9-5db3f76ff389

📥 Commits

Reviewing files that changed from the base of the PR and between ba44a19 and 259b14a.

📒 Files selected for processing (2)
  • libraries/computed-posthog-events/src/definitions.test.ts
  • libraries/computed-posthog-events/src/definitions.ts

Comment thread libraries/computed-posthog-events/src/definitions.ts Outdated
@Will-Howard
Will-Howard temporarily deployed to wh-2713-participant-anchor-fallback - bluedot-preview PR #2807 July 19, 2026 15:01 — with Render Destroyed
@Will-Howard
Will-Howard temporarily deployed to wh-2713-participant-anchor-fallback - bluedot-storybook-preview PR #2807 July 19, 2026 15:01 — with Render Destroyed
@Will-Howard
Will-Howard merged commit 09a3d98 into master Jul 19, 2026
7 checks passed
@Will-Howard
Will-Howard deleted the wh-2713-participant-anchor-fallback branch July 19, 2026 15:03
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.

1 participant