Event Listeners

React to chat events in real-time with JavaScript callbacks.

Overview

The embed widget dispatches custom events that you can listen to from your application. Use these to track user interactions, trigger analytics, or integrate with other tools.

Available Events

EventFired When
agent:readyWidget has loaded and is ready
agent:openChat widget is opened
agent:closeChat widget is closed
agent:message:sentUser sends a message
agent:message:receivedAgent responds with a message
agent:conversation:startA new conversation begins

Usage

window.addEventListener('agent:message:sent', (event) => {
  console.log('User sent:', event.detail.message);
});

window.addEventListener('agent:message:received', (event) => {
  console.log('Agent replied:', event.detail.message);
});

window.addEventListener('agent:ready', () => {
  console.log('Chat widget is ready');
});

Event Detail

Each event's detail property contains relevant data:

// agent:message:sent
interface MessageSentDetail {
  message: string;
  conversation_id: string;
  timestamp: string;
}

// agent:message:received
interface MessageReceivedDetail {
  message: string;
  conversation_id: string;
  timestamp: string;
  sources?: string[];
}

Programmatic Control

You can also control the widget programmatically:

// Open the chat widget
window.AgentWidget.open();

// Close the chat widget
window.AgentWidget.close();

// Send a message programmatically
window.AgentWidget.sendMessage('Hello!');

On this page