You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(analytics): skip the identity lookup when no token is set
On a public page the analytics module still issued a `User/me` at client
construction: `trackInitializationEvent` queues an event, the processor
flushes the first batch immediately, and `flush` awaits `getSessionContext`,
which calls `auth.me()` unconditionally. With no session that request can
only answer 401, and the browser logs it to the console from the network
layer -- before any handler in the SDK sees the rejection. The existing
`.catch` in `getSessionContext` and the try/catch in `flush` already keep the
rejection from escaping, so no amount of additional catching removes what
users actually see.
Skip the lookup when no access token is set. Anonymous events already
reported `user_id: null` through that same `.catch`, so no analytics data
changes -- the request is dropped only in the case where its sole possible
outcome was a 401.
The dedupe from #245 is unaffected: when a token is present, analytics still
resolves through `auth.me()` and shares the app's in-flight request, so an
authenticated cold load continues to issue exactly one `User/me`.
Token presence is tracked in the auth module rather than read off
`axios.defaults`, so it follows the identity transitions that already exist
there (`setToken`, `logout`) and is seeded from the constructor token, which
is how the server-side SDK carries a session it never sets explicitly. The
skip is deliberately not memoized: a visitor who logs in mid-session must
start resolving an identity again.
This does not cover a stale or expired token, which still 401s. Removing that
one needs `User/me` to answer 200 with a null user instead of 401, which is a
backend change.
Tests: the two analytics tests that exercise identity resolution now build a
token-bearing client; the axios mock gains the `defaults` that `setToken` and
`logout` write through. Three new tests cover the anonymous skip, resolution
resuming after `setToken`, and token presence across identity changes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MtKFfzaVR6nWAi8tNvJqfw
0 commit comments