Chatspark
K

API Reference

v1
K

Widget Events

Listen to chat activity with window.ChatSpark.on(event, handler). React to user messages, agent responses, lead capture, and more.

Subscribing to Events

Use ChatSpark.on(eventName, callback) to register handlers. The callback receives an event-specific payload:

javascript

window.ChatSpark.on('user-message', function(payload) {
  console.log('User sent:', payload.message);
});

Available Events

user-message

Fired when the user sends a message. Payload: { message, conversationId }

javascript

window.ChatSpark.on('user-message', function(data) {
  console.log('User message:', data.message);
  analytics.track('chat_message_sent', { text: data.message });
});

agent-response

Fired when the agent finishes a response. Payload: { message, conversationId, messageId }

javascript

window.ChatSpark.on('agent-response', function(data) {
  console.log('Agent replied:', data.message);
  // Use for analytics, logging, or UI updates
});

conversation-started

Fired when a new conversation begins. Payload: { conversationId }

javascript

window.ChatSpark.on('conversation-started', function(data) {
  console.log('New conversation:', data.conversationId);
  // Reset page state, start session timer, etc.
});

lead-captured

Fired when a user completes lead capture. Payload: { email, name, phone, ... }

javascript

window.ChatSpark.on('lead-captured', function(data) {
  console.log('Lead:', data.email, data.name);
  // Send to CRM, analytics, or close the chat
  fetch('/api/leads', {
    method: 'POST',
    body: JSON.stringify(data),
    headers: { 'Content-Type': 'application/json' }
  });
});

action-triggered

Fired when a user triggers a custom action (e.g. button click or form submit). Payload: { actionId, actionType, data }

javascript

window.ChatSpark.on('action-triggered', function(data) {
  if (data.actionId === 'book_demo') {
    window.location.href = '/book-demo?source=chat';
  }
});

Unsubscribing

To remove a listener, call ChatSpark.off(eventName, handler) with the same function reference:

javascript

const handler = function(data) { console.log(data); };
window.ChatSpark.on('user-message', handler);

// Later, to unsubscribe:
window.ChatSpark.off('user-message', handler);

Previous

Controls

Next

Custom Messages