feat(telemetry): set App Insights authenticated user context for per-user tracing#2216
Merged
Conversation
Set the App Insights authenticated user context so all telemetry (requests, dependencies, exceptions, customEvents, traces, pageViews) carries a user_AuthenticatedId, making per-user tracing trivial. - loginSuccess: setAuthenticatedUserContext(payload.id) on login - App.container: re-apply on app start for already-logged-in users, since the context is per-session - logout: clearAuthenticatedUserContext() (covers the People logout path too, which dispatches the same action) Uses the Mongo user _id, never the email (PII). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Closes #2206
What
Sets the Application Insights authenticated user context so that all telemetry (
requests,dependencies,exceptions,customEvents,traces,pageViews) carries auser_AuthenticatedId, making per-user tracing trivial:Previously the only user identifier was on the Firebase side, so finding a user's telemetry in App Insights meant fragile string-matching of the id/email inside dependency URLs — and events/exceptions/traces/page views couldn't be tied to a user at all.
Changes
Login.actions.jsloginSuccess:appInsights.setAuthenticatedUserContext(payload.id)on login success (alongside the existingFirebasePlugin.setUserId).logout:appInsights.clearAuthenticatedUserContext(). This also covers the People logout path, which dispatches the samelogout()action.App.container.js: re-applysetAuthenticatedUserContext(userId)on app start for already-logged-in users, since the context is per-session and must be re-applied on each launch. Placed in its own block (not inside the Cordova-only analytics init) because the context applies to web telemetry too.try/catchto mirror the existing defensive Firebase/gtag handling.Important: identifier choice
Uses the Mongo user
_id(the same value already passed toFirebasePlugin.setUserId), never the email. The email is PII and would be persisted in Azure; the opaque_idis the correct identifier.Compatibility
@microsoft/applicationinsights-web@2.8.18(the pinned version) exposes bothsetAuthenticatedUserContextandclearAuthenticatedUserContexton the mainApplicationInsightsclass — verified against the package typings.Acceptance criteria
user_AuthenticatedIdis populated on telemetry for logged-in users (requests, dependencies, exceptions, custom events, traces, page views)._id, not the email.Testing
yarn test --watchAll=false --testPathPattern="Login.actions"→ 15 passed (incl. 2 new tests).Follow-up (out of scope)
The
UserData_EmailLostinstrumentation noted in the issue is tracked separately.🤖 Generated with Claude Code