Centralized User Identification and Persistence #38
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.
The Issue
Currently, associating a user with a session is a manual and repetitive process. Developers must pass
identity_parametersinto every singletrackcall. This architecture leads to several problems:Inconsistency: If a developer misses the identity parameters on a specific event, that event is lost to anonymity, even if the user is logged in.
Payload Bloat: Repetitive identity data is passed through the
paramsobject, cluttering the domain logic of the event (e.g., product or cart data).Integration Friction: Platforms like Fenicio, which lack automatic post-login events, require a standard way to "bind" a session to a user profile upon login.
The Change
This PR introduces a Persistent Identity Layer to the SDK. By centralizing the user state, the SDK now "remembers" who the user is across page loads and automatically attaches that identity to all outgoing tracking events.
Key Components:
UserModel: A new internal model that manages the lifecycle of the user's identity. It uses browser cookies (hello_user_idandhello_user_source) to ensure the identity persists throughout the browser session.Hellotext.identify(id, options): A new top-level method to explicitly set the user's identity. It performs an async call to the newpublic/identificationsendpoint and, upon success, persists the ID locally.track: Thetrackmethod has been refactored to automatically inject the persisted user data into the request body under a nesteduserkey.Hellotext.forget(): A cleanup method to clear the identity cookies, essential for handling user logouts and preventing data cross-contamination on shared devices.Presentation in Code
1. Centralized State (
models/user.js)The
Userclass acts as the single source of truth for the current identity.2. Clean Tracking Body (
hellotext.js)The
trackmethod now merges the global identity automatically, removing the need for manualidentity_parametersin the event params.3. Explicit Identification API
How to Use
Once the user logs in, call
identifyonce:All subsequent events will now be automatically attributed:
Note
Persistent Identity Layer
Usermodel backed by cookies (hello_user_id,hello_user_source) and exposesHellotext.identify(user_id, options)andHellotext.forget()user(fromUser.identificationData) intotrackrequest bodypublic/identificationsAPI client and wires it intoAPIidentifyAPI,IdentificationOptions/Data, andUser/CookiestypesUsermodel behaviordocs/sessions.md, adds README linkactions/checkoutandactions/setup-nodeto v6 and minor YAML quoting cleanupWritten by Cursor Bugbot for commit c112539. This will update automatically on new commits. Configure here.