This guide shows you how to add Heyo to your Vue.js application. Compatible with Vue 2, Vue 3, Options API, and Composition API.
Install the SDK:
npm install @heyo.so/js
Then use it in your App component (Composition API):
<script setup>
import { onMounted } from 'vue';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
onMounted(() => {
HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
});
</script>
<template>
<div id="app">
<!-- Your app content -->
</div>
</template>
<script setup>
import { onMounted } from 'vue';
onMounted(() => {
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);
});
</script>
<template>
<div id="app">
<!-- Your app content -->
</div>
</template>
<script>
export default {
name: 'App',
mounted() {
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);
},
};
</script>
Create composables/useChat.js:
import { onMounted } from 'vue';
export const useChat = () => {
onMounted(() => {
if (window.HEYO) {
window.HEYO.init({ projectId: 'YOUR_PROJECT_ID' });
}
});
const showChat = () => window.HEYO?.show();
const hideChat = () => window.HEYO?.hide();
const toggleChat = () => window.HEYO?.toggle();
return {
showChat,
hideChat,
toggleChat,
};
};
Usage in component:
<template>
<button @click="showChat">Open Chat</button>
</template>
<script setup>
import { useChat } from '@/composables/useChat';
const { showChat } = useChat();
</script>
<script setup>
import { watch } from 'vue';
import { useRoute } from 'vue-router';
const route = useRoute();
watch(
() => route.path,
(newPath) => {
// Hide chat on checkout pages
if (newPath.includes('/checkout')) {
window.HEYO?.hide();
} else {
window.HEYO?.show();
}
}
);
</script>
<script setup>
import { onMounted } from 'vue';
import HEYO from '@heyo.so/js'; // or: import { HEYO } from '@heyo.so/js'
const user = {
name: 'John Doe',
email: '[email protected]',
};
onMounted(() => {
HEYO.init({
projectId: 'YOUR_PROJECT_ID',
user,
});
});
</script>
Access chat from your store:
// stores/chat.js
import { defineStore } from 'pinia';
export const useChatStore = defineStore('chat', {
actions: {
openChat() {
window.HEYO?.show();
},
closeChat() {
window.HEYO?.hide();
},
},
});
For TypeScript projects, add type definitions:
declare global {
interface Window {
HEYO: {
init: (config: { projectId: string; user?: any }) => void;
show: () => void;
hide: () => void;
toggle: () => void;
};
}
}
export {};
onBeforeUnmount if loading scripts dynamicallyEnsure the script loads after the DOM is ready and you've added your correct Project ID.
Initialize Heyo only once in your app, preferably in App.vue or main.js.
Add the type definitions shown above to your src/types or env.d.ts file.