Website Integration
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?
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
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
Available Events
Listen for these events with window.chatbot.on(eventName, callback):
messageSent
The visitor's message was accepted. The callback gets the message:
{
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:
{
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
}window.chatbot.on("messageSent", (message) => {
analytics.track("Chat message sent", { conversationId: message.conversationId });
});
window.chatbot.on("messageReceived", (message) => {
if (message.content.includes("refund")) showRefundPolicyLink();
});tool-call
The agent asked your page to run a client-side custom Action.
{
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.
{
data: {
toolCallId: "call_8f2c1d",
name: "get_order_status",
result: { status: "success", data: { status: "Shipped" } },
type: "client"
}
}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.
window.chatbot.on("contactIdentified", (contact) => {
console.log("Signed in to the chat as", contact.email);
});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.
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
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:
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:
const handleMessage = (message) => {
console.log("Visitor said:", message.content);
};
window.chatbot.on("messageSent", handleMessage);
// Later
window.chatbot.off("messageSent", handleMessage);// 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 <iframe>, window.chatbot doesn't exist. The iframe posts messages to your page instead: MESSAGE_SENT when the visitor sends a message and NEW_MESSAGE when a reply arrives.
const QWRYAI_ORIGIN = "https://www.qwryai.com";
window.addEventListener("message", (event) => {
if (event.origin !== QWRYAI_ORIGIN) return;
if (event.data?.type === "MESSAGE_SENT") console.log("Visitor said:", event.data.message.content);
if (event.data?.type === "NEW_MESSAGE") console.log("Agent replied:", event.data.message.content);
});See the Iframe Developer Guide for every message the iframe sends and accepts.
Event Management Best Practices
- Keep handlers fast: do heavy work asynchronously so the chat stays responsive.
- Catch your own errors: an error in one handler is logged to the console and doesn't stop the others, but your logic won't finish.
- Remove what you add: in single-page apps, call
offwhen a view unmounts. - Don't send private content to third parties without your visitors' consent.
Troubleshooting
Events Not Firing
- Wait for the script:
window.chatbotis only there once the widget script has loaded. Add listeners after that. - Check event names: valid names are
ready,open,close,messageSent,messageReceived,tool-call,tool-result,contactIdentified,contactUpdated,logout,unreadCountChangedanderror. - Check the console: errors in your handlers are logged there.
Memory Leaks
- Call
offwith the same function reference you passed toon. - In single-page apps, remove listeners when routes change, not only on full page loads.
Handler Function Issues
- Don't pass inline functions if you need to remove them later: each inline function is a new reference.
- Store your handlers in variables and reuse them for
onandoff.