Skip to content

IM Web SDK Widget Integration Guide ​

Widget mode lets you add the online customer service widget to a business website with a <script> tag. The SDK automatically loads the configuration and renders the floating launcher and chat window.

Quick Integration ​

Create the window.mchat queue before loading the SDK asynchronously. Commands called while the SDK is loading are added to the queue and 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: 'user@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 chat widget's AppKey. You can find it in the chat widget 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 page URL's ?token= parameter.

Set the Signed-In User ​

Call the following before the SDK loads or before the chat window opens for the first time:

js
window.mchat.push('setLoginInfo', {
  user_id: 'user_10001',
  user_name: 'Alex Smith',
  language: 'en-US',
  phone: '+1 202 555 0123',
  email: 'user@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
user_namestringNoUser name field
languagestringNoUser language, such as en-US or zh-CN
phonestringNoPhone number
emailstringNoEmail address
descriptionstringNoUser note
label_namesstring[]NoUser labels
update_label_typeappend | updateNoAppend or replace labels
custom_fields_extRecord<string, unknown>NoCustom extension fields

When the user switches accounts on your site, call setLoginInfo again with the new user_id. When the user signs out, call:

js
window.mchat.push('clearLoginInfo')

Pass User Information in Page Mode ​

Page mode accepts sign-in information through URL query parameters. At least user_id is required:

text
https://chat.example.com/?user_id=user_10001&user_name=Alex%20Smith&language=en-US&phone=%2B12025550123&email=user%40example.com&description=VIP%20customer&label_names=VIP,Signed%20in&update_label_type=append&custom_fields_ext=%7B%22member_level%22%3A%22gold%22%2C%22register_source%22%3A%22web%22%2C%22order_count%22%3A12%7D

Supported parameters are 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 string.

Privacy

URLs may appear in browser history, logs, and analytics systems. Never include passwords, verification codes, tokens, or other sensitive credentials 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 Contact Support 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 message count changes
onSendMessagefunction(message)The visitor sends a message
onReceiveMessagefunction(message)An agent or system message arrives
onWindowOpenfunction()The chat window opens
onWindowClosefunction()The chat window closes
onIconClickfunction()The visitor clicks the floating launcher
onOpenInfoCollectionfunction()The pre-chat survey or offline form opens
onCompleteInfoCollectionfunction(data)The 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 Type Example ​

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.