SectionFor developers
Control the chat from your site’s code: JavaScript API
In this article
The chat code on your site gives JavaScript access to the widget: you can open the chat from your own button, pass the data of a customer who is signed in to your site, and react to events — for example, send an add-to-cart from the chat to your analytics.
You need access to your site’s template. The chat code must be on the page — see “Install the chat on your website”.
How to subscribe to events
Widget events are dispatched on document via jQuery. If your site has no jQuery, the chat loads it itself, so subscribe once jQuery is available:
(function wait() {
if (!window.jQuery) return setTimeout(wait, 300);
jQuery(document).on('SBChatOpen', function () {
console.log('The customer opened the chat');
});
jQuery(document).on('SBCartAdded', function (e, data) {
console.log('Added to cart from the chat', data.product_id, data.quantity);
});
})();Main events
- SBReady
- The chat has loaded.
- SBChatOpen, SBChatClose
- The customer opened or closed the chat window.
- SBNewMessagesReceived
- New messages arrived. Data: messages, conversation_id.
- SBConversationOpen
- A conversation was opened.
- SBWelcomeMessage
- The welcome message was sent.
- SBFollowUp
- The chat asked for the customer’s contact details.
- SBNewEmailAddress
- The customer left an email. Data: name, email.
- SBRegistrationForm
- The customer filled in the registration form.
- SBCartAdded
- A product was added to the cart from the chat. Data: platform, product_id, quantity.
- SBOrderCreated
- The store created an order from the chat. The data contains order_id.
- SBPopup
- A popup message was shown.
Open the chat from your own button
document.querySelector('#help-button').addEventListener('click', function () {
if (window.SBChat) SBChat.open();
});SBChat.open(false) closes the chat window.
Pass customer data
If the customer is signed in to your site, pass their name and contacts to the chat so a manager knows who they’re talking to. Declare the SB_DEFAULT_USER variable before the chat code:
<script>
var SB_DEFAULT_USER = {
first_name: 'Emma',
last_name: 'Smith',
email: '[email protected]',
extra: { phone: ['+380501234567', 'Phone'] }
};
</script>
<script id="chat-init" src="https://app.ayweb.chat/account/js/init.js?id=CHAT_ID"></script>In the same way, before the chat code you can set SB_DEFAULT_DEPARTMENT and SB_DEFAULT_AGENT — the department and agent IDs for new conversations — and SB_DEFAULT_TAGS — tags.
If it doesn’t work
- The event handler doesn’t fire
- Subscribing with addEventListener doesn’t see these events. Use jQuery(document).on(...).
- window.SBChat is undefined
- The chat is still loading. Call SBChat after the SBReady event.
- Customer data isn’t passed
- SB_DEFAULT_USER must exist on the page before the chat loads. Declare it above the chat code.
Don’t pass data in SB_DEFAULT_USER that the customer hasn’t given you. Data from this variable goes into the customer’s profile in “Users”.