DealerAI
Webchat

JavaScript SDK

Control the DealerAI webchat widget programmatically from your website JavaScript.

The DealerAI webchat widget exposes a window.DealerAI JavaScript API that lets you open, close, and send messages from your own website code.

Waiting for the SDK

window.DealerAI is created by webcomponent-loader.js. If your embed uses iframe-loader.js, that script only injects webcomponent-loader.js, so window.DealerAI does not exist yet when its onload fires: calling window.DealerAI.api(...) there throws, and window.DealerAI?.api?.(...) silently does nothing.

Wait for the SDK to be ready instead. Once window.DealerAI is defined and the widget element is on the page, the loader sets window.DealerAI.ready to true and fires a dealerai:ready event on window. Listen for the event and also check the flag, so the code works whether it runs before or after the loader:

function initDealerAI() {
  window.DealerAI.addListener('languageChanged', function (language) {
    mySite.setLanguage(language.code);
  });
  window.DealerAI.api({ action: 'setLanguage', data: 'zh-TW' });
}

// Fires for the first load and after every later load().
window.addEventListener('dealerai:ready', initDealerAI);
// The loader may already have run.
if (window.DealerAI && window.DealerAI.ready) {
  initDealerAI();
}

setLanguage is safe to call from the handler: the widget does not need to have finished rendering, because a language set before it has started is held and applied once it does.

dealerai:ready fires again after every load(), and unload() sets ready back to false. Listeners added with addListener belong to the current widget and unload() removes them with it, which is why the example re-adds them in the handler rather than once. If your site never calls unload(), { once: true } on the event listener is fine.


Methods

api(payload)

The primary method for sending commands to the widget.

window.DealerAI.api({ action: 'ACTION_NAME', data: { /* ... */ } });
ActionDescription
openchatOpen the chat window
closechatClose the chat window
sendOpen the chat and send a message or metadata to the AI
messageSend a raw message to the chat without opening the window
resetReset the conversation and start a new session
setLanguageSwitch the widget to another of your dealership's enabled languages
getLanguageReturn the active language and the enabled list (returns a value)

Open the chat window

window.DealerAI.api({ action: 'openchat' });

Close the chat window

window.DealerAI.api({ action: 'closechat' });

Open the chat and send a message

When the customer clicks a custom button on your website (e.g., "Ask about this car"), you can open the chat and pre-send a message — including a VIN and an optional hint — so the AI immediately responds in context:

window.DealerAI.api({
  action: 'send',
  data: {
    vin: '1HGBH41JXMN109186',
    hint: 'The customer wants to know about financing options for this vehicle.',
  },
});

The data object is sent as metadata to the AI on webchat/join. Supported fields:

FieldTypeDescription
vinstringVIN of the vehicle the customer is interested in
hintstringContextual hint passed to the AI to guide the opening response

Reset the conversation

window.DealerAI.api({ action: 'reset' });

This ends the current DirectLine session, clears the message history, and starts fresh. Use this after a customer logs out or after a completed transaction.

Change the language

If your site has its own language toggle, switch the widget along with it:

window.DealerAI.api({ action: 'setLanguage', data: 'zh-TW' });

data is a language tag, matched the same way as the data-lang attribute: case-insensitive, an optional region (fr-CA matches fr), and Chinese matched by script (zh, zh-CN, zh-Hans are Simplified; zh-TW, zh-HK, zh-Hant are Traditional).

The widget's text, welcome message and conversation starters switch straight away, whether the chat is open or closed, without a page reload. Messages already in the conversation stay as they were; new replies use the new language.

The choice is remembered on later page loads, except that a page with data-lang opens in that language, and a language the shopper picks inside the widget always wins. If your site toggles the language through the SDK, either render data-lang from your own language setting or call setLanguage on each page load.

A tag that names a language your dealership hasn't enabled, or isn't a language tag at all, is ignored with a console warning. You can call setLanguage as soon as the loader script has run; a call made before the widget finishes starting is applied once it does.

Read the language

const language = window.DealerAI.api({ action: 'getLanguage' });
// {
//   code: 'zh-hant',
//   name: '繁體',
//   source: 'sdk',
//   supported: [{ code: 'en', name: 'English' }, { code: 'zh-hant', name: '繁體' }, ...]
// }

getLanguage returns synchronously. supported lists the languages enabled for your dealership, in the order configured in the portal. source says how the current language was chosen: 'auto' (browser language), 'embed' (data-lang), 'sdk' (setLanguage) or 'user' (the shopper). It returns null until the widget has finished starting.

Listen for language changes

window.DealerAI.addListener('languageChanged', function (language) {
  // Same shape as getLanguage()
  mySite.setLanguage(language.code);
});

languageChanged fires each time the language changes after the widget has started, whatever changed it. It does not fire for the initial language.


load()

Initialise the widget. Only needed when you have set ?initialize=false on the script URL to defer widget startup.

window.DealerAI.load();

dealerai:ready fires again once load() has run; see Waiting for the SDK.

See Deferred initialisation for the full usage pattern.


unload()

Completely unmount the widget from the page and remove all event listeners.

window.DealerAI.unload();

Call window.DealerAI.load() to remount it. Useful for single-page applications that need to tear down and rebuild the widget when the current route changes significantly (e.g., when navigating from a public page to an authenticated portal).

Listeners added with addListener are removed along with the widget. Add them again after each load(), from a dealerai:ready handler.


Full Integration Example

The following example shows a typical integration on a vehicle detail page where a custom Ask about this car button opens the chat in context with vehicle information:

<dealerai-chat data-dealer-id="YOUR_DEALER_ID"></dealerai-chat>
<script type="module" src="https://webchat.dealerai.com/webcomponent-loader.js"></script>

<button id="ask-btn">Ask about this car</button>

<script>
  // Open chat in context when the button is clicked
  document.getElementById('ask-btn').addEventListener('click', function () {
    window.DealerAI.api({
      action: 'send',
      data: {
        vin: '1HGBH41JXMN109186', // replace with page VIN
        hint: 'Customer clicked "Ask about this car" on the VDP.',
      },
    });
  });
</script>

Analytics Events

The widget automatically tracks interactions and fires events to Google Analytics 4 (GA4) and ShiftDigital / ASC if configured. No extra code is required on your end.

To enable GA4 tracking, configure your GA4 Measurement ID under Settings → Webchat → Custom Tracking → GA4 ID.

GA4 / ASC eventTrigger
Chat impressionWidget is displayed on page
Chat click / openCustomer opens the widget
LeadCustomer submits a lead form
AppointmentCustomer books an appointment

GA4 events are only fired if the window.google_tag_manager object is present on the page. If you use a tag manager such as Google Tag Manager, ensure the GTM container fires on all pages before the DealerAI widget script.

On this page