JavaScript API
The betterfeedback() command vocabulary for opening, configuring and mounting the embed, the events it dispatches on the element and document, the queue stub for calls made before the script loads, and the message shape a plain iframe receives.
widget.js installs a global betterfeedback() function that takes a command name. Events are
CustomEvents named betterfeedback:<name> that bubble from the element to document.
Commands
betterfeedback("open");
betterfeedback("close");
betterfeedback("toggle");
betterfeedback("config", { accent: "#e11d48", theme: { surface: "transparent" } });
betterfeedback("on", "completed", (e) => dataLayer.push({ event: "intake_completed", ...e.detail }));
betterfeedback("off", "completed", handler);
betterfeedback("mount", { survey: "YOUR_SURVEY_ID", target: "#contact-form" });
betterfeedback("unmount");| Command | Does |
|---|---|
open, close, toggle | The panel, in floating mode |
config | Applies an object of options by JS key, merged over the attributes. theme, copy and context merge key by key |
on, off | Adds or removes a listener for an event from the table below. Works before any embed is mounted, because the events bubble to document |
mount | Creates the element. Takes any option by JS key and reads the rest from the script tag's data-* attributes. Does nothing when an element already exists on the page |
unmount | Removes every widget element from the page |
The global function addresses the first embed on the page. A second element answers the same
commands through its own run(name, arg) method.
A command other than on and off issued before an element exists is buffered and replayed
when one connects.
Calling it before the script has loaded
Install the queue stub above the script tag. Calls are stored on window.betterfeedback.q and
replayed in order when widget.js takes over.
<script>
window.betterfeedback = function () {
(window.betterfeedback.q = window.betterfeedback.q || []).push(arguments);
};
</script>
<script async src="https://ask.betterfeedback.ai/widget.js" data-survey="YOUR_SURVEY_ID"></script>To keep mounting in your own hands, add data-render-on-load="false" to the script tag and call
betterfeedback("mount", …) when the page is ready for it.
Events
The element dispatches each event as betterfeedback:<name>, bubbling and composed, with the
payload in detail. betterfeedback("on", name, fn) listens on document; a direct
document.addEventListener("betterfeedback:completed", fn) is the same thing.
| Event | When | detail |
|---|---|---|
ready | The conversation page is loaded and listening | surveyId, title |
started | The visitor pressed Start and the connection is up | surveyId |
completed | The conversation finished | surveyId, redirectUrl when the survey redirects on completion |
ended | The session ended without completing | surveyId, reason: disconnected or other-tab |
error | Something your page may want to react to | surveyId, code: mic-denied, no-agent or unavailable |
resize | Inline mode with auto height: the content height changed | surveyId, height in pixels |
Messages in a plain iframe
Without the script, the conversation page posts the same events to the window that frames it,
as { "type": "bf:<name>", "v": 1, "surveyId": "…", … } with the same fields, and only when the
URL carries ?embed=1. Check event.origin against https://ask.betterfeedback.ai before
reading them. Plain iframe shows a listener.
Behaviour
What the floating and inline modes do, how auto height sizes the frame, the launcher and trigger options, open on load, voice off, keyboard handling and what happens when a conversation completes.
Respondent context
Hand the conversation the visitor's email and what your page already knows about them, so the agent confirms those details in one sentence instead of asking, with the limits and the trust rules that apply.