I have a custom code to fire my Live chat bot instance if a user visits a particular URL.
if(location.hash.indexOf("#/financial-bills")>-1){
return true;
The issue is the chats are repeating if I go to child URLs e.g #/financial-bills/summary My chat code is below:
var initESW = function(gslbBaseURL) {
embedded_svc.init(
'https://example.com',
'https://example/ChatBot',
gslbBaseURL,
'00D4C00000019Iy',
'Support_Only',
{
baseLiveAgentContentURL: 'https://c.la2-c1cs.com/content',
deploymentId: '5721E',
buttonId: '5731E',
baseLiveAgentURL: 'https://d.la2-c1cs.com/chat',
eswLiveAgentDevName: 'EmbeddedServiceLiveAgent_Parent0200EAA_1754b2de11b',
isOfflineSupportEnabled: false
}
);
};
if (!window.embedded_svc) {
var s = document.createElement('script');
s.setAttribute('src', 'https://example/embeddedservice/5.0/esw.min.js');
s.onload = function() {
initESW(null);
};
document.body.appendChild(s);
} else {
initESW('https://service.force.com');
}
My issue is I want the chat function to initialize only once. As I beleive it is causing the chats to repeat. I have tried removing the chat script based on URL change event but its not working for me.
script.id = "test";
window.addEventListener('popstate', function (event) {
(elem=document.getElementById("test")).parentNode.removeChild(elem)
});
You can use a temporary session cookie to check if the chat was initialized once or not. If not just initialize the chat and set the cookie.
function setCookie(cname,cvalue) {
document.cookie = cname+"="+cvalue;
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
if(!getCookie('chatInitialized')){
initChat();
setCookie('chatInitialized', true);
}