Javascript API: Listeners/Events

These events are triggered when the Tiledesk Widget fires a specific event. Register an event handler with:

window.Tiledesk(event_name, handler)

Register an event handler to an event type. The handler signature is function(event_data), where event_data is a JavaScript CustomEvent. More info about CustomEvent: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

Arguments

  • event_name (String, required): Event name to bind to

  • handler (Function, required): Function with the signature function(event_data)

Available events

  • onLoadParams — Fired when the parameters are loaded*. Use this event to change at runtime your Tiledesk settings.

  • onInit — Fired when the widget is initialized

  • onAuthStateChanged — Generated when the user logs in or logs out

  • onOpen — Fired when the widget is open

  • onClose — Fired when the widget is closed

  • onBeforeMessageSend — Fired before the message is sent. Use to add user information or custom attributes to the chat message.

  • onAfterMessageSend — Generated after the message has been sent.

  • onOpenEyeCatcher — Fired when the callout box is opened

  • onClosedEyeCatcher — Fired when the callout box is closed

  • onCloseMessagePreview — Fired when the user clicks the close button in message preview while a new message is received and the widget is closed

  • onNewConversationComponentInit — Fired just after a new conversation is initialized

  • onBeforeDepartmentsFormRender — Fired just before rendering Departments in the Departments view (useful to filter the default Departments list)

  • onMessageCreated — Fired when the widget receives a message

  • onNewConversation — Fired when the widget starts a new conversation

  • onConversationUpdated — Fired when the widget receives a conversation update

*This event (onLoadParams) will be fired before the tiledesk parameters are loaded. Use this event to change at runtime your Tiledesk settings.

Initial events lifecycle

1

onLoadParams -> onInit -> onAuthStateChanged

The initial event lifecycle follows this sequence:

  1. onLoadParams

  2. onInit

  3. onAuthStateChanged

Each event handler receives a single argument: event_data (a JavaScript CustomEvent).

Important payload of event_data

Common important payload values
  • detail.default_settings (Object): the constructor default settings (window.tiledeskSettings)

Examples

Logging widget events

Widget with visitor fullname and email from localStorage

Widget with welcome message containing current date

Event details and examples by event

onBeforeMessageSend

  • Description: Fired before a message is sent. Use to add user information or custom attributes to the chat message.

  • Important payload:

    • detail.message (Object): the message that is being sent

Example — programmatically setting custom user metadata:

Example — add a custom attribute (page title) to the message:

onAfterMessageSend

  • Description: Generated after the message has been sent.

  • Important payload:

    • detail.message (Object): the message that was sent

Example:

onAuthStateChanged

  • Description: Generated when the authentication state changes (e.g., user sign-in, sign-out).

  • Important payload: detail (Object) — the auth event.

Auth Event description (expandable)

Auth event structure
  • event (string) — Possible values: 'online' (user is logged in), 'offline' (user is logged out)

  • isLogged (boolean) — true if the user is logged in, false otherwise

  • user_id (string) — The current user identifier

  • global (object) — An object with all the widget global parameters

  • default_settings (object) — The initial widget config parameters (window.tiledeskSettings)

  • appConfigs (object) — Remote widget config parameters obtained from the remote Tiledesk server

Example:

onBeforeDepartmentsFormRender

  • Description: Generated before rendering the Departments selection view. Use to filter the default Departments list based on conditions.

  • Important payload:

    • detail.departments (Array): the array of the default Departments (modify this array by reference)

Example — filter departments based on widget language (uses department.description to store language in this example):

onNewConversationComponentInit

  • Description: Generated as soon as a new conversation view is rendered. Use to execute actions on conversation start.

  • Important payload:

    • detail.newConvId (Object): the id of the conversation that fired the event

Example — send a hidden message when a conversation starts (useful to fire a bot welcome message):

onMessageCreated

  • Description: Generated when the widget receives a message.

  • Important payload:

    • detail (Object): the message that was received

Example:

onConversationUpdated

  • Description: Generated when the widget receives a conversation update.

  • Important payload:

    • detail (Object): the conversation that was received

Example:

Related

  • Previous: Javascript API: Attributes — https://developer.tiledesk.com/widget/attributes

  • Next: Widget Authentication — https://developer.tiledesk.com/widget/auth

Last updated 2 years ago