Kustomer.addListener()

Adds a callback function that is run after a specific event

๐Ÿ‘

First, initialize chat fully

Kustomer 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 native window.addEventListener listener.

Syntax

Kustomer.addListener(event, function(callbackResponse, error))
ParameterTypeDescription
eventStringRequired

The name of the event you want to add a listener to, for example, onOpen, onClose, onUnread
function(callbackResponse, error)FunctionRequired

The function you want to run after the event is triggered.

callbackResponse is either null or a value, depending on what event you are hooking onto. See the list of events below for the different structures.

error is either undefined or a native JavaScript Error object.

event types

onUnread

Hook for when the unread count changes

// callbackResponse

{
  total: Number,
  change: {
    conversationId: String,
    count: Number
  }
}

onOpen

Hook for after the chat widget is opened.

onClose

Hook for after the chat widget is minimized.

onConversationCreate

Hook for after a conversation is created.

// callbackResponse

{
  conversationId: String,
  createdAt: String,
  ended: Boolean,
  isInAssistantMode: Boolean
}

onLogin

Hook for after the user is authenticated with Kustomer.login().

// callbackResponse

{
  identified: Boolean,
  email: String | undefined,
  externalId: String | undefined,
  customerId: String,
}

onLogout

Hook for after the user is logged out with Kustomer.logout().


Whatโ€™s Next