Kustomer.addListener()
Adds a callback function that is run after a specific event
First, initialize chat fullyKustomer Chat must finish initializing fully with
Kustomer.start()before you can execute any additional Web SDK methods.Tip: Call other methods inside the callback of
Kustomer.start()to ensure Kustomer Chat always completes initialization before the code tries to run other methods.To learn more, see Troubleshooting: Kustomer method calls won't execute.
Examples
We're provided some examples of how you can call Kustomer.addListener():
const onOpenHandler = function() {
console.log('Widget was opened');
}
Kustomer.addListener('onOpen', onOpenHandler);const onConversationCreateHandler = function(response, error) {
if (error) {
console.error(error);
return;
}
Kustomer.describeConversation(
{
conversationId: response.conversationId,
customAttributes: {
customName: 'SOME_CUSTOM_NAME',
},
},
function(response, error) {
if (error) {
console.error(error);
} else {
console.log('Successfully described conversation');
}
}
);
};
Kustomer.addListener('onConversationCreate', onConversationCreateHandler);
Remove extra listeners 🧹Remove any extra listeners you no longer need with
Kustomer.removeListener()to clear up your memory, just as you would for a nativewindow.addEventListenerlistener.
Syntax
Kustomer.addListener(event, function(callbackResponse, error))| Parameter | Type | Description |
|---|---|---|
| event | String | Required The name of the event you want to add a listener to, for example, |
| function(callbackResponse, error) | Function | Required The function you want to run after the event is triggered. callbackResponse is either error is either |
event types
event typesonUnread
onUnreadHook for when the unread count changes
// callbackResponse
{
total: Number,
change: {
conversationId: String,
count: Number
}
}onOpen
onOpenHook for after the chat widget is opened.
onClose
onCloseHook for after the chat widget is minimized.
onConversationCreate
onConversationCreateHook for after a conversation is created.
// callbackResponse
{
conversationId: String,
createdAt: String,
ended: Boolean,
isInAssistantMode: Boolean
}onLogin
onLoginHook for after the user is authenticated with Kustomer.login().
// callbackResponse
{
identified: Boolean,
email: String | undefined,
externalId: String | undefined,
customerId: String,
}onLogout
onLogoutHook for after the user is logged out with Kustomer.logout().
Updated 16 days ago