Open chat from a button
This example assumes you installed the widget with the snippet in Installation, which createswindow.sagepilotWidgetReady.
1
Mark the elements that open chat
Add the
data-sagepilot-open attribute to any button or link. You can use it on as many elements as you like.2
Handle the click
Add this once per page, after the widget snippet.
Hide the floating launcher
If only your own buttons should open the chat, addhideNativeLauncherButton: true to the init() call. This hides the launcher in the corner of the page and its welcome popup. ChatWidget.open() still works.
The unread-message badge appears only on the Sagepilot launcher. If you hide it, visitors see new replies when they open the chat.
If you cannot change the install snippet
Use this version if the widget is already installed and you cannot edit its snippet, for example when it was installed with the Sagepilot Shopify app embed, through a tag manager, or with the one-line script from Embed Code. It does not needwindow.sagepilotWidgetReady.
ChatWidget.open() returns false until the widget is ready, so the helper retries every 250 ms for up to 10 seconds. While it retries, the browser console may show [ChatWidget] Widget not initialized warnings. These are expected.
Do not add a second copy of the widget script to reach the API. The widget must be loaded only once per page.
Complete example
A Shopify theme that shows its own Ask the concierge button and no floating launcher. Add this tolayout/theme.liquid before </body>, and put the button wherever it should appear in your theme.
Close and toggle the chat
For example, a button that switches between open and closed:
How opening behaves
open()always opens the full chat. It skips the welcome popup and the two-step intro card.- Calling
open()while the chat is already open does nothing. Repeated clicks do not create extra windows or new conversations. - Opening the chat does not send a message. To prefill or send one, see Draft and send messages.
- Visitors who have chatted before on the same browser see their existing conversations.
- On screens 768 px wide or narrower, the chat opens full screen. Visitors close it with the close button inside the chat.
Verify the setup
- Open your site in a private browser window.
- Click your button. The chat should open.
- Click it again several times. The same chat should stay open.
- Close the chat and click the button again. It should reopen.
- Repeat on a phone, or with your browser’s device toolbar set to a phone width.
- In your browser’s developer tools, throttle the network to a slow connection, reload, and click the button immediately. The chat should open once the widget finishes loading.