Skip to content

Instantly share code, notes, and snippets.

@johnbeadle
Last active September 4, 2017 11:27
Show Gist options
  • Select an option

  • Save johnbeadle/c68c0183ddf2ac7a9789a5f33377abb8 to your computer and use it in GitHub Desktop.

Select an option

Save johnbeadle/c68c0183ddf2ac7a9789a5f33377abb8 to your computer and use it in GitHub Desktop.
how to detect a chat is in progress and take action
/*
*************** PLEASE NOTE *************
* This code is POC -- should be TESTED BEFORE BEING ADDED TO PRODUCTION WEBSITE
* Assumes will only be added to specific pages (logout) where you want this behaviour
* Assumes the page is already tagged with liveperson general code and account number
*/
var LP_CHAT_WINDOW_PARENT_ELEMENT_ID = "lpChat";
var LP_DESTROY_CHAT_WINDOW_IN_PROGRESS_FLAG = true;
var LP_WAIT_FOR_EVENTS_LOOP_LIMIT = 60; // * 500ms per loop = 30secs of max attemps
var LP_WAIT_FOR_EVENTS_LOOP_COUNTER = 0; // counter tracking attempts to find events bridge
var waitForLpTagEvents = setInterval(function () {
if (lpTag.events && lpTag.events.bind || (LP_WAIT_FOR_EVENTS_LOOP_COUNTER >= LP_WAIT_FOR_EVENTS_LOOP_LIMIT)) {
clearInterval(waitForLpTagEvents);
lpBindToChatWindowEvents(); // events bridge now loaded - setup bindings
} else {
LP_WAIT_FOR_EVENTS_LOOP_COUNTER = LP_WAIT_FOR_EVENTS_LOOP_COUNTER + 1;
}
}, 500);
function lpBindToChatWindowEvents() {
if (lpTag && lpTag.events && lpTag.events.bind) {
lpTag.events.bind("lpUnifiedWindow", "conversationInfo", function (eventData, appName) {
if (eventData.state == "interactive") {
warnCustomerChatInProgress();
sendSdeToAgent();
clickCloseChatButton();
// SLEDGEHAMMER APPROACH! Destroy chat window after 30 seconds timer.
if (LP_DESTROY_CHAT_WINDOW_IN_PROGRESS_FLAG) {
var destroyChatWindowTimer = setTimeout(destroyChatWindow, 30000);
}
}
});
}
}
function sendSdeToAgent() {
lpTag.sdes.send({
"type": "error", //MANDATORY
"error": {
"message": "Logged out and still chatting!", // THE ERROR MESSAGE
"code": "er99999" // THE ERROR CODE
}
})
}
function warnCustomerChatInProgress() {
alert("you are still chatting after logging off -- agent will close this chat shortly or it will be closed automatically in 30 seconds");
}
function destroyChatWindow() {
var chatWindow = document.getElementById(LP_CHAT_WINDOW_PARENT_ELEMENT_ID);
if (chatWindow) {
// SLEDGEHAMMER APPROACH! remove from DOM!
chatWindow.parentNode.removeChild(chatWindow);
}
}
function clickCloseChatButton() {
var closeBtn = document.querySelector('.lp_close');
if (closeBtn) {
closeBtn.click();
var confirmBtn = document.querySelector('.lp_confirm_button');
if (confirmBtn) {
setTimeout(function () {
confirmBtn.click(); // wait 2 seconds and then auto click they "are your sure?" confirm button to end the conversation.
// this is required to allow time for the window to create the button before we try and click it
}, 2000);
}
}
}
@johnbeadle
Copy link
Author

alert shown if chat in progress detected....

screen shot 2017-09-04 at 12 11 00

auto click the close chat X button

Opens the confirmation box which is auto clicked after a short delay.

screen shot 2017-09-04 at 12 22 13

Shows any exit survey configured on the window.

screen shot 2017-09-04 at 12 22 22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment