Question
When a visitor's chat connection times out but they do not close the window or tab that the widget is loaded in, then later reconnect to send a new message, the pre-chat form isn't automatically presented to them. Is there a way to require a visitor to always see the pre-chat form in the Web Widget when they reconnect after a timeout?
Answer
By default, a visitor skips the pre-chat form when they reconnect. However, you can force the pre-chat form to appear upon reconnection using the API by applying the following script:
<script id="ze-snippet"
src="https://static.zdassets.com/ekr/snippet.js?key=ACCOUNT_KEY"> </script>
<script>
// run whenever chat connects (or reconnects)
zE('webWidget:on', 'chat:connected', function() {
// get the current state of the chat session
let isChatting = zE('webWidget:get', 'chat:isChatting');
// check there is not already an active chat session
if (isChatting == false) {
// if not then get the current state of the widget
let widgetState = zE('webWidget:get', 'display');
// check if already in the open 'chat' state, indicating prior timeout
if (widgetState == 'chat') {
// if it is then close the widget, reset and reopen in pre-chat state
zE('webWidget', 'close');
zE('webWidget', 'reset');
zE('webWidget', 'open');
}
}
// finally apply (or reapply) widget settings if needed (see note below)
zE('webWidget', 'updateSettings', {
webWidget: {
chat: {
departments: {
select: 'Shopping Cart'
}
}
}
});
});
</script>
The final step in this script uses the updateSettings API to apply or reapply the settings for the widget. In the above example, the 'Shopping Cart' department is applied whenever the widget first connects or is reconnected. This workflow is discussed in more detail in the related article: Can I reapply the department after a timed-out chat visitor reconnects?
To validate that you've correctly configured this solution, check the current state of the widget when it connects. Then close, reset, and reopen the widget if needed to make sure the pre-chat form always shows to a reconnecting visitor.