Can I stop visitors who load the widget before operating hours end from being routed to offline departments?

Question

If a visitor loads the Web Widget (Classic) before operating hours end, they are able to chat in after all our agents have just gone offline for the day. As a result, they create a missed chat instead of an offline message. Is there a way to ensure the widget will not allow a chat to be requested for a department that is currently offline?

Answer

Disclaimer: This article is provided for instructional purposes only. Zendesk does not support or guarantee the code. Post any issues you have in the comments section or try searching for a solution online.

The native widget behavior is to not be updated in real-time as a specific department goes offline after it was loaded on a page. However, you can force the widget to update whenever this occurs and the visitor isn't already in an active session using the API.

Whenever the chat department has a status update, verify the new status is Offline and then check if the visitor is already in an active session. If the agent's status is Offline and the visitor is not in an active session, then use the updateSettings method to suppress chat.

See the below example that checks if the CRM department is online.

<script id="ze-snippet"
src="https://static.zdassets.com/ekr/snippet.js?key=ACCOUNT_KEY">


// run whenever the department changes
zE('webWidget:on', 'chat:departmentStatus', function(dept) {

// only continue if the desired department is returning offline
if (dept.name === 'CRM' && dept.status === 'offline') {

// get the current state of the chat session
let isChatting = zE('webWidget:get', 'chat:isChatting');

// only update to suppress chat if the visitor is not currently chatting
if (isChatting == false) {
zE('webWidget', 'updateSettings', {
webWidget: {
chat: {
suppress: true
}
}
});
}
}
});
