Chat widget
Load the chat widget with JavaScript
Updated · View as Markdown
The standard embed code loads the widget on every page where it runs. If you need more control, load the script without an ID and start the widget yourself with window.ticketdesk.initChatbot().
How the script starts
The embed script checks for window.TICKETDESK_ID when it loads. If it's set, the widget starts right away. If it isn't, the script only defines window.ticketdesk and waits for you.
<!-- Starts automatically -->
<script>
window.TICKETDESK_ID = 'd_xxxxxxxx-s-xxxxxxxxxxxx';
</script>
<script src="https://ticketdesk.ai/ticketdesk-chatbot.min.js" defer></script>
Copy your chatbot ID from Settings, Chatbots, in the Embed to Website box of the chatbot card.
Start the widget from code
Load the script without setting TICKETDESK_ID, then call initChatbot with the ID when you want the widget to appear:
<script src="https://ticketdesk.ai/ticketdesk-chatbot.min.js" defer></script>
<script>
window.addEventListener('load', () => {
window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');
});
</script>
initChatbot adds a <div id="ticketdesk-ai"> to the end of the page body and renders the widget into it. The domain rules still apply, so the page must be on one of the chatbot's allowed domains.
Remove the widget
initChatbot returns an object with a close() method. Calling it unmounts the widget and removes its container from the page:
const chat = window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');
// Later, for example when the user leaves the help section
chat.close();
Keep the returned object if you plan to remove the widget later. Call initChatbot again to bring it back.
Show the widget only on some pages
A common setup is to show chat on pricing and help pages but not on the checkout or the blog:
const CHATBOT_ID = 'd_xxxxxxxx-s-xxxxxxxxxxxx';
const showOn = ['/pricing', '/help', '/contact'];
window.addEventListener('load', () => {
if (showOn.some((path) => location.pathname.startsWith(path))) {
window.ticketdesk.initChatbot(CHATBOT_ID);
}
});
In a single-page app the page doesn't reload between routes. Call initChatbot and close() from your router's navigation hook instead, and make sure you never start two widgets at once. The React and Next.js guide and Vue and Nuxt guide show component-based setups.
Wait for cookie consent
If your consent banner must approve chat tools first, start the widget from the banner's accept callback:
function onConsentGiven() {
if (!window.ticketdesk) return;
window.ticketdesk.initChatbot('d_xxxxxxxx-s-xxxxxxxxxxxx');
}
The script is still downloaded on page load, but the widget doesn't render or connect until you call initChatbot. To avoid loading the script at all before consent, add the <script> tag from the callback instead and set window.TICKETDESK_ID before it loads.
Troubleshooting
window.ticketdeskis undefined: the script hasn't loaded yet. Withdeferorasync, wait for theloadevent before calling it.- The widget doesn't appear on a staging site: add that domain under Allowed Domains in the chatbot settings.
- The widget starts on every page even though you call
initChatbotyourself: remove thewindow.TICKETDESK_IDline, or the script starts it automatically on load.
For colors, position, the welcome message and suggestion buttons, see customize the chatbot and chat suggestions.