🇺🇸English
🇪🇸Español
🇧🇷Português

docs.callbacks.title

docs.callbacks.intro

docs.callbacks.available

docs.callbacks.callbackdocs.callbacks.parametersdocs.callbacks.description
onReady() => voiddocs.callbacks.onReadyDesc
onOpen() => voiddocs.callbacks.onOpenDesc
onClose() => voiddocs.callbacks.onCloseDesc
onMessageSent(message: ChatMessage) => voiddocs.callbacks.onMessageSentDesc
onMessageReceived(message: ChatMessage) => voiddocs.callbacks.onMessageReceivedDesc
onError(error: WidgetError) => voiddocs.callbacks.onErrorDesc
onUserUpdated(user: UserConfig) => voiddocs.callbacks.onUserUpdatedDesc
onAnalyticsEvent(event: AnalyticsEvent) => voidCalled for all trackable user interactions (widget open/close, messages, errors, etc.)

docs.callbacks.example

ChaskWidget.init({
  apiKey: 'your-api-key',
  organizationId: 'your-org-id',
  callbacks: {
    // Called when widget is ready
    onReady: () => {
      console.log('Widget is ready!');
    },

    // Called when chat opens
    onOpen: () => {
      console.log('Chat window opened');
      analytics.track('chat_opened');
    },

    // Called when chat closes
    onClose: () => {
      console.log('Chat window closed');
    },

    // Called when user sends a message
    onMessageSent: (message) => {
      console.log('Message sent:', message.content);
    },

    // Called when a message is received
    onMessageReceived: (message) => {
      console.log('New message from:', message.sender);

      if (!document.hasFocus()) {
        new Notification('New message', {
          body: message.content
        });
      }
    },

    // Called on errors
    onError: (error) => {
      console.error('Widget error:', error.message);
      errorTracker.capture(error);
    },

    // Called when user info is updated
    onUserUpdated: (user) => {
      console.log('User updated:', user.name);
    }
  }
});

docs.callbacks.messageObject

docs.callbacks.messageObjectDesc

docs.api.propertydocs.configuration.typedocs.callbacks.description
idstringUnique message identifier
contentstringMessage text content
sender'user' | 'agent' | 'system'Who sent the message
timestampDateWhen the message was sent
status'sending' | 'sent' | 'delivered' | 'read' | 'failed'Message delivery status

docs.callbacks.errorObject

docs.callbacks.errorObjectDesc

docs.api.propertydocs.configuration.typedocs.callbacks.description
codestringError code (e.g., 'CONNECTION_ERROR', 'AUTH_ERROR')
messagestringHuman-readable error message
detailsobjectAdditional error details (optional)

Common Error Codes

Codedocs.callbacks.description
CONNECTION_ERRORFailed to connect to the server
AUTH_ERRORAuthentication failed (invalid API key)
SEND_ERRORFailed to send message
NETWORK_ERRORNetwork connection issue

Analytics Events

The onAnalyticsEvent callback provides a unified way to track all user interactions with the widget. This is useful for integrating with analytics platforms like Google Analytics, Mixpanel, or custom tracking systems.

Analytics Event Object

docs.api.propertydocs.configuration.typedocs.callbacks.description
typeAnalyticsEventTypeThe type of event that occurred
timestampDateWhen the event occurred
dataobjectAdditional event-specific data (optional)

Available Event Types

Event Typedocs.callbacks.descriptionData
widget_loadedWidget has been initialized and is ready-
widget_openedChat window was opened-
widget_closedChat window was closed-
message_sentUser sent a message{messageId, hasAttachments}
message_receivedMessage received from agent{messageId, sender}
attachment_uploadedFile was uploaded{fileType, fileSize}
session_startedNew chat session started{conversationId}
error_occurredAn error occurred{code, message}

Analytics Integration Example

ChaskWidget.init({
  apiKey: 'your-api-key',
  organizationId: 'your-org-id',
  callbacks: {
    onAnalyticsEvent: (event) => {
      // Google Analytics 4
      gtag('event', event.type, {
        ...event.data,
        timestamp: event.timestamp.toISOString()
      });

      // Mixpanel
      mixpanel.track(event.type, event.data);

      // Custom analytics endpoint
      fetch('/api/analytics', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(event)
      });

      // Console logging for debugging
      console.log('[Analytics]', event.type, event.data);
    }
  }
});