Skip to main content
User identity lets you connect chat conversations with your application’s user accounts. When you identify a user, their conversations become linked to their profile, forms can be pre-filled with known data, and the agent can personalize responses.

Quick Start

Identify a user when they log in:
Clear identity when they log out:

Identity Object

The identity object has two parts:

User ID

The userId should be your internal identifier for the user:

User Metadata

Pass any properties that help personalize the experience:

How Identity Is Used

1. Conversation Linking

Conversations are linked to the user ID, allowing:
  • History continuity — Users see their past conversations across devices
  • Agent context — The agent knows who they’re talking to
  • Analytics — Track conversations per user in your dashboard

2. Form Pre-filling

Forms automatically use metadata to pre-fill fields when the field name exactly matches a key in userMetadata:
Field names must exactly match the keys in userMetadata. For example, a field named user_email will only be pre-filled if you have userMetadata.user_email, not userMetadata.email.

3. Agent Personalization

The agent receives user metadata and can use it in responses:
For security, sensitive fields are automatically excluded from the agent’s context. Fields containing tokens, passwords, secrets, API keys, or credentials are filtered out. See Personalized Responses for details.

4. Dynamic Form Schemas

Form schema providers receive user context:

5. HTTP Tool Authentication

Identity data can be passed to HTTP tools for authenticated API calls. This is useful when your tools need to access user-specific data from your backend.
Then configure your HTTP tool to use the token:
The {{userId}} and {{userMetadata.path}} placeholders are replaced with the user’s identity data at runtime.
See HTTP Tools - User Context Variables for complete documentation on this feature.

6. Personalized Responses

The agent receives sanitized user context to personalize its responses. This enables greetings, contextual answers, and tailored recommendations based on user data.

Initial Message Templating

The widget’s initial message supports template variables:
If the user is identified:
If no identity or the field is missing:
Use conditional phrasing for optional fields: “Welcome back!” works whether or not you have the user’s name.

Template Syntax

If a field is undefined, it renders as an empty string.

Security: Excluded Fields

To protect sensitive data, certain fields are automatically excluded from the agent’s context. Fields with keys matching these patterns (case-insensitive) are filtered:
  • token
  • secret
  • password
  • apikey / api_key
  • accesstoken / access_token
  • refreshtoken / refresh_token
  • auth
  • credential
  • private
Sensitive fields are only filtered from the agent’s LLM context. They are still available for HTTP tool substitution (see section 5) and are sent to your backend.

Implementation Patterns

On Login

On Logout

React Context

Next.js App Router

Vue Composition API

Visitors vs Users

Ansa tracks two types of identities:

Getting Visitor ID

Visitor IDs are:
  • Generated automatically on first visit
  • Stored in localStorage
  • Persisted until cleared
  • Unique per browser/device

Linking Visitors to Users

When a visitor logs in, their anonymous conversations become linked to their user account:

Privacy Considerations

Only pass data that’s necessary for personalization. Avoid sensitive information like passwords, SSNs, or financial details.

What to Include

  • Name, email (for personalization)
  • Account type, plan (for context)
  • Preferences (for customization)
  • Non-sensitive IDs

What to Avoid

  • Passwords or secrets
  • Full credit card numbers
  • Social security numbers
  • Health information
  • Any data you wouldn’t want logged

GDPR Compliance

If a user requests data deletion:
  1. Use the API to delete their conversations
  2. Their identity data is automatically cleared
  3. Consider calling clearIdentity() to clear client state

SDK Usage

The SDK provides the same identity functions:

Debugging

Check the current identity state in the console:

Best Practices

Identify early, update often. Call identify() as soon as you know who the user is, and update metadata when it changes.
  1. Use stable IDs — Don’t use session IDs or emails as userId
  2. Keep metadata current — Update when user profile changes
  3. Reset on logout — Always call resetUser() when signing out
  4. Be selective — Only pass metadata you’ll actually use
  5. Match field names — Use standard names for automatic form pre-fill

Next Steps

Triggering Forms

Pre-fill forms with user data

Webhooks

Send user data to external systems