> For the complete documentation index, see [llms.txt](https://api.beacon.li/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://api.beacon.li/listening-to-events-from-beacon-bar/adding-event-listeners-in-beacon.md).

# Adding event listeners in Beacon

#### Listen to different analytical events emitted by Beacon Bar

Beacon Bar allows you to track and emit various analytical events that provide insights into user behavior and interactions with your application. You can listen to these events by adding event listeners, enabling you to gather valuable data and respond to specific user actions.

#### Adding an Event Listener

To listen to analytical events emitted by Beacon Bar, you can use the `Beacon.addEventListener` method. This method takes one parameter, which is a callback function with the following signature:

```
(eventName, attributes, userId) => {
    // Your event handling logic here
}
```

* `eventName` (string): The name of the event.
* `attributes` (object): An object containing attributes passed with the event.
* `userId` (string, default: `beacon-anonymous`): The unique identifier passed during `loadUser`.

Here's how to add an event listener:

```
import { Beacon } from '@beacon.li/bar';
Beacon.addEventListener((eventName, attributes, userId) => {
    // Your event handling logic here
});
```

In the example above, we've added an event listener that will execute the specified event handling logic whenever an analytical event is emitted by Beacon Bar.

#### Use Cases

Listening to analytical events can be useful for various purposes, such as:

* Gathering data on user interactions and behaviors.
* Tracking specific events for analytics and reporting.
* Responding to user actions or behaviors within your app.

By adding event listeners with `@beacon.li/bar`, you can gain insights into user engagement and use this data to improve your application's user experience.

#### Important Considerations

When handling events, ensure that you have a clear understanding of the events you want to listen to and the actions you want to take based on those events. Analytical events can provide valuable information for optimizing your app's functionality and user satisfaction.

By following these steps, you can listen to analytical events emitted by Beacon Bar and respond to user interactions and behaviors effectively.


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://api.beacon.li/listening-to-events-from-beacon-bar/adding-event-listeners-in-beacon.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
