> 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/developer-reference/hooking-into-event-callbacks.md).

# 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, or after the Guide cancels because of an error

## Hooking into events

All events work the same way:

```javascript
// 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 two 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.

## onStart

Fired whenever a Guide starts, including a Guide started by [the start command](/developer-reference/start-a-guide-with-a-javascript-command.md).

```javascript
// 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.

```javascript
// 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. It is also fired when a Guide cancels itself after an error, or when an important step cannot be found.

```javascript
// 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.

```javascript
// remove all callbacks 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.

An example user object:

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

## Relevant articles

{% content-ref url="/pages/heJOtTedSFOThPQGYSX3" %}
[Start a Guide with a JavaScript command](/developer-reference/start-a-guide-with-a-javascript-command.md)
{% endcontent-ref %}

{% content-ref url="/pages/JaoydeD5UPCYFS9NuupA" %}
[Quick reference of commands](/developer-reference/quick-reference-of-commands.md)
{% endcontent-ref %}

{% content-ref url="/pages/gtcOzAVjL7zzv75As2Jd" %}
[Attributes](/administration/attributes.md)
{% endcontent-ref %}


---

# 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/developer-reference/hooking-into-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.
