Widget SDK

Capture user sessions automatically with a lightweight JavaScript SDK.

Installation

npm

bash
npm install @usercontext/widget

Script Tag

html
<script src="https://cdn.usercontext.com/widget.js"></script>
<script>
  UserContext.init({
    apiKey: 'uc_your_api_key',
  });
</script>

Configuration

Pass a configuration object to UserContext.init().

OptionTypeDefaultDescription
apiKeystringrequiredYour project API key (uc_...)
endpointstringhttps://api.usercontext.comAPI endpoint URL
debugbooleanfalseEnable debug logging to console
captureConsoleLogbooleanfalseAlso capture console.log and console.info
flushIntervalMsnumber5000How often to flush the log buffer (ms)
maxBufferSizenumber50Flush when buffer reaches this many entries

Methods

UserContext.init(config)

Initialize the SDK. Call once on page load.

typescript
await UserContext.init({
  apiKey: 'uc_...',
  debug: true,
  captureConsoleLog: true,
});

UserContext.identify(userId, traits?)

Associate the session with a user.

typescript
UserContext.identify('user_123', {
  plan: 'pro',
  company: 'Acme Corp',
});

UserContext.reportIssue(description)

Create an issue from the current session. Returns the issue ID.

typescript
const issueId = await UserContext.reportIssue('Checkout button does nothing');

UserContext.captureError(error)

Manually capture an error.

typescript
try {
  riskyOperation();
} catch (e) {
  UserContext.captureError(e);
}

What’s captured automatically

CategoryWhatEvent Type
ErrorsUncaught exceptions, unhandled promise rejectionslog
Consoleconsole.error, console.warn (optionally log/info)log
NetworkFailed fetch/XHR requests (status 400+)log
ClicksAll click events with element selector, text, coordinatesclick
NavigationURL changes via pushState, replaceState, popstate, hashchangenavigation