Skip to content

feat(telemetry): set App Insights authenticated user context for per-user tracing#2216

Merged
RodriSanchez1 merged 1 commit into
masterfrom
enhance/appisightuserid
Jun 10, 2026
Merged

feat(telemetry): set App Insights authenticated user context for per-user tracing#2216
RodriSanchez1 merged 1 commit into
masterfrom
enhance/appisightuserid

Conversation

@RodriSanchez1

Copy link
Copy Markdown
Collaborator

Closes #2206

What

Sets the Application Insights authenticated user context so that all telemetry (requests, dependencies, exceptions, customEvents, traces, pageViews) carries a user_AuthenticatedId, making per-user tracing trivial:

union requests, dependencies, exceptions, customEvents, traces
| where timestamp > ago(90d)
| where user_AuthenticatedId == "<USER_ID>"
| order by timestamp asc

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.js
    • loginSuccess: appInsights.setAuthenticatedUserContext(payload.id) on login success (alongside the existing FirebasePlugin.setUserId).
    • logout: appInsights.clearAuthenticatedUserContext(). This also covers the People logout path, which dispatches the same logout() action.
  • App.container.js: re-apply setAuthenticatedUserContext(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.
  • All calls are wrapped in try/catch to mirror the existing defensive Firebase/gtag handling.
  • Tests added for the login (set) and logout (clear) behavior.

Important: identifier choice

Uses the Mongo user _id (the same value already passed to FirebasePlugin.setUserId), never the email. The email is PII and would be persisted in Azure; the opaque _id is the correct identifier.

Compatibility

@microsoft/applicationinsights-web@2.8.18 (the pinned version) exposes both setAuthenticatedUserContext and clearAuthenticatedUserContext on the main ApplicationInsights class — verified against the package typings.

Acceptance criteria

  • user_AuthenticatedId is populated on telemetry for logged-in users (requests, dependencies, exceptions, custom events, traces, page views).
  • The identifier used is the Mongo _id, not the email.
  • Context is set on login and re-applied on app start for already-logged-in users.
  • Context is cleared on logout.

Testing

yarn test --watchAll=false --testPathPattern="Login.actions" → 15 passed (incl. 2 new tests).

Follow-up (out of scope)

The UserData_EmailLost instrumentation noted in the issue is tracked separately.

🤖 Generated with Claude Code

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>
@RodriSanchez1 RodriSanchez1 merged commit d85733d into master Jun 10, 2026
5 checks passed
@RodriSanchez1 RodriSanchez1 deleted the enhance/appisightuserid branch June 10, 2026 19:53
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.

Set authenticated user context in Application Insights for per-user telemetry tracing

1 participant