feat: add PostHog identify and alias after CLI login to merge anonymous events#146
Open
devin-ai-integration[bot] wants to merge 5 commits intomainfrom
Open
feat: add PostHog identify and alias after CLI login to merge anonymous events#146devin-ai-integration[bot] wants to merge 5 commits intomainfrom
devin-ai-integration[bot] wants to merge 5 commits intomainfrom
Conversation
…us events Co-Authored-By: arsh <arshsb1998@gmail.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
…login event capture Co-Authored-By: arsh <arshsb1998@gmail.com>
Contributor
Greptile SummaryThis PR adds a PostHog Key changes:
Confidence Score: 4/5
Important Files Changed
Last reviewed commit: 65ced6c |
…/Alias events are flushed Co-Authored-By: arsh <arshsb1998@gmail.com>
Previously, GetDistinctId returned errors from machineid.ID() even when a valid email-based distinctId was available from the config file. This caused CaptureEvent to early-return and never flush enqueued Identify/ Alias events. Now non-critical errors are logged at debug level and only a missing distinctId produces an error. Co-Authored-By: arsh <arshsb1998@gmail.com>
Co-Authored-By: arsh <arshsb1998@gmail.com>
16 tasks
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.
Description 📣
Before this change, the CLI uses an anonymous machine-based
distinctId(anonymous_cli_+ machineId) for PostHog events captured before login. After login, it switches to the user's email. However, PostHog treats these as two separate persons, so pre-login CLI events are never linked to the authenticated user.This PR adds an
IdentifyUsermethod to theTelemetrystruct that:posthog.Identifycall with the user's email as thedistinctId, enriching the person record with propertiesposthog.Aliascall to link the anonymous machine ID to the user's email, merging pre-login events into the same personCalled after successful user login (after credentials are written to config).
Additionally, this PR fixes a pre-existing bug in
GetDistinctIdwhere amachineid.ID()error would causeCaptureEventto silently early-return even when a valid email-based distinctId was available from the config file. NowGetDistinctIdonly returns an error when no distinctId can be resolved at all.Companion PR: Infisical/infisical#5643 adds server-side
identifyUserin the backend auth hook with Redis dedup.Updates since last revision
defer t.posthogClient.Close()fromIdentifyUser: The initial version closed the PostHog client insideIdentifyUser, which would prevent the subsequentCaptureEvent("cli-command:login")from enqueuing. The Identify and Alias messages are now enqueued without closing — theCaptureEventcall that follows in the login flow handles flushing and closing the client.CaptureEventbefore--plainearly return:CaptureEvent("cli-command:login", ...)now runs before theplainOutputcheck so that Identify, Alias, and Capture events are all flushed viaCaptureEvent'sdefer Close()regardless of the--plainflag.GetDistinctIderror propagation: Previously returned errors frommachineid.ID()even when a valid email was available, which causedCaptureEventto bail out and silently drop all enqueued events. Now non-critical errors are logged at debug level and only an empty distinctId produces an error.fmt.Errorfwitherrors.Newfor static error string (no format verbs).Human review checklist
login.go, verify there is no code path that returns betweenTelemetry.IdentifyUser(...)andTelemetry.CaptureEvent(...). SinceIdentifyUserdeliberately does not callClose(), any return beforeCaptureEventwould silently drop the Identify/Alias events. The posthog-go client'sClose()panics (recovered) on double-call, so it must only be called once.GetDistinctIdnow returnsnilerror whenever a valid distinctId is resolved, which affects ALLCaptureEventcallers (not just login). Previously amachineid.ID()failure would suppress events even when the user was logged in. This is a strict improvement, but verify there are no callers that depend on the old error-returning behavior.IdentifyUsercall is inside theloginMethod == "user"block, so machine identity logins do not trigger identify/alias. This is intentional per the PR scope.Type ✨
Tests 🛠️
Manual testing required — this change affects PostHog telemetry, which requires a Cloud instance with a PostHog project.
No automated tests added — telemetry logic is difficult to test without mocking the PostHog client.
Link to Devin Session: https://app.devin.ai/sessions/6cf49960d59a4f7b974a7fe4861d6206
Requested by: @0xArshdeep