Skip to content

IM Web SDK Widget Integration

Widget mode embeds customer service into a website with a <script> tag. The SDK loads the plugin configuration and renders the floating launcher and chat window.

Quick integration

Create the window.mchat queue before loading the SDK. Commands called while the SDK is loading will run after initialization.

html
<script>
  window.mchat = window.mchat || {
    _q: [],
    push: function () {
      this._q.push(Array.prototype.slice.call(arguments))
    },
  }

  window.mchat.push('setLoginInfo', {
    user_id: 'user_123',
    nickname: 'Alex',
    email: 'alex@example.com',
  })
</script>

<script
  src="https://your-domain.com/plugin.js"
  data-app-id="YOUR_APP_KEY"
  async
></script>

Set data-app-id to the AppKey shown in the chat plugin list.

Application ID lookup order

The Widget looks for an application ID in this order:

  1. data-app-id on the current SDK script.
  2. data-token on the current SDK script.
  3. Any script[data-app-id] on the page.
  4. The current URL's ?token= parameter.

For local development, you can also use:

text
http://localhost:5173/?token=YOUR_APP_KEY

Set the signed-in user

Call setLoginInfo before the first chat window opens whenever possible.

js
window.mchat.push('setLoginInfo', {
  user_id: 'user_10001',
  nickname: 'Alex',
  user_name: 'Alex Smith',
  language: 'en-US',
  phone: '+1 202 555 0123',
  email: 'alex@example.com',
  description: 'VIP customer',
  label_names: ['VIP', 'Signed in'],
  update_label_type: 'append',
  custom_fields_ext: {
    member_level: 'gold',
    order_count: 12,
  },
})
FieldTypeRequiredDescription
user_idstringYesUnique user ID from your business system
nicknamestringNoDisplay name
user_namestringNoCompatible user-name field
languagestringNoLanguage such as en-US or zh-CN
phonestringNoPhone number
emailstringNoEmail address
descriptionstringNoInternal user description
label_namesstring[]NoCustomer labels
update_label_typeappend | updateNoAppend or replace labels
custom_fields_extRecord<string, unknown>NoCustom business fields

Call setLoginInfo again with a new user_id when the signed-in account changes. When the user signs out, call:

js
window.mchat.push('clearLoginInfo')

Pass identity to a standalone Page URL

Page mode accepts identity through query parameters. user_id is required to create signed-in identity data.

text
https://chat.example.com/?user_id=user_10001&nickname=Alex&language=en-US

Supported parameters include nickname, user_name, language, phone, email, description, label_names, update_label_type, and custom_fields_ext.

  • Separate label_names with commas.
  • update_label_type accepts only append or update.
  • custom_fields_ext must be a URL-encoded JSON object.

Privacy

URLs may be stored in browser history, access logs, and analytics. Never include passwords, verification codes, tokens, or other secrets in query parameters.

Control the chat window

js
window.mchat.push('openChat')
window.mchat.push('closeChat')
window.mchat.push('hideIcon')
window.mchat.push('showIcon')

Connect a custom button to the chat window:

html
<button type="button" id="contact-service">Contact support</button>
<script>
  document.getElementById('contact-service').addEventListener('click', function () {
    window.mchat.push('openChat')
  })
</script>

Event callbacks

CommandCallback argumentTrigger
onReadyfunction()SDK initialization completes
onUnreadfunction(count)Unread count changes
onSendMessagefunction(message)Visitor sends a message
onReceiveMessagefunction(message)Agent or system message arrives
onWindowOpenfunction()Chat window opens
onWindowClosefunction()Chat window closes
onIconClickfunction()Visitor clicks the launcher
onOpenInfoCollectionfunction()Pre-chat or offline form opens
onCompleteInfoCollectionfunction(data)Information form is submitted
js
window.mchat.push('onReady', function () {
  console.log('SDK ready')
})

window.mchat.push('onUnread', function (count) {
  document.title = count > 0 ? '(' + count + ') New messages' : 'Home'
})

window.mchat.push('onCompleteInfoCollection', function (data) {
  console.log('Information collected:', data)
})

TypeScript declaration

ts
type MChatCommand =
  | 'setLoginInfo'
  | 'clearLoginInfo'
  | 'openChat'
  | 'closeChat'
  | 'hideIcon'
  | 'showIcon'
  | 'onReady'
  | 'onUnread'
  | 'onSendMessage'
  | 'onReceiveMessage'
  | 'onWindowOpen'
  | 'onWindowClose'
  | 'onIconClick'
  | 'onOpenInfoCollection'
  | 'onCompleteInfoCollection'

interface MChatApi {
  version?: string
  _q?: Array<[string, unknown]>
  push(command: MChatCommand, data?: unknown): void
}

declare global {
  interface Window {
    mchat: MChatApi
  }
}

One workspace for global customer communication.