JavaScript API
Your store's own JavaScript can control the chat widget through window.$echat: open and close it, tell it who the visitor is, start a conversation, open the Leave a message form, and react when a message arrives or a person from your team joins.
The calls run in the visitor's browser, on your store pages. Nothing has to be set up in the eChat backend.
Calling the widget before it is ready
The widget loads after the page itself, and only after the visitor accepts cookies when you hold it back for cookie consent. Wrap your calls in window.$echat(function () { ... }): the function runs as soon as the widget is ready, or straight away when it already is.
Your code may run before the eChat script is on the page, so add this line first. It is safe to repeat:
window.$echat = typeof window.$echat === 'function' ? window.$echat : function (cb) { (window.$echat.q = window.$echat.q || []).push(cb) };
window.$echat(function () {
// window.$echat.on(...), window.$echat.leaveMessage.open(), ...
});
An error thrown inside your function is written to the browser console and does not stop the widget.
Chat window
| Method | What it does |
|---|---|
$echat.window.open() | Opens the chat window, as a click on the chat button does. |
$echat.window.close() | Closes the chat window. The chat button stays. |
$echat.window.hide() | Hides the widget, the chat button included. |
$echat.window.show() | Shows the chat button again after hide(). |
Leave a message
$echat.leaveMessage.open() opens the chat window on the Leave a message form, the same form a visitor gets when they ask for a person and nobody from your team is available. Use it for your own "Contact support" button.
- When the visitor has a conversation, the form opens in it: the one on screen, or else the latest. The email you get has the transcript of that conversation, as when the visitor reaches the form in the chat.
- When the visitor has no conversation yet, the form opens on its own.
- Your team's status does not matter: the form opens whether someone is Available or not.
- When live chat is off, the widget has no Leave a message form. The call does nothing and writes a warning to the browser console.
Events
$echat.on(event, callback) calls callback each time the event happens. $echat.off(event, callback) stops it; pass the same function you gave to on().
| Event | When | The callback receives |
|---|---|---|
message | A new message appears in the chat: the visitor's, the AI assistant's or your team's. The assistant's reply arrives once, complete. | conversationId, messageId, and senderType: visitor, ai or agent |
handover | The conversation's state with your team changes. | conversationId, and phase: none (the AI is answering), waiting (the visitor asked for a person and waits), human (a person from your team is in the conversation) or offline (the visitor asked for a person, nobody was available, and Leave a message is offered) |
Only what happens while the page is open fires. Messages and states loaded from earlier, when the visitor opens the chat or moves to the next page, do not.
No event contains the text of a message, or the visitor's name or email.
window.$echat(function () {
window.$echat.on('message', function (event) {
console.log(event.senderType, event.conversationId);
});
});
For analytics, the widget also pushes its events to window.dataLayer. See How to track chats in Google Analytics and other tools.
Visitor
The eChat extension already tells the widget who a signed-in customer is: their customer ID, name and email. Use these methods for what the extension does not know.
| Method | What it does |
|---|---|
$echat.visitor.set(fields) | Saves details about the visitor. $name, $email, $phone, $notes and $externalId fill the visitor's standard fields; any other key is saved as a custom field. Example: $echat.visitor.set({$name: 'Jane', plan: 'wholesale'}). |
$echat.visitor.identify(id) | Sets your own ID for the visitor. The same as $echat.visitor.set({$externalId: id}). |
Conversation
| Method | What it does |
|---|---|
$echat.conversation.start() | Starts a new conversation and opens the chat window on it. |
$echat.conversation.sendMessage(text, options) | Sends text as the visitor, as if they had typed it. It goes to options.conversationId when given, otherwise to the visitor's latest conversation. Call start() first when the visitor has none. |
Page views
$echat.trackPage(url, title) records a page view in the visitor's path, which your team sees on the Live chat page. The widget records each page load itself; call it when your store changes the page without reloading it. Without arguments it takes the current address and title.
Before the chat is opened
With Settings > Data & privacy > Track visitors at Only once they open the chat, a new visitor has no session until they open the chat (see Privacy and GDPR). Until then:
| Method | What happens |
|---|---|
$echat.visitor.set(), $echat.visitor.identify() | Held back and sent when the visitor opens the chat. Several calls are combined: a later value for the same field replaces an earlier one. |
$echat.trackPage() | Not recorded. The visitor's path starts with the page the chat is opened on. |
$echat.conversation.start(), $echat.conversation.sendMessage() | Start the tracking, as opening the chat does, then run. |
$echat.window.open(), $echat.leaveMessage.open() | Open the chat, which starts the tracking. |
Visitor context
$echat.context.provider(fn) registers the function the widget asks for the visitor's context (their cart, recent orders and account) the first time the visitor opens the chat. The eChat extension registers its own, and a second call replaces it, so on a Magento store leave it to the extension.
Example: your own "Contact support" button
This shows a button after the AI assistant's first reply, hides it once the visitor is with your team, and opens Leave a message on click.
<button id="contact-support" type="button" hidden>Contact support</button>
<script>
window.$echat = typeof window.$echat === 'function' ? window.$echat : function (cb) { (window.$echat.q = window.$echat.q || []).push(cb) };
window.$echat(function () {
var button = document.getElementById('contact-support');
window.$echat.on('message', function (event) {
if (event.senderType === 'ai') {
button.hidden = false;
}
});
window.$echat.on('handover', function (event) {
button.hidden = event.phase === 'waiting' || event.phase === 'human';
});
button.addEventListener('click', function () {
window.$echat.leaveMessage.open();
});
});
</script>
The visitor's message, the transcript of their chat, and the email they typed in the form reach you in the A visitor left a message email. See Notifications.