Controlling the widget from the page
Open the chat from your own button, show an invitation, listen to events and see conversations in your analytics.
On this page
Commands from the site #
The widget puts a window.WidgetChat object on the page. Use it to open and close the window, show an invitation and subscribe to events. Do not rely on our markup ids: they are internal and change.
| Command | What it does |
|---|---|
| WidgetChat.open() | Open the chat window |
| WidgetChat.open({ message: 'text' }) | Open it and put a prepared question into the input |
| WidgetChat.close() | Close the window |
| WidgetChat.toggle() | Open or close, depending on the current state |
| WidgetChat.isOpen() | true when the window is open |
| WidgetChat.unreadCount() | How many replies the visitor has not read |
| WidgetChat.showInvite('text') | Show the invitation right now |
| WidgetChat.hideInvite() | Remove the invitation |
| WidgetChat.attention() | Make the button call for attention; attention(false) stops it |
| WidgetChat.on('open', fn) | Subscribe to an event; off removes the subscription |
| WidgetChat.ready(fn) | Run once the widget is ready |
<button id="ask">Message us</button>
<script>
document.getElementById('ask').addEventListener('click', function () {
WidgetChat.open({ message: 'A question about delivery' })
})
</script>The button is clicked before the widget loads #
The widget file loads asynchronously, so for the first seconds the object is not on the page yet. If your button can be clicked during that time, declare a queue stub before the widget snippet: the widget picks it up at start and runs the collected commands.
<script>
window.WidgetChat = window.WidgetChat || { q: [] };
['open', 'close', 'toggle', 'showInvite', 'hideInvite', 'attention'].forEach(function (name) {
WidgetChat[name] = WidgetChat[name] || function () {
WidgetChat.q.push([name, [].slice.call(arguments)]);
};
});
</script>A queued command runs once, when the window is built. There is no need to repeat it.
Events #
Every event arrives two ways: in a WidgetChat.on('name', fn) handler and on window as widgetchat:name. The second one is handy when your code runs before the widget or lives in another script.
| Event | When it fires | Payload |
|---|---|---|
| ready | The widget is built and accepts commands | version |
| open | The window was opened, by a person or by your command | — |
| close | The window was closed | — |
| invite | The invitation was shown | text |
| sent | The visitor's message was accepted by the server | first — whether it is the first one |
| message | Support replied | text, from |
| unread | The number of unread replies changed | count |
| goal | The widget sent a goal to the site's analytics | goal, params |
window.addEventListener('widgetchat:sent', function (event) {
if (event.detail.first) console.log('first conversation from this page')
})Invitation to a conversation #
After the given delay a bubble with your text appears next to the button, and the button starts calling for attention. Clicking the bubble opens the chat, the cross dismisses it.
By default the widget stays silent. The invitation is switched on in the dashboard, on the “Appearance” step, together with its delay and text. Attributes on the script tag win over the dashboard setting: use them to invite earlier and in different words on a particular page, a checkout for example.
| Attribute | Value |
|---|---|
| data-invite-delay | Seconds before the invitation shows. Without the attribute the widget stays silent; 1 to 300 is allowed |
| data-invite-text | The invitation text. Without it the widget speaks its own words in the visitor's language |
<script async
src="https://api.widgetchat.ru/widget/bozex-support-widget.js"
data-widget-id="wc_9qA…Lk"
data-invite-delay="15"
data-invite-text="We can help with stock and delivery dates"
></script>The invitation shows once per tab and never on top of an open chat or a fresh reply. A dismissal is remembered for the visit; until the visitor opens the chat the widget writes nothing to the browser.
Goals for analytics #
The widget sends goals to the counters already present on the page: Yandex.Metrica via reachGoal, Google Tag Manager via dataLayer and gtag. The counter number is not needed — Metrica knows its own counters. In the Metrica dashboard create the goal as a JavaScript event with the same identifier.
| Goal | When it fires |
|---|---|
| widgetchat_open | The visitor opened the chat window |
| widgetchat_first_message | The visitor's first message — this is the conversation itself |
| widgetchat_message | Every message from the visitor |
| widgetchat_reply | Support replied |
| widgetchat_invite_shown | The invitation was shown |
| widgetchat_invite_click | The invitation was accepted |
| widgetchat_close | The window was closed |
<script async
src="https://api.widgetchat.ru/widget/bozex-support-widget.js"
data-widget-id="wc_9qA…Lk"
data-metrika-id="12345678"
data-goals="off"
></script>data-metrika-id is only needed when the page carries several counters and the goals must go to a particular one. data-goals="off" turns the sending off completely.