> ## Documentation Index
> Fetch the complete documentation index at: https://docs.userpilot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Callbacks

> Configure SDK callbacks for content rendering, URL changes, socket events, and auto-capture events.

## Options

| Key | Arguments | Description | Example |
| - | - | - | - |
| `on_url_change` | `(page: {hostname: string; url: string})` | Fired on each call for the reload method. | `on_url_change: (page) => console.log(page)` |
| `on_socket` | `(event: {event: string; time: number})` | Fired on socket lifecycle events (`channelJoin`, `joinChannelError`, `unmonitor`, `onError`). | `on_socket: (e) => console.log(e)` |
| `on_autocapture` | `(payload: Object, type: string)` | Fired when an auto-captured event occurs (`click`, `submit`, `change`). | `on_autocapture: (el,t) => console.log({el,t})` |
| `on_render` | `(event: RenderEvent)` | Fired when Userpilot content renders. Includes its type and ID. | `on_render: ({type, id}) => console.log(type, id)` |

***

## Example: Handling Callbacks

```js theme={null}
callbacks: {
  on_url_change: (page) => {
    console.log(page); // {hostname: HOSTNAME, url: URL}
  },
  on_socket: (event) => {
    console.log(event); // {event: channelJoin | joinChannelError | unmonitor | onError, time: TIMESTAMP}
  },
  on_autocapture: (payload, type) => {
    console.log(element_chain, type); // {element: Object, type: click | submit | change}
  },
  on_render: ({ type, id, element, height }) => {
    console.log(type, id, element, height);
  }
}
```

```js theme={null}
// Masking page view url call
callbacks: {
  on_url_change: (page = {url, hostname}) => {
    page.hostname = "https://example.com"
    page.url = `${page.hostname}${new URL(page.url).pathname}`
  }
}
```

```js theme={null}
// logging element

const maskAutoCaptureURLs = (payload, key) => {
  console.log(payload[key]);
  
  return payload[key];
}

....

callbacks: {
  on_autocapture: (payload, type) => {
    const maskedKeys = ['href', 'elements_chain'];
    
    maskedKeys.forEach(key => {
      payload[key] = maskAutoCaptureURLs(payload, key)
    })
  }
}
```

### Render callback

Register `on_render` in `window.userpilotSettings.callbacks` **before the SDK script loads**. It receives one event object for banners, cards, flow steps, spotlights, checklists, surveys, NPS, and the resource center. Every event includes `type`, `id`, and the mounted host-page `element`. Flow events use the step ID as `id` and also include `flow_id`. Survey events can run for each displayed module and include `module_id`.

| Event field | Description |
| - | - |
| `type` | Content type: `banner`, `card`, `modal`, `slideout`, `tooltip`, `driven`, `spotlight`, `checklist`, `survey`, `nps`, or `resource_center`. |
| `id` | Content ID. NPS and resource center events use their configured ID when present, otherwise their content type. |
| `element` | Mounted element on the host page, such as the banner or card wrapper or a content iframe. |
| `height` | Element height in CSS pixels at render time; supplied for banners and cards. |
| `flow_id`, `embed_id`, `module_id`, `spotlight_type`, `embedded` | Additional context supplied for the corresponding content type. |

```js theme={null}
window.userpilotSettings = window.userpilotSettings || {};
window.userpilotSettings.callbacks = {
  ...window.userpilotSettings.callbacks,
  on_render({ type, id, element, height }) {
    console.log('Rendered Userpilot content:', type, id);
    if (type === 'banner' || type === 'card') {
      console.log('Height before display:', height);
      // Observe `element` if the host layout must track later size changes.
    }
  },
};
```

Banner and card callbacks run after the element is mounted and sized, immediately before its iframe becomes visible. `height` is the element's `getBoundingClientRect().height` and excludes outside margins. The height can change later, so use a `ResizeObserver` on `element` if the host layout must keep tracking it. `on_render` is separate from `userpilot.on('render', ...)`, which listens to flow events.

***

## Type definitions

```ts theme={null}
type CallbacksConfig = {
  on_url_change?: (page: { hostname: string; url: string }) => void;
  on_socket?: (event: { event: "channelJoin" | "joinChannelError" | "unmonitor" | "onError"; time: number }) => void;
  on_autocapture?: (element: Element, type: "click" | "submit" | "change") => void;
  on_render?: (event: RenderEvent) => void;
};

type RenderEvent = {
  type: "banner" | "card" | "modal" | "slideout" | "tooltip" | "driven" | "spotlight" | "checklist" | "survey" | "nps" | "resource_center";
  id: string | number;
  element: Element;
  height?: number;
  flow_id?: string | number;
  embed_id?: string | number;
  module_id?: string | number;
  spotlight_type?: string;
  embedded?: boolean;
};
```

***

## Troubleshooting

* **“My callback doesn’t fire”** – Ensure the SDK is initialized with `callbacks` defined before events occur.
* **“Socket events not showing”** – Verify network connections and channel join logic.
* **“Autocapture callback too noisy”** – Combine with `excluded_elements` or `masked_elements` to reduce unwanted triggers.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.