Hi,
We have HubSpot conversation chatflows in our Wordpress site and we are using four different chatflows for different pages of the site. Those are individually working as expected. However, the current issue is that if a user visits the home page and begins a chat conversation there, then that chat conversation history gets carried over to other pages on the site. This is an issue because we have a different chatflow for example on the contact page. So, if the user first begins a chat conversation on the home page and then changes the page to the contact page, the same chat history from the home page would still be remembered and visible. What we need is for the conversation widget to reset itself to make sure that the correct chatflow is loaded everytime on page load.
I’ve read the Conversations SDK documentation: https://developers.hubspot.com/docs/api/conversation/chat-widget-sdk and I believe the following clear method is exactly what we need:
window.HubSpotConversations.clear({resetWidget:true});
Based on the examples shown there, I created the following JavaScript which is being loaded in the <head> of the HTML page:
<script type="text/javascript">
// Define the function which is to be called once the widget is ready
function onConversationsAPIReady() {
console.log(`1. HubSpot Conversations API: ${window.HubSpotConversations}`);
// Check and log the widget status
const status = window.HubSpotConversations.widget.status();
console.log(`2.` + status);
if (status.loaded) {
console.log(`3. HubSpot conversations widget is loaded and will not be cleared`);
} else {
console.log(`3. About to clear HubSpot conversations widget and HubSpot cookies`);
// Delete conversation cookies and return the widget to default state
// Reason: We do not want to preserve the chat state across different pages
window.HubSpotConversations.clear({resetWidget:true});
console.log(`4. HubSpot conversations widget was cleared`);
}
}
// If external API methods are already available, use them
if (window.HubSpotConversations) {
// Call the function
onConversationsAPIReady();
console.log(`5. Function call`);
} else {
// Otherwise, callbacks can be added to the hsConversationsOnReady window object.
// These callbacks will be called once the external API has been initialized.
window.hsConversationsOnReady = [onConversationsAPIReady];
console.log(`5. Callback`);
}
</script>
However it is causing a loop in which the onConversationsAPIReady() function seems to be called over and over again which can be seen from the console logs (marked in red). I’m thinking this could be because the window.HubSpotConversations method is also using the window.hsConversationsOnReady event which then calls the function over and over again (?):
If I change the “clear” method to any other method, for example to:
window.HubSpotConversations.widget.refresh();
then the onConversationsAPIReady() function only gets called once as expected. So, something in the
window.HubSpotConversations.clear({resetWidget:true});
method is causing this loop and the documentation does not really explain why. We need to use that method so that the chat conversation gets cleared every time on page load by deleting the associated cookies. Or is there another way?
Could you please assist on how this method is supposed to be used without causing the looping issue? Calling the function repeatedly is increasing page load time until the maximum call stack size gets exceeded and the script finally ends. I could not find any working examples of this.
Disclaimer: Unfortunately, I cannot share a live example on our production site, but I’d expect that my code would give similar results on other sites as well. Also, I am not an actual web developer so in-depth answers are greatly appreciated.
Thank you in advance.





