fix(js-sdk): identify user on init when userId is present#1331
fix(js-sdk): identify user on init when userId is present#1331jagannalla wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Reviewed the JS SDK init-userId change in libs/jitsu-js/src/index.ts, the new node test coverage, and the JitsuOptions type update. I found two actionable issues worth addressing: one around unhandled promise rejection risk on init-time auto-identify, and one around user state/privacy/reset behavior when opts.userId is backfilled in user().
| if (opts.privacy?.disableUserIds || opts.privacy?.dontSend) { | ||
| storage.reset(); | ||
| } else if (opts.userId) { | ||
| a.identify(opts.userId); |
There was a problem hiding this comment.
a.identify(opts.userId) returns a Promise, but this call is fire-and-forget. With errorPolicy: "rethrow", send() can throw on network/non-OK responses, which makes this an unhandled rejection during SDK initialization. Can we await/catch this (or explicitly void ...catch(...)) so init-time auto-identify doesn’t introduce unhandled async errors?
| }, | ||
| user() { | ||
| const u = analytics.user(); | ||
| if (u && !u.id && opts.userId) { |
There was a problem hiding this comment.
Question: should this fallback ignore privacy/reset state? Right now it re-injects opts.userId into client.user().id even after storage.reset() or when privacy.disableUserIds is enabled, so callers can still observe an ID even though IDs were disabled/cleared.
|
Thanks for the review. I've pushed a new commit addressing both points:
Added corresponding unit tests for these cases and verified that the entire test suite passes. Let me know if everything looks good! |
There was a problem hiding this comment.
Reviewed the changes between f0efd99 and 6e25078 in jitsu-js (analytics plugin, SDK init path, typings, and added Node tests).\n\nI focused on correctness around auto-identification initialization, privacy/reset behavior, and async error handling for identify/track/page calls. I did not find actionable bugs or regressions in the current diff.
Summary
This PR fixes Jitsu Issue #1250 where the browser JS SDK parsed the
userIdfrom HTML script data-attributes (data-user-idorwindow.jitsuConfig.userId) into the configuration object but never automatically registered the user state or sent it with early events on startup.Root Cause
In
libs/jitsu-js/src/browser.ts, script attributes successfully parseddata-user-idintogetConfiguration().userId. However, inlibs/jitsu-js/src/index.ts'screateUnderlyingAnalyticsInstance(), the SDK never checked foropts.userIdduring startup, leading to:jitsu.page()and early events being fired without the user ID.jitsu.user().id(viajitsu.getState()) returningundefineduntil a manualidentify()was called.Changes Made
userId?: stringoption toJitsuOptionsintypes/protocols/analytics.d.tsfor first-class type safety.__user_idinside the storage wrapper right before the underlyinganalyticspackage instantiates, allowingclient.user().idto return the configured ID synchronously on startup.a.identify()during SDK startup to dispatch the identify payload to the server.test auto-identification on initinsidelibs/jitsu-js/__tests__/node/nodejs.test.tsverifying all scenarios (configuration storage, user state population, auto-triggering identify dispatch, and automatic attachment to subsequent track events).Verification
Ran the unit test suite inside an isolated Docker container:
Closes #1250