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

# Add Event Subscriber

> Subscribes your application to real-time AdoptAI SDK events, allowing you to monitor and respond to sidebar interactions as they happen.

The addEventSubscriber method lets you listen to real-time events triggered inside the AdoptAI SDK sidebar.

By subscribing to these events, your app can respond to user actions, analytics events, or sidebar updates — all without modifying the SDK itself.

This is useful if you want to:

* Track how users interact with the sidebar.
* Log events for analytics or monitoring.
* Trigger workflows in your own app when specific Adopt events occur.

### How to Use

We introduced an event subscription system that allows external applications to **subscribe to AdoptAI-specific events** emitted on the global `window` object.\
This enables you to monitor and handle sidebar interactions (like message loads, tool executions, or user actions) in real time.

Paste this code block **after the SDK has been booted** in your application.

```json theme={null}
//Define your handler (mandatory handler)
const myHandler = (eventName, eventData) => {
  // Process the events as required
};
//Subscribe to events
const unsubPromise = window.AdoptAI.addEventSubscriber(myHandler);
//To unSubscribe 
```

### Explanation

#### The Event Handler

```json theme={null}
const myHandler = (eventName, eventData) => {
  // Process the events as required
};
```

* **eventName** — a string representing the event type (e.g. `adopt_copilot:message_loaded`).
* **eventData** — an object containing the event payload and contextual details.

Your handler function is called automatically each time the SDK emits a new event.

#### Subscribing to Events

```json theme={null}
window.AdoptAI.addEventSubscriber(myHandler);
```

When you subscribe, the SDK invokes your handler for every emitted event.\
You can store or forward these events (for example, to analytics tools like Segment or Datadog), or trigger custom actions inside your own app.

To clean up, call the function returned by the promise:

```json theme={null}
unsubPromise.then((unsub) => unsub());
```

This ensures your handler is properly unsubscribed when it’s no longer needed.

### Available Events

All event names follow the `adopt_copilot:<event>` convention.

| **Event Name**                        | **Description**                                                 |
| :------------------------------------ | :-------------------------------------------------------------- |
| `adopt_copilot:newConversation_click` | Triggered when a user starts a new conversation in the sidebar. |
| `adopt_copilot:minimize_click`        | Fired when the sidebar is minimized.                            |
| `adopt_copilot:expand_click`          | Fired when the sidebar is expanded.                             |
| `adopt_copilot:history_open`          | Triggered when a user opens conversation history.               |
| `adopt_copilot:history_close`         | Triggered when a user closes conversation history.              |
| `adopt_copilot:action_initiated`      | Fired when a copilot action is initiated.                       |
| `adopt_copilot:action_executed`       | Fired when a copilot action completes successfully.             |
| `adopt_copilot:action_failed`         | Fired when a copilot action fails.                              |
| `adopt_copilot:stop_execution`        | Triggered when a user manually stops a running action.          |
