Question
If a visitor times out their connection to Chat but does not close the window or tab the widget is loaded in, then reconnect the widget to send a new message, the pre-chat form won't automatically be presented to the visitor. 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 once more upon reconnection by using the API. First, 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.
<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>
Note that the final step in this script is using the updateSettings API to apply or reapply the settings for the widget, such as the Shopping Cart department in the above example, whenever the widget first connects or reconnects. This workflow is discussed in more detail in the related article: Can I reapply the department after a timed-out chat visitor reconnects?
4 Comments
How do I talk to someone
Hi Maria -
Please see the options available here: Contacting Zendesk Customer Support
This answer code seems to always throw an error: "VM856:3 Uncaught ReferenceError: zE is not defined
at <anonymous>:3:1
at Object.updateUserHtmlFrame"
Hi CJ, I would expect to see this "Uncaught ReferenceError: zE is not defined" error if the script in this article is being used without your account-specific widget snippet being in your code first.
Try adding the widget snippet from your account (on the Channels > Widget page in Support) above the script using the widget APIs - your Web Widget will need to be loaded before these zE APIs would work.
Hope this helps!
Please sign in to leave a comment.