This guide shows you how to add Heyo to any JavaScript application, whether using vanilla JS, jQuery, or any library.
Add to your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My App</title>
<script src="https://heyo.so/embed/script" defer data-project-id="YOUR_PROJECT_ID"></script>
</head>
<body>
<!-- Your app -->
</body>
</html>
Install via npm:
npm install @heyo.so/js
Then in your JavaScript:
// Use either import style
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
// Initialize on page load
window.addEventListener('load', () => {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
});
Load the script dynamically:
(function() {
const script = document.createElement('script');
script.src = 'https://heyo.so/embed/script';
script.defer = true;
script.setAttribute('data-project-id', 'YOUR_PROJECT_ID');
document.head.appendChild(script);
})();
// Show the chat widget
window.HEYO.show();
// Hide the chat widget
window.HEYO.hide();
// Toggle visibility
window.HEYO.toggle();
<button id="chat-button">Need Help?</button>
<script>
document.getElementById('chat-button').addEventListener('click', () => {
window.HEYO?.show();
});
</script>
window.addEventListener('load', () => {
if (window.HEYO) {
// Widget is ready
console.log('Heyo is loaded');
}
});
window.addEventListener('load', () => {
const user = getUserFromSession(); // Your auth logic
if (user) {
window.HEYO.init({
projectId: 'YOUR_PROJECT_ID',
user: {
name: user.name,
email: user.email,
id: user.id
}
});
}
});
Track when chat opens:
// Custom event handling
document.addEventListener('DOMContentLoaded', () => {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// Detect when chat widget changes
if (mutation.target.id === 'heyo-widget-button') {
console.log('Chat widget state changed');
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
For SPAs that change routes without page reload:
// Call on route change
function onRouteChange(route) {
// Show/hide chat based on route
if (route === '/checkout') {
window.HEYO?.hide();
} else {
window.HEYO?.show();
}
}
// Your router integration
router.on('routeChange', onRouteChange);
$(document).ready(function() {
// Initialize Heyo
if (window.HEYO) {
window.HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
// Custom button
$('#chat-trigger').on('click', function(e) {
e.preventDefault();
window.HEYO?.show();
});
});
async function initializeChat() {
// Wait for user data
const user = await fetchUserData();
// Initialize with user context
window.HEYO?.init({
projectId: 'YOUR_PROJECT_ID',
user: {
name: user.name,
email: user.email
}
});
}
window.addEventListener('load', initializeChat);
const Chat = (function() {
let initialized = false;
function init(projectId, userData) {
if (initialized) return;
window.HEYO?.init({
projectId,
user: userData
});
initialized = true;
}
function open() {
window.HEYO?.show();
}
function close() {
window.HEYO?.hide();
}
return {
init,
open,
close
};
})();
// Usage
Chat.init('YOUR_PROJECT_ID', { name: 'John', email: '[email protected]' });
Chat.open();
class HeyoChat {
constructor(projectId) {
this.projectId = projectId;
this.init();
}
init() {
window.addEventListener('load', () => {
window.HEYO?.init({ projectId: this.projectId });
});
}
show() {
window.HEYO?.show();
}
hide() {
window.HEYO?.hide();
}
setUser(user) {
window.HEYO?.init({
projectId: this.projectId,
user
});
}
}
// Usage
const chat = new HeyoChat('YOUR_PROJECT_ID');
chat.show();
Add type definitions:
declare global {
interface Window {
HEYO: {
init: (config: { projectId: string; user?: any }) => void;
show: () => void;
hide: () => void;
toggle: () => void;
};
}
}
export {};
Usage in TypeScript:
class ChatWidget {
private projectId: string;
constructor(projectId: string) {
this.projectId = projectId;
this.initialize();
}
private initialize(): void {
window.addEventListener('load', () => {
window.HEYO?.init({ projectId: this.projectId });
});
}
public show(): void {
window.HEYO?.show();
}
public hide(): void {
window.HEYO?.hide();
}
}
defer attribute)load event before calling HEYO methods?.) when calling HEYO methodswindow.addEventListener('load', ...)init() once per page