- pass values the page already knows, for example from your app’s state;
- open or close the chat from your own buttons;
- show the conversation inside your own element instead of the floating bubble;
- react when the agent answers — for example to log it, or to show the answer in your own interface.
Add the widget
In the widget channel, click Install Widget and open the JavaScript tab. It looks like this:CogniAgent(...) safe to call before embed.js has loaded: calls are queued and run in order once it loads. After loading, CogniAgent.createWidget(options) also works and returns a handle to control the widget.
Options
Control the widget
createWidget. Pass your values in createWidget; setParameters only works in the same moment you create the widget, and afterwards does nothing (the browser console says so). Use restart to begin a new conversation with new values. A widget opened from a widget link can’t be restarted — its values belong to the link.
A widget shown in your own element (target) is always open, so open and close don’t apply to it.
You can add several widgets to one page — each keeps its own conversation (two widgets for the same agent and channel share one). Floating widgets all sit in the same corner, so show at most one floating widget and put the others in your own elements.
Events
answer_generated fires once per answer, with the complete text, after the agent has finished it — never for partial text while it’s still being written. For spoken replies, text is the final text of the reply. Use it when your page needs the agent’s answer, for example to show it in your own chat interface or to record it in your system.
An error in your listener doesn’t break the widget; it’s reported in the browser console.
Values from the page address
On the hosted page (the test page and widget links), values can be put in the address:#vars=followed by the values as JSON, base64url-encoded:#vars=eyJvcmRlcl9pZCI6IkEtMTAwMSJ9- or one value at a time:
#var_order_id=A-1001
var_ wins for its name. Values after # stay in the browser — they’re not sent to any server when the page loads.
On your own pages, the widget only reads the address when you say so — with the SDK option parametersFromUrl: ['order_id'], or with data-parameters-from-url="order_id" on the regular snippet ("*" for all of the agent’s parameters). Only the names you list are read. Values written in your code win over values from the address.
Values from the address are shown as Page URL in the conversation’s Parameters block and, like all values from the page, can be changed by visitors.