Skip to Content
BetterFeedback Docs

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");
CommandDoes
open, close, toggleThe panel, in floating mode
configApplies an object of options by JS key, merged over the attributes. theme, copy and context merge key by key
on, offAdds or removes a listener for an event from the table below. Works before any embed is mounted, because the events bubble to document
mountCreates 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
unmountRemoves 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.

EventWhendetail
readyThe conversation page is loaded and listeningsurveyId, title
startedThe visitor pressed Start and the connection is upsurveyId
completedThe conversation finishedsurveyId, redirectUrl when the survey redirects on completion
endedThe session ended without completingsurveyId, reason: disconnected or other-tab
errorSomething your page may want to react tosurveyId, code: mic-denied, no-agent or unavailable
resizeInline mode with auto height: the content height changedsurveyId, 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.

On this page