Heyo provides event callbacks to help you execute code at specific moments in the widget lifecycle.
Use onReady() to execute code when the widget is fully loaded and initialized:
HEYO.onReady(() => {
console.log('Widget is ready!');
});
onReady(), the callback is executed immediately. Otherwise, it's queued and called once initialization completes.Use onAgentStatusChange() to execute code when the agent's status changes:
HEYO.onAgentStatusChange((status) => {
console.log('Agent status changed to:', status);
// Update your UI based on status
if (status === 'online') {
showNotification('Support is now available!');
}
});
The callback receives one of three status values:
online: Agent is available and activeaway: Agent is temporarily unavailableoffline: Agent is not availableUpdate UI based on availability:
const statusIndicator = document.getElementById('support-status');
HEYO.onAgentStatusChange((status) => {
if (status === 'online') {
statusIndicator.textContent = '🟢 Chat available';
statusIndicator.style.color = '#22c55e';
} else if (status === 'away') {
statusIndicator.textContent = '🟡 Be right back';
statusIndicator.style.color = '#f59e0b';
} else {
statusIndicator.textContent = '⚫ Leave a message';
statusIndicator.style.color = '#6b7280';
}
});
Auto-open chat when agent comes online:
let hasPrompted = false;
HEYO.onAgentStatusChange((status) => {
if (status === 'online' && !hasPrompted) {
hasPrompted = true;
setTimeout(() => HEYO.open(), 3000);
}
});
Multiple callbacks for different purposes:
// Update UI
HEYO.onAgentStatusChange((status) => {
updateSupportButton(status);
});
// Track analytics
HEYO.onAgentStatusChange((status) => {
analytics.track('agent_status_changed', { status });
});
You typically don't need onReady() because the SDK automatically queues all method calls until the widget is ready. However, it's useful when you need to:
You don't need onReady() for regular widget methods:
// ❌ Not needed - these work fine without onReady
HEYO.onReady(() => {
HEYO.identify({ userId: '123' });
HEYO.open();
HEYO.configure({ widgetColor: '#10b981' });
});
// ✅ Just call them directly - they're automatically queued
HEYO.identify({ userId: '123' });
HEYO.open();
HEYO.configure({ widgetColor: '#10b981' });
Confirm widget loaded successfully:
HEYO.onReady(() => {
console.log('Heyo widget loaded successfully');
analytics.track('chat_widget_loaded');
});
Initialize based on async data:
// Fetch user data asynchronously
async function initializeChat() {
const user = await fetchCurrentUser();
// Wait for widget to be ready before identifying
HEYO.onReady(() => {
if (user) {
HEYO.identify({
userId: user.id,
email: user.email,
name: user.name,
});
}
});
}
initializeChat();
Integration with monitoring tools:
HEYO.onReady(() => {
// Report to your monitoring service
Sentry.addBreadcrumb({
category: 'chat',
message: 'Chat widget initialized',
level: 'info',
});
});
onReady(). The SDK handles queueing automatically, so you can call methods like identify(), open(), and configure() immediately after loading the script.