> For the complete documentation index, see [llms.txt](https://docs.userlane.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.userlane.com/developers/userlane-commands/event-callbacks.md). Agents can also search and read this documentation through its [MCP server](https://docs.userlane.com/~gitbook/mcp).

# Hooking into event callbacks

React to `onStart`, `onComplete`, and `onExit` events with custom callback functions.

## About event callbacks

The Userlane JavaScript API exposes events that application code can hook into, so the application is notified when a user does something in Userlane.

Hooking into these events makes it possible to:

* trigger a reaction in the application to what a user does in Userlane
* forward the events to an analytics tool and correlate them with data already held there
* extend Userlane behavior into the surrounding product

There are three events:

* `onStart`: fired after a user starts a Guide
* `onComplete`: fired after a user completes a Guide at its last step
* `onExit`: fired after a user cancels a Guide

{% hint style="info" %}
`onStart`, `onComplete`, and `onExit` also fire when a content creator previews a Guide from the Editor. Events forwarded to an analytics tool therefore include these previews.
{% endhint %}

## Hooking into events

All events work the same way:

```js
// hook into onStart and provide a callback function
Userlane('onStart', function (userlaneId, user) {
    // any code can go here
    console.log('the user started a Guide');
});
```

Pass the event name as the first argument and a callback function as the second. The callback receives these arguments:

1. `userlaneId` is the unique identifier of the Guide that triggered the event. Despite the name, it is the Guide ID.
2. `user` is the object holding the data Userlane has about the user at that point. It always contains an ID attribute.
3. `stepIndex` is passed to `onExit` only. It is the index of the last step the user saw, starting at 0.

## onStart

Fired whenever a Guide starts, including a Guide started by the `start` command.

It is also fired when a running Guide continues after a page load, but not when a page load restores a paused Guide. Resuming a paused Guide does not fire it either. Changing the language fires it when the Guide has loaded in the new language, unless the Guide is paused at that moment.

```js
// hook into onStart and provide a callback function
Userlane('onStart', function (userlaneId, user) {
    // any code can go here
    console.log('the user started a Guide');
});
```

## onComplete

Fired whenever a user reaches the end of a Guide, which indicates successful completion. From that point the Guide is marked as completed in the Assistant for that user.

```js
// hook into onComplete and provide a callback function
Userlane('onComplete', function (userlaneId, user) {
    // any code can go here
    console.log('the user completed a Guide');
});
```

## onExit

Fired whenever a user cancels a Guide using the Close button in a Guide step. This includes closing a Guide because one of its steps cannot be found.

It is also fired when a user exits a paused Guide. Pausing a Guide does not fire it. If another browser tab has already resumed, exited, or replaced the paused Guide, exiting it does not fire it. The same is true when the paused Guide has expired, for example because the computer was asleep for more than an hour.

It is also fired when the `init` command runs with a different property ID while a Guide is playing, because switching the property stops the Guide.

It is not fired when a Guide stops by itself because of an error, for example when none of the remaining steps can be found, or when the Guide is no longer available to the user.

```js
// hook into onExit and provide a callback function
Userlane('onExit', function (userlaneId, user, stepIndex) {
    // any code can go here
    console.log('the user canceled a Guide');
});
```

## Overwriting callbacks

Only one callback is kept per event. Hooking into an event again overwrites the previous hook.

## Removing callbacks

To unbind a callback, call the event name again with `false`, `null`, or `undefined` in place of a callback function.

```js
// remove the callback from the onStart event
Userlane('onStart', false);
```

## The user object

The second argument passed to the callback is the user object. It holds the data Userlane has about the user at the time of the event, and it always contains an ID.

Alongside the ID it carries the `generated` flag, which indicates whether the ID was generated randomly by Userlane or supplied through the API or the snippet. The `time_signup` field is included only when Userlane has a signup time for the user.

An example user object:

```js
{
    id: '9717166719265375',
    generated: true,
    time_signup: '2017-11-22T11:50:48.671Z'
}
```

## Relevant articles

* [Start a Guide with a JavaScript command](https://docs.userlane.com/developers/userlane-commands/start-a-guide-with-javascript.md)
* [Quick reference of commands](https://docs.userlane.com/developers/userlane-commands/quick-reference-of-commands.md)
* Attributes


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.userlane.com/developers/userlane-commands/event-callbacks.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
