BookbagBookbag

Event listeners

Listen for real-time chat events in the browser with window.bookbag.addEventListener — user messages, assistant replies, and tool calls — and use webhooks for reliable server-side events.

View as Markdown

Once the embed script is on your page it exposes a global, window.bookbag, that emits real-time chat events. Register listeners to drive analytics, route leads, sync your UI, or react when the agent calls a tool. For reliable server-side awareness (a lead was captured even if the tab closed), use webhooks.

Registering listeners

window.bookbag.addEventListener(eventName, callback);
window.bookbag.removeEventListener(eventName, callback);

Each callback receives an event object of the shape { type, data }.

Store the function reference

Pass the same function to addEventListener and removeEventListener. Inline anonymous functions can be added but never removed — keep a reference if you need to clean up on navigation.

Available events

EventFires whendata
user-messageThe visitor sends a message.{ content }
assistant-messageThe agent finishes replying.{ content, messageId }
tool-callThe agent calls a tool / action.{ id, name, args, type }
tool-resultA tool returns a result.{ toolCallId, name, result, type }

Example

function onUserMessage(e) {
  console.log("User said:", e.data.content);
  window.analytics?.track("chat_user_message", { text: e.data.content });
}
function onAssistantMessage(e) {
  console.log("Agent replied:", e.data.content);
}

window.bookbag.addEventListener("user-message", onUserMessage);
window.bookbag.addEventListener("assistant-message", onAssistantMessage);

// Later (e.g. on SPA route change):
window.bookbag.removeEventListener("user-message", onUserMessage);

Reacting to tool calls

tool-call and tool-result let you observe what the agent is doing. On Shopify, the storefront cart bridge uses tool-call to add items to the live cart — you can hook the same event to fire analytics when the agent recommends or adds a product.

Controlling the widget

The same global opens, closes, and resets the widget — see JavaScript embed for window.bookbag('open'), ('close'), ('toggle'), and ('resetChat').

Server: webhooks for reliable events

Browser events live only as long as the tab is open. For anything you must act on — a lead was captured, a conversation ended — use webhooks: server-to-server, signed, and delivered regardless of the browser.

You want to know when...Listen for
A visitor leaves their email/phonelead.created webhook
A conversation endsconversation.ended webhook
An action/tool runsaction.run webhook