Events
The ChatCops widget emits events you can listen to for analytics, custom behavior, or integration with your app.
Subscribing to Events
Section titled “Subscribing to Events”// Using .on()ChatCops.on('open', () => { console.log('Chat opened');});
// Returns unsubscribe functionconst unsub = ChatCops.on('message', (data) => { console.log('Message:', data);});
// Unsubscribe laterunsub();Event Types
Section titled “Event Types”Fired when the chat panel opens.
ChatCops.on('open', () => { analytics.track('chat_opened');});Fired when the chat panel closes.
ChatCops.on('close', () => { analytics.track('chat_closed');});message
Section titled “message”Fired when a message is sent or received.
ChatCops.on('message', (data) => { // data.role: 'user' | 'assistant' // data.content: string // data.timestamp: number console.log(`${data.role}: ${data.content}`);});Fired when an error occurs (network failure, rate limit, etc.).
ChatCops.on('error', (error) => { // error.type: 'network' | 'rate_limit' | 'generic' // error.message: string console.error('Chat error:', error.message);});Use Cases
Section titled “Use Cases”Analytics Integration
Section titled “Analytics Integration”ChatCops.on('open', () => { gtag('event', 'chat_widget_open');});
ChatCops.on('message', (data) => { if (data.role === 'user') { gtag('event', 'chat_message_sent'); }});Custom Triggers
Section titled “Custom Triggers”Open the chat when a user clicks a help button:
document.querySelector('#help-btn').addEventListener('click', () => { ChatCops.open();});Conditional Display
Section titled “Conditional Display”// Only show chat on certain pagesif (window.location.pathname.startsWith('/support')) { ChatCops.init({ apiUrl: '/api/chat' });}