- Configure chat widget settings
- Control the widget’s behavior
- Clear chat cookies
- Listen for and respond to widget events
Initializing
The API is housed in thewindow.HubSpotConversations object, which provides access to all available methods. The object is created by the HubSpot tracking code, but may not be available immediately on page load. To defer accessing the API until it’s initialized, you can use the window.hsConversationsOnReady helper.
window.hsConversationsOnReady is an optional field you can define on the window object which enables you to specify code to be executed as soon as the widget becomes available. This field takes an array functions to be executed once the API has been initialized.
Configure conversations settings
hsConversationsSettings
This optional object enables you to provide some configuration options to the widget before it initializes.
| Field | Type | Default | Description |
|---|---|---|---|
loadImmediately | Boolean | true | Whether the widget should implicitly load or wait until the widget.load method is called. |
inlineEmbedSelector | String | "" | Specify a selector (#some-id) to embed the chat widget in a specific location on the page. Widget will be embedded inline within that DOM node and will remain open until it is removed with widget.remove. Learn more about styling embedded chat widgets. |
enableWidgetCookieBanner | Enumeration | false | Control behavior of the cookie banner for all chat widgets on the page. Options include:
|
disableAttachment | Boolean | false | Whether to hide the upload attachment button in the chat widget. |
disableInitialInputFocus | Boolean | false | Whether to automatically prevent focusing on the widget’s input field after an inline embedded widget is initially loaded. |
avoidInlineStyles | Boolean | false | When set to true, injects a link tag with externally hosted CSS instead of a direct dynamic insertion of a style tag. |
hideNewThreadLink | Boolean | false | When set to true, prevent the To start a new chat, click here link from appearing below the Your chat has ended text when a chat has ended. |
Inline embed styling
When the widget is embedded in a specific location usinginlineEmbedSelector, several DOM elements are added and can be styled (e.g. height, width, border).
For example, if you embed the chat widget using the #some-id selector, it would be loaded with the following containers and IDs:
Widget behavior
HubSpotConversations.widget
The widget object contains a number of methods that allow you to manipulate the chat widget on your page, including:
Below, learn more about each method.
widget.load
Thewidget.load method handles the initial load on the page. This method is only necessary if you set loadImmediately to false. Otherwise, the widget will load itself automatically.
This method is throttled to one call per second.
| Field | Type | Default | Description |
|---|---|---|---|
widgetOpen | Boolean | false | Whether the widget should load in an open state. |
widget.refresh
Thewidget.refresh method handles refreshing and re-rendering the widget’s information, given the current page URL. This method can be useful for chat widgets embedded in single-page applications when you need to refresh the widget on route changes. This method also enables you to specify different chat widgets on different page routes.
If you call widget.refresh on a route where there is no chat widget, and the user isn’t engaged in a chat, the widget will be removed. It will not remove the widget when there is a currently active chat.
This method is throttled to one call per second.
| Field | Type | Default | Description |
|---|---|---|---|
openToNewThread | Boolean | false | Whether to force a new thread to be created. |
Example
Using this method, you could create buttons and links to open specific chatflows on a page by adding query parameters to the page URL.
For example, you could add the following code to your pages to generate the buttons:
widget.open
Thewidget.open method opens the widget if it is not already open or isn’t currently loaded.
widget.close
Thewidget.close method closes the widget if it isn’t already closed.
widget.remove
Thewidget.remove method removes the widget from the page. If the widget isn’t present on the page, this method does nothing. The widget will display again on page refresh or if widget.load is invoked.
widget.status
Thewidget.status method returns an object containing properties related to the current status of the widget.
| Field | Type | Default | Description |
|---|---|---|---|
loaded | Boolean | false | Whether the widget iframe has loaded. |
Clear chat cookies
Theclear method deletes cookies related to the chat widget and returns it to its default state on subsequent load.
The chat widget creates several cookies to preserve its state across site visits and page refreshes. These cookies are scoped to the domain of the page hosting the widget, and are used to support the following features:
- Referencing historical conversations.
- Persisting the open state of the chat widget across page loads.
- Persisting the open state of the welcome message across page loads.
messagesUtkhs-messages-is-openhs-messages-hide-welcome-message
{resetWidget:true} to the clear() function to clear all chat related cookies, remove the widget from the page, and create a new instance of the chat widget.
Chat events
The chat widget emits various events you can listen and respond to throughout its lifecycle. These events include:- conversationStarted
- conversationClosed
- userSelectedThread
- unreadConversationCountChanged
- contactAssociated
- userInteractedWithWidget
- widgetLoaded
- quickReplyButtonClick
- widgetClosed
on and off, as shown below.
conversationStarted
TheconversationStarted event triggers when a new conversation has been successfully started.
| Field | Type | Description |
|---|---|---|
payload.conversation.conversationId | Number | The thread ID of the conversation that was started. You can use this ID when making calls to the conversations API. |
conversationClosed
TheconversationClosed event triggers when a new conversation has marked as closed from the conversations inbox.
Site visitors minimizing or closing the chat widget will not trigger this event. For that event, use widgetClosed instead.
| Field | Type | Description |
|---|---|---|
payload.conversation.conversationId | Number | The thread ID of the conversation that was started. You can use this ID when making calls to the conversations API. |
userSelectedThread
TheuserSelectedThread event triggers when creating a thread or selecting an existing thread.
| Field | Type | Description |
|---|---|---|
payload.conversation.conversationId | Number | The thread ID of the conversation that was started. You can use this ID when making calls to the conversations API. |
unreadConversationCountChanged
TheunreadConversationCountChanged event is triggered when the number of conversations with unread messages increases or decreases.
| Field | Type | Description |
|---|---|---|
unreadCount | Number | The total number of conversations with at least one unread message. |
contactAssociated
ThecontactAssociated event is triggered when the visitor is associated with a contact in the CRM.
| Field | Type | Description |
|---|---|---|
message | String | A confirmation message that the visitor has been associated with a contact. |
userInteractedWithWidget
TheuserInteractedWithWidget event is triggered when the visitor interacts with the widget, such as clicking to open the widget or closing the initial welcome message.
| Field | Type | Description |
|---|---|---|
message | String | A confirmation message that the visitor has been interacted with the widget. |
widgetLoaded
ThewidgetLoaded event is triggered when the widget iframe is loaded.
| Field | Type | Description |
|---|---|---|
message | String | A confirmation message that the widget iframe has loaded. |
quickReplyButtonClick
ThequickReplyButtonClick event is triggered when the visitor clicks a quick reply in a bot conversation.
| Field | Type | Description |
|---|---|---|
value | Array | An array containing the text of the quick reply option that was clicked. |
In the example screenshot above, the bot chatflow contains three quick reply options. If the user selects Learn more, the resulting event payload would be:
widgetClosed
ThewidgetClosed event is triggered when the visitor closes the chat widget.
| Field | Type | Description |
|---|---|---|
message | String | A confirmation message that the visitor has closed the chat widget. |