Hello,
There’s a bug on Freshchat widget (Freshcat CRM tracking code) where, after an agent resolves a conversation, if the user remains on the page with the widget open and sends a new message, the bot does not get triggered. To address this issue, Freshworks support suggested destroying the widget and re-initializing it when a conversation is resolved.
I’ve attempted to implement this solution, but I’m facing difficulties in properly re-initializing the widget after destroying it. Sometimes, after destroying and recreating the widget, the conversation:resolved
event gets triggered in a loop, causing the widget to be destroyed and created continuously. It does not always happen, but it does occur occasionally.
Here’s the code I’m using:
<script>
window.fcWidgetMessengerConfig = {
firstName: "{{portal.user.firstname}}",
lastName: "{{portal.user.lastname}}",
email: "{{portal.user.email}}",
phone: "{{portal.user.mobile}}"
};
function initializeWidget() {
// Register event for "conversation:resolved"
window.fcWidget.on("conversation:resolved", function (resp) {
console.log('Conversation resolved');
// Destroy the widget
window.fcWidget.destroy();
});
// Register event for "widget:destroyed"
window.fcWidget.on("widget:destroyed", function () {
console.log('Widget destroyed');
// Re-initialize the widget after 1 second
setTimeout(() => {
// Create and add the widget script again
var script = document.createElement("script");
script.src = "//fw-cdn.com/xxx/xxx.js"; // Original widget URL
script.setAttribute("chat", "true");
script.onload = () => {
initializeWidget(); // Reattach the events
};
document.body.appendChild(script);
}, 1000); // Waits 1 second before re-initializing
});
}
window.fcSettings = {
onInit: function () {
initializeWidget();
},
};
</script>
{% if portal.user.email %}
<script
src='//fw-cdn.com/xxx/xxx.js'
chat='true'>
</script>
{% endif %}
So, what is the recommended way to re-initialize the Freshchat widget after destroying it, using the methods available in the documentation?