# Event Listeners > React to what happens in the chat as it happens: messages, the chat opening and closing, sign-ins and client-side Actions Event listeners let your page react to activity in the Chat bubble. When a visitor sends a message, the agent replies, the chat opens or a client-side Action runs, your callback gets the details, so you can update your page, send analytics or sync with your own systems. ## Why Use Event Listeners? > [accordion] Custom User Experiences > > * Show related content when the visitor asks about a topic > * Change your page when the chat opens or closes > * Show an unread count on your own button > * Tie the chat into the rest of your page > [accordion] Third-party Integrations > > * Send chat activity to your analytics or CRM > * Record when a visitor is identified > * Log client-side Action calls and their results > * Report widget errors to your monitoring ### Prerequisites > [!NOTE] > > A website with the QwryAI Chat bubble installed and working. New to QwryAI? Start with [Your first AI agent](/docs/user-guides/quick-start/your-first-agent). ## Available Events Listen for these events with `window.chatbot.on(eventName, callback)`: > [tab] Message Events > > **messageSent** > > The visitor's message was accepted. The callback gets the message: > > ```javascript > { > conversationId: "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3", > messageId: "0c4f6a2e-8d13-4b7a-b0e9-1a2c3d4e5f60", > sender: "user", > content: "Do you ship to Canada?" > } > ``` > > **messageReceived** > > A reply arrived from the agent, or from a teammate who took over. The callback gets the message, including: > > ```javascript > { > id: "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10", > conversationId: "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3", > content: "Yes, we ship to Canada. Orders arrive in 5 to 8 business days.", > sender: "ai", // "ai", "agent" or "admin" > senderName: "Agent" // set for replies from a teammate > } > ``` > > ```javascript > window.chatbot.on("messageSent", (message) => { > analytics.track("Chat message sent", { conversationId: message.conversationId }); > }); > > window.chatbot.on("messageReceived", (message) => { > if (message.content.includes("refund")) showRefundPolicyLink(); > }); > ``` > [tab] Action Events > > **tool-call** > > The agent asked your page to run a [client-side custom Action](/docs/developer-guides/website-integration/client-side-custom-actions). > > ```javascript > { > data: { > id: "call_8f2c1d", // the call's id > name: "get_order_status", // the Action's name > args: { orderNumber: "SL-20417" }, > type: "client" > } > } > ``` > > **tool-result** > > Your Action's handler finished. `result` is what your handler returned. > > ```javascript > { > data: { > toolCallId: "call_8f2c1d", > name: "get_order_status", > result: { status: "success", data: { status: "Shipped" } }, > type: "client" > } > } > ``` > > > [!NOTE] > > > > These events fire only for Actions that run in the browser. Actions that call an API on QwryAI's servers don't send them. > [tab] Contact Events > > **contactIdentified** > > The visitor was signed in with `identify` or `setUser`. The callback gets their contact, with fields such as `name`, `email`, `phone` and `externalUserId`. > > **contactUpdated** > > The visitor's contact changed, for example after they shared their email in the chat. The callback gets the updated contact. > > **logout** > > `logout`, `reset` or `shutdown` was called. No data. > > ```javascript > window.chatbot.on("contactIdentified", (contact) => { > console.log("Signed in to the chat as", contact.email); > }); > ``` > [tab] Widget Events > > **ready** > > The chat has loaded and is ready. No data. > > **open** and **close** > > The chat window opened or closed. No data. > > **unreadCountChanged** > > The number of unread replies changed. The callback gets the new count. > > **error** > > Something failed, such as a refused sign-in. The callback gets the error details. > > ```javascript > window.chatbot.on("open", () => document.body.classList.add("chat-open")); > window.chatbot.on("close", () => document.body.classList.remove("chat-open")); > window.chatbot.on("unreadCountChanged", (count) => updateBadge(count)); > ``` ## Basic Event Listener Usage ### Adding Event Listeners ```javascript window.chatbot.on(eventName, callback); ``` **eventName** `string` *required* One of the event names above. Names are case-sensitive. **callback** `function` *required* Called each time the event fires, with the event's data as its argument. `window.chatbot` exists once the widget script has loaded, a moment after your page's `load` event. Wait for it before adding listeners: ```javascript function onQwryaiReady(callback) { if (window.chatbot) return callback(window.chatbot); setTimeout(() => onQwryaiReady(callback), 200); } onQwryaiReady((chatbot) => { chatbot.on("messageReceived", (message) => console.log("Agent replied:", message.content)); }); ``` ### Removing Event Listeners Remove listeners you no longer need with `off`, passing the same function you gave to `on`: ```javascript const handleMessage = (message) => { console.log("Visitor said:", message.content); }; window.chatbot.on("messageSent", handleMessage); // Later window.chatbot.off("messageSent", handleMessage); ``` ```javascript // In React, clean up listeners when the component unmounts useEffect(() => { const handleMessage = (message) => setLastMessage(message.content); window.chatbot?.on("messageSent", handleMessage); return () => window.chatbot?.off("messageSent", handleMessage); }, []); ``` ## Listening Without the Embed Script If you embed the agent as a plain `