The Userpilot JavaScript SDK allows you to identify users, track events, trigger content, and manage sessions directly from your web application.

Installation

npm install @userpilot/sdk
Or include via CDN:
<script src="https://js.userpilot.io/sdk.js"></script>

Getting Started

To use the SDK, ensure it is loaded on your page. Then, you can call the available methods on the userpilot object.

Methods

identify(userId, [properties])

Identifies the current user with an ID and optional properties.
userpilot.identify("123", {
  name: "John",
  email: "john@example.com",
  created_at: "1519205055"
  // Additional user properties
});

anonymous()

Assigns a session-based unique ID for the current user on public pages.
userpilot.anonymous();

reload([options])

Updates Userpilot content when the page state changes.
userpilot.reload();
userpilot.reload({ url: 'https://example.com/hello' });

track(name, [properties])

Tracks a custom event for the current user.
userpilot.track("invitedAgent", {
  name: "Sam",
  email: "sam@example.com"
});

trigger(contentId)

Forces a specific Userpilot content to show for the current user.
userpilot.trigger(17);

on(event, callback)

Registers a callback for a Userpilot event.
userpilot.on('completed', function(event) {
  alert('Flow completed');
});

Event Examples

// Flow started
userpilot.on('started', (event) => { /* ... */ });

// Flow completed
userpilot.on('completed', (event) => { /* ... */ });

// Flow dismissed
userpilot.on('dismissed', (event) => { /* ... */ });

// Step interaction
userpilot.on('step', (event) => { /* ... */ });

off(event)

Removes a callback function attached to an event.

once(event, callback)

Registers a callback to be triggered only once.

userpilot.end()

Ends the currently running flow.

userpilot.clean()

Clears Userpilot storage and session data.

userpilot.destroy()

Fully clears Userpilot storage and removes all active content.

Parameters

MethodParametersDescription
identifyuserId, properties?Identify a user and set properties
anonymousAssign a session-based ID
reloadoptions?Reload Userpilot content
trackname, properties?Track a custom event
triggercontentIdShow specific content
onevent, callbackListen for Userpilot events
offeventRemove event listener
onceevent, callbackListen for event once
endEnd the current flow
cleanClear Userpilot storage
destroyRemove all Userpilot data and content