Skip to content
QwryAI
Documentation
Pages

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:

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();
});

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 <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.

JavaScript
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 off when a view unmounts.
  • Don't send private content to third parties without your visitors' consent.

Troubleshooting

Events Not Firing
  • Wait for the script: window.chatbot is 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, unreadCountChanged and error.
  • Check the console: errors in your handlers are logged there.
Memory Leaks
  • Call off with the same function reference you passed to on.
  • 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 on and off.

Next Steps

Explore the documentation