Custom JavaScript for web chat
Run your own script with the chat widget to react to chat events and attach data to conversations.
Each web chat configuration can carry its own JavaScript snippet that runs on your pages with the widget. Use it to connect the chat to the rest of your site: send chat events to analytics and tag managers, flag handoffs in your CRM, or attach cart state and account details that the AI agent and your agents should know about the visitor.
To add one, open Settings -> Advanced -> Custom JavaScript, pick the configuration, turn it on and paste the code in. A business with several configurations, for different deployments, runs a different snippet on each. Copilot and MCP clients can set it with the enableCustomJs and customJs fields of update_web_chat_config. Because the snippet is code, changing it needs the Write and run automations and code permission (automations:write).
How it runs
The snippet runs once per page load, as soon as the widget has loaded its configuration, in the page's own scope: the same place your other site scripts run. It is served with the public widget configuration, so treat it as public and never put secrets or API keys in it.
A snippet that throws is caught and logged to the console as
[octocom] custom script failed, so it cannot break the widget or your page. In
DevTools the snippet appears as octocom-custom-script.js, so you can set
breakpoints in it.
Widget events
Inside the snippet, window.octocom is an event emitter:
octocom.on("message:sent", function (event) {
window.dataLayer.push({ event: "chat_message_sent", text: event.text });
});
octocom.on("handoff", function (event) {
myCrm.flagConversation(event.publicId);
});| Event | Fires when | Detail |
|---|---|---|
widget:ready | The widget has loaded its configuration and the API is usable. | configId, url |
widget:open | The chat panel opened. | instanceId |
widget:close | The chat panel closed. | instanceId |
conversation:started | A conversation became active and its public id is known (a resumed one included). | conversationId, publicId |
message:sent | The visitor sent a message. | text, sender, conversationId |
message:received | A bot or agent message was added to the transcript. | text, sender, conversationId |
handoff | The conversation was handed off to a human agent. | conversationId, publicId |
page:changed | The page URL changed under the widget, including SPA navigation. | url, previousUrl |
octocom.on returns an unsubscribe function, and octocom.off(name, handler)
does the same job. widget:ready, conversation:started and handoff are
replayed to handlers that subscribe after they fired, so a late subscription
never misses them.
Every event is also dispatched on document as octocom:<name>, if you would
rather listen from your own scripts:
document.addEventListener("octocom:message:received", function (event) {
console.log(event.detail.text);
});Attaching data to the conversation
octocom.setCustomData writes key-value pairs onto the visitor's conversation.
Each pair becomes chat-widget:<key> conversation metadata, the same place
chat custom data query parameters land, so
it shows up in the dashboard, in custom system prompt sections, in Python actions, and in analytics. The keys
email, name and phone additionally identify the customer.
octocom.on("conversation:started", function (event) {
octocom.setCustomData({
plan: window.currentUser.plan,
cart_value: window.cart.total,
last_order_id: window.currentUser.lastOrderId,
});
});If a conversation is already open, the pairs are pushed to it immediately instead of waiting for the visitor's next message. If none is open yet, they are kept and attached when the visitor starts one.
Timing matters if the AI agent itself has to see the data. conversation:started
fires once the visitor has already sent their first message, so the AI agent's first
turn may be underway before the push lands. Set anything the AI agent needs from a
widget:ready handler, or any time before the visitor writes, and use the
conversation:started push for data your agents, Python actions and analytics
read afterwards.
Up to 25 pairs per call, keys up to 64 characters, values up to 2000. Setting the same key again overwrites it.
Values are coerced to strings, objects are JSON-encoded, and empty keys are
dropped. octocom.getCustomData() returns everything currently stored for the
visitor, whether it came from a snippet, a query parameter, or localStorage.
Nothing is written to the visitor's device before they open the chat, matching the widget's own storage behavior. See Browser storage and consent.
Opening, showing and nudging from code
These controls work from any script on your site, with or without custom JavaScript. Full details and examples are in Control the chat widget with code:
- Open the chat, with or without a first message: dispatch
openChatEvent. See Opening the chat from your own code. - Reveal a widget that is hidden by default: dispatch
showWidgetEvent. The snippet still runs while the widget is hidden, so it is the natural place for your show-or-hide check. See Showing a hidden widget. - Show a manual attention grabber: inside the snippet, call
octocom.showAttentionGrabber("Spring sale")with the grabber's id or name. From other scripts, dispatchshowAttentionGrabberEvent. See Showing an attention grabber.