Recent searches


No recent searches

The number of unread messages in the widget



Posted Feb 04, 2022

We show the number of unread user messages if the widget is closed. But if the user refreshes the page or goes to another page, the data on the number of unread messages is lost. I remember that everything worked fine before, the data on the number of unread messages was not lost.

To increase the speed of the site and the speed of loading the page, I use the delayed loading of the script and form it by the script. Most of all, that's what it takes. How can I get the number of unread messages in this case?

We have custom launcher, and i added number with this script

zE('webWidget:on', 'chat:unreadMessages', (number) => {
if (number > 0) {
$(".lets-chat-button__number").text(number);
$(".lets-chat-button__text-inner").text("New");

checkButtonTextWidth();

} else {
$(".lets-chat-button__number").text('');
$(".lets-chat-button__text-inner").text("Let’s Chat");
}
})

https://prnt.sc/26oj773

 

And I also see that the default launcher displays the number of unread messages after the page reloads.

https://prnt.sc/26ojwgp


0

3

3 comments

image avatar

Erica Girges

Zendesk Developer Advocacy

Hi Yuliia!
 
Thanks for posting your question to the Community. That definitely is strange. Was the number of unread messages persisting in the UI prior to the use of your site optimization script? If so, I definitely want to go ahead and see if there is another way we can meet your optimization needs while persisting the state of the total of unread messages. 
 
Hope to hear back from you soon!
 
Best, 
 
Erica - Dev Support

0


Hi! Yes, the number of unread messages were shown correctly before optimization.

This is a script with some delay in which I form a script tag for the widget and my file.

window.onload = function () {
const siteUrl = document.location.origin;

function createScript() {
const chatConnection = document.createElement("script");
chatConnection.id = "ze-snippet";

if(siteUrl === "https://masterofcode.com") {
chatConnection.src = "https://static.zdassets.com/ekr/snippet.js?key=d34f9844-cba6-4b57-96f5-3b30aaa3d307";
} else {
chatConnection.src = "https://static.zdassets.com/ekr/snippet.js?key=60bb6a3f-f42a-4ee1-84bb-48b4939af38d";
}

document.getElementById("js-chat-screen").after(chatConnection);
}

function createWidgetScripts() {
const widgetScripts = document.createElement("script");
widgetScripts.id = "ze-snippet";
widgetScripts.src = `${siteUrl}/wp-content/themes/moc/js/src/chatwidget/webwidget.js`;

document.getElementById("widget-script").after(widgetScripts);
}

setTimeout(function(){
createScript();
}, 4000);

setTimeout(function(){
createWidgetScripts();
}, 5000);
};

 

And in my file I use a function that checks when the chat is connected so that there is no error that zE is not a function.

 

var waitForZopim = setInterval(function () {
if (window.$zopim === undefined || window.$zopim.livechat === undefined) {
console.log('loading chat');
return;
}

console.log('chat connected');

zE('webWidget:on', 'chat:unreadMessages', (number) => {
if (number > 0) {
$(".lets-chat-button__number").text(number);
$(".lets-chat-button__text-inner").text("New");

checkButtonTextWidth();

} else {
$(".lets-chat-button__number").text('');
$(".lets-chat-button__text-inner").text("Let’s Chat");

checkButtonTextWidth();
}
})

clearInterval(waitForZopim);
}, 100);

 

0


image avatar

Erica Girges

Zendesk Developer Advocacy

Hi Yuliia,
 
Thank you for sharing these code snippets. Off the bat, I'm not seeing anything that would affect the persistence of state for displaying unread messages. I'm going to go ahead and create a ticket for you so we can deep dive more into this issue. Please keep an eye on your email for that correspondence from me.
 
Best,
 
Erica - Dev Support

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post