Send custom context during live chat
The custom context is a collection of key or value pairs. Only primitive values are allowed for any key. The keys must correspond to context variables that are created for the associated workstream. If no context variables have been created under live workstream with a matching logical name, variables are created at runtime assuming the type as String. The custom context provider would be invoked by the live chat widget when a new chat is started. Because the values are case-sensitive, use exact match to pass them to the context variables. More information: Considerations for context variables
Important
- The setContextProvider is supported only for unauthenticated chat. For authenticated chat, you must use the JSON Web Token (JWT). More information: Send authentication tokens
- You can pass only 100 custom context variables during each chat session.
When a customer starts a chat from the portal, you can pass custom context to Omnichannel for Customer Service. This custom context can be used to display information on the user interface and to create routing rules that eventually determine the queue to which you must route the chats.
Follow these steps to send custom context when you start a chat:
- The live chat SDK methods should be invoked after the lcw:ready event event is raised. You can listen for this event by adding your own event listener on the window object.
- After the
lcw:ready
event is raised, register a custom context provider with live chat using the setContextProvider method. - Start the chat using the startChat method.
Sample code
function contextProvider(){
//Here it is assumed that the corresponding work stream would have context variables with logical name of 'contextKey1', 'contextKey2', 'contextKey3'. If no context variable exists with a matching logical name, items are created assuming Type:string
return {
'contextKey1': 'contextValue1', // string value
'contextKey2': 12.34, // number value
'contextKey3': true // boolean value
};
}
window.addEventListener("lcw:ready", function handleLivechatReadyEvent(){
// Handle LiveChat Ready event
// SDK methods are ready for use now
// Setting custom context provider to be used with Chat for Dynamics 365
// The custom context provided by custom context provider can be used for routing the chat to a particular queue
Microsoft.Omnichannel.LiveChatWidget.SDK.setContextProvider(contextProvider);
// Starting a new chat
Microsoft.Omnichannel.LiveChatWidget.SDK.startChat();
});
window.addEventListener("lcw:error", function handleLivechatErrorEvent(errorEvent){
// Handle LiveChat SDK error event
console.log(errorEvent);
});
For information about how to display the context variables in Conversation summary, see Display custom context.
Related information
setContextProvider
getContextProvider
removeContextProvider
Live chat SDK JavaScript API reference
Configure context variables for a bot