Recipe: Automatically route chats based on department online status

Return to top
Have more questions? Submit a request

45 Comments

  • Sergiu Birca
    Zendesk team member

    Hi Filiph,

    when departments 1,2 and 3 is offline the chat is still working and online on Site1, only the pre-chat form is not visible. Im guessing its because there is still agents avalable in departments 4/5/6 (which is departments for Site2). The agents in Site2-departments cannot handle chats for Site1 and vice versa.

     

    Yes, if at least one of the departments is online => the widget will be presented in the online state.

    To avoid this you have to set the entire account as offline:

    $zopim.livechat.setStatus('offline');

     

    pseudo code:


    if (dep1.status == 'offline' && dep2.status == 'offline' && dep3.status == 'offline')
    {

    // Set the account status to 'offline'
    $zopim.livechat.setStatus('offline');

    } else {

    // set everything you want to set if the department(s) is/are online
    }

     

    In case if you offer the chat channel via the Web Widget + other channels as well (HC and/or Contact form), pls have a look at this article as well:
    https://support.zendesk.com/hc/en-us/articles/115000566208-Web-Widget-Chat-advanced-customization

     

    Hope it helps.

    2
  • Sergiu Birca
    Zendesk team member

    Hi Ifeanyi,

    There isn't a functionality that would allow you identify this state. The system will add the new customers to the queue and will allocate the first chat from the queue to the first agent that will close a chat (chat limit -1). 

    If you want to have a certain control over this and are ready to invest in dev work (+ your account is on Enterprise), you can check our Real Time API:

    https://developer.zendesk.com/rest_api/docs/chat/apis

    It has a parameter that returns the incoming chats value (number of customers waiting to be served (your queue)). You can use that data in a script.

    pseudocode:

    If (incoming chat value > x)
    {
    // route the current chat to a second department

    else
    {
    // route the current chat to the default department
    }

     

    Hope it helps.

     

    2
  • James Lawrence

    Indeed, the department was disabled, thank you for the help.

    1
  • industry

    Awesome, I got it working now!

    I think it was the .setOnStatus function in the original post, which caused the error!

    Thank you both Armi and Filiph for your help!

     

    Best

    An

    1
  • Sergiu Birca
    Zendesk team member
    Hey Charles, If you offer the chat service via the Zendesk Web Widget (second script), then you don't have to do anything (no need to modify the script). If the Contact Form option is enabled in the Web Widget settings, then the system will go straight to the Web Widget Contact form page (will skip the chat step, even if there are online agents in other departments). If you do it via the Zopim widget (the first script), then you will just have to remove this line: $zopim.livechat.hideAll(); from the script + enable the Offline Form option in Zopim dashboard / Widget / Forms.
    1
  • Armi Trixia San Juan

    Hi An,

    To set the department of your chats per URL, you may use the following API:

    For Department A /Website A - Replace YOURDEPARTMENT with the name of your Department A

    <script type="text/javascript">
    $zopim(function() {
    $zopim.livechat.setOnConnected(function() {
    var department_status = $zopim.livechat.departments.getDepartment('YOURDEPARTMENT');
    if( department_status.status=='online')
    {
    $zopim.livechat.departments.setVisitorDepartment('YOURDEPARTMENT');
    }
    else
    {
    $zopim.livechat.setStatus('offline');
    }
    });
    });
    });
    </script>

    Same goes for Department B/Website B - Just change YOURDEPARTMENT with the name of your Department B.

    Hope this helps.

    Cheers!

    1
  • Filiph Petzäll

    Thank you so much @Sergiu, you are amazing!  

    Just to let you and all the ones who find themselves in a similar situation know that this worked for us and now we're able to control online/offline status of the chat on each site independently based on if agents are avalable! Thanks a lot! 

    Here's some code to help in case someone reads this with the same issue;

    $zopim(function() {
    $zopim.livechat.setOnConnected(function() {
    $zopim.livechat.setOnStatus(function () {
    //Feel free to add however many departments you'd like to this list
    var dep1 = $zopim.livechat.departments.getDepartment('yourDepartmentName');
    var dep2 = $zopim.livechat.departments.getDepartment('yourDepartmentName');
    var dep3 = $zopim.livechat.departments.getDepartment('yourDepartmentName');

    //Check status of each dep. from list above and include && after in the conditions for IF statement
    if (dep1.status == 'offline' && dep2.status == 'offline' && dep3.status == 'offline'){
    //This is what happens if ALL departments is offline
    $zopim.livechat.setStatus('offline');
    } else {
    //This is what happens if one or more departments is online
    $zopim.livechat.setStatus('online');
    }
    });
    });
    });

    UPDATE: https://chat.zendesk.com/hc/en-us/articles/218624817/comments/360001771448

    1
  • Sergiu Birca
    Zendesk team member

    Hi @James,

    a couple of reasons for this:

    - The department is disabled (make sure it is enabled);

    - The department name is Case sensitive. Make sure you use the exact name.

     

    Hope it helps.

    1
  • Ifeanyi Obiano

    Thanks for this.

    Can I check for when chat limits has been reached for all agents in a department and reroute?

    on other words, I want to route chats to a default department when all agents in a department has reached chat limits

    1
  • Sergiu Birca
    Zendesk team member

    Hi Nathan,

    yep, that is the solution (I should probably consider this use case as well (the department switching to offline for customers that are already engaged)).

     

    Depending on the implementation, there might be the case of displaying the .window element for customers that are already engaged in a chat, who keep accessing various pages on the website where the script is loaded.

    So potentially you might want to add to your if statement, an else:

    else{

     

    $zopim.livechat.window.show();

    }

    0
  • Mandvi Singh

    Hi @Devan can you help me with this?

    0
  • Mike Sutton

    That looks like it should work. I'll give it a shot.

    Thanks, Sergiu!

    0
  • Rameez Munir Bari

    Hi Sergiu,

    I am facing a issue in zopim in which offline zendesk tickets are not creating as my whole department is invisible for whole day. For Example: Department XYZ is Invisible for 23rd,Jan and they receive 20 overall chat during but only 15 offline zendesk tickets are been created there no zendesk ticket against 5. 

    I have talked with Support team of zendesk they say that

    "The way the widget may be working right now is that upon loading the webpage, the widget checks to see if agents are available in that department and if the page is idle for hours before the user tries to initiate a chat, the widget still thinks agents are available even if none are actually available. What we would recommend doing instead is changing your script so that instead of checking for available agents upon page load, have the widget check when the user clicks the widget to initiate a chat"

    We have Zopim triggers enabled that sets the department when the end-user first loads the widget.

    So can you please help what changing should i made in the script or is it some think else is the issue.

     

    Thanks

    0
  • James Lawrence
    The Line:
    if (department_status.status == 'offline')

    is always giving me an error:
    Uncaught TypeError: Cannot read property 'status' of undefined

    I have double checked the spelling and the case of our departments
    0
  • Carmen Lúcia Fernandes

    Funciona com o widget da central de ajuda e qdo nao permite o acesso ao chat. Usamos um novo recurso de comunicação entre o chat e a central, por meio de esta atualização temos clientes que estão entrando "sem departamento". Temos varios departamentos cadastrados, mas alguns clientes não receberam uma tela para a escolha do departamento. O problema foi resolvido, o problema foi solucionado, mas ainda existe muitos casos nesta situação. Preciso saber se o código está em uso, o que é o problema, se o problema esta entre uma navegação de bate-papo e central ou nenhum encerramento do bate-papo.

    0
  • Sergiu Birca
    Zendesk team member

    Hi Mike,

    The WW (with the new sets of APIs) docs:

    https://developer.zendesk.com/embeddables/docs/widget/core

    If you have the WW + integrated chat enabled => you should be using the suppress endpoint to suppress the chat channel:

    zE('webWidget', 'updateSettings', { webWidget: { chat: { suppress: true }} });

    the setStatus (part of the Chat JS APIs) to offline is not part of the new WW APIs.

     

    If you need to build something that will have a similar logic:

    if (the right department is online){//do something}

    else {//kill the chat channel in the WW for this customer}

     

    =>

     

    if (department_status && department_status.status === 'online') {
    // ONLINE LOGIC
    zE('webWidget', 'updateSettings', {
    webWidget: {
    chat: {
    // Set a specific department and hide the department selection drop-down field IF enabled: ['']
    // Set a specific department and show only one department in selection drop-down field IF enabled: [departmentName]
    departments: {
    enabled: [''],
    //enabled: [config.departmentName],
    select: config.departmentName
    },
    suppress: false
    }
    }
    });
    } else {
    // OFFLINE & UNDEFINED LOGIC (undefined = a department with this name doesn't exist OR the department has been disabled)
    // suppress the Chat channel as the targeted department is offline
    zE('webWidget', 'updateSettings', { webWidget: { chat: { suppress: true }} });
    }

     

    ==========

     

    Hi Chris,

    not sure I understand the task.

    Do you want to localise the widget + set the right department based on the the current page locale?

     

     

     

     

     

     

     

    0
  • Sergiu Birca
    Zendesk team member

    Hi Jeremy,

    ok, so this is what I suggest:

    =1=

    We will not use the triggers to set the department (it will be done via the script only);

    =2=

    We can not "disable" a trigger. We can only ensure that due to certain conditions, the trigger will not be applied to a chat session;

    =3=

    We will use tags instead. Via the following script, we will check the following conditions:

    a) if the Department1 status is online => add tag1

    b) if the department1 status is offline => check Department2 status.

    c)  if the Department2 status is online => add tag2

    d) if Department2  if offline => potentially hide or disabled (grey out) the 'Chat Now' button (you will have to build this yourself)

     

        $zopim(function() {

            setTimeout(function() {

                    $zopim.livechat.setOnConnected(function() {

                        var department_status = $zopim.livechat.departments.getDepartment('Department1');

                        if (department_status.status == 'offline') {

                            var department_status_2 = $zopim.livechat.departments.getDepartment('Department2');

                            if (department_status_2.status == 'offline') {

                                // hide the Zopim Widget or set the account to offline

                                //$zopim.livechat.hideAll();

                                $zopim.livechat.setStatus('offline');

                            } else {

                                // The department is Online (At least one agent in that department is Online)

                                $zopim.livechat.addTags("dept1_chat_redirected");

                                $zopim.livechat.departments.filter('');

                                $zopim.livechat.departments.setVisitorDepartment('Department2');

                                $zopim.livechat.theme.reload();

                            }

                        } else {

                            $zopim.livechat.addTags("dept1_chat");

                            $zopim.livechat.departments.filter('');

                            $zopim.livechat.departments.setVisitorDepartment('Department1');

                            $zopim.livechat.theme.reload();

                        }

                    });

                }, 3000) //3 secs

        });

     

     

    =4=

    Have 2 triggers in Chat:

    The critical condition in the first trigger: Visitor tag contains:  dept1_chat

    The critical condition in the second trigger:Visitor tag contains:   dept1_chat_redirected

     

     

    As a result of the above, the first trigger will be applied only if via the script, you will have the confirmation that the Dept1 is online (the dept1_chat tag is present), while the second trigger will work only if Dept1 is offline and Dept2 is online(the dept1_chat_redirected tag is present).

     

    Hope it helps.

    0
  • Sergiu Birca
    Zendesk team member

    Hi Chris,

    this is the problem:

    if (!zE('webWidget:get', 'chat:isChatting'))

     

    To just clarify a few things:

    Up until recently, the customers that wanted to offer the chat channel part of the Web Widget had to use a combination of Web Widget APIs and Chat Javascript APIs:

    https://api.zopim.com/files/meshim/widget/controllers/LiveChatAPI-js.html

    and the code would have looked like this:

    zE(function() {
    $zopim(function() {
    // the chat APIs

    });
    });

     

    ~ 2 months ago, as part of the Web Widget APIs, we released most of the endpoints that would help you perform manipulations with the chat area of the widget. We call this "Integrated Chat":

    https://chat.zendesk.com/hc/en-us/articles/360002088088--draft-Chat-in-the-Web-Widget-Reference

    As a result, we don't have to use the Chat JS APIs anymore, as most of those APIs can be found as part of WW:

    https://developer.zendesk.com/embeddables/docs/widget/core

    To be able to use the new chat APIs part of the WW, you have to enable the

    Integrated Chat (Limited Availability)

    option in the Web Widget settings.

     

    To summarize:

    if Integrated Chat is enabled => use the chat APIs part of the Web Widget APIs (I strongly recommend this approach)

    if Integrated Chat is disabled => use a combination of Web Widget APIs + Chat JS APIs.

     

    In your case, if you are targeting the isChating state + if you have the Integrated chat disabled =>

    https://api.zopim.com/files/meshim/widget/controllers/LiveChatAPI-js.html#isChatting

    if I were you, I would refactor the code to ONLY use the Chat APIs part of the Web Widget (Integrated chat is ON). In a previous comment (when I replied to Mike), I shared an example of how this could be done via the new APIs.

     

     

    0
  • Nathan Miller

    Hi Sergiu,

    This solution is terrific; however, we've run into one problem I'm hoping you can help us resolve.  When we use the method described to create a Support department, when all Support agents go Invisible the chat button does go away as it's supposed to.  However, if anybody is currently in a chat with an agent and all agents are invisible the current chat window for the visitor also disappears.

    We want to accommodate the end of the day when agents are going home, so they can go invisible, so no new chats come in, and finish their current chats.  Is there a method you see to accomplish this?

    We did already try changing $zopim.livechat.hideAll(); to $zopim.livechat.button.hide(); but this caused other problems (the contact form pops up on every single page load for visitors when we do that.

    Your help is much appreciated.

    UPDATE!

    It looks like we figured out a way to make this work.  We just wrapped the hideAll() within isChatting().  Example:

    if ($zopim.livechat.isChatting() == false) {
    $zopim.livechat.hideAll();
    }
    0
  • Mike Sutton

    Is there equivalent functionality using Unified Web Widget Syntax to set the widget to Offline status.

    $zopim.livechat.setStatus('offline');
     
    My intent is that live chat not be available on same pages depending on a specific department's status. (e.g., If this is a sales page, don't offer "Live Chat" if only the Service Department is online. And if this is a support page, don't offer "Live Chat" if only the Sales Department is online.)
     
    How can I accomplish this using the Unified Web Widget Syntax, which is otherwise working well for me.
    0
  • Jeremy Pinson

    Thanks for your reply, Sergiu. In our current configuration, the widget is hidden by default and users can start a chat by clicking a 'Chat Now' button in our header. We have also created a proactive chat trigger to fire only on a specific page after 30 seconds. It is configured to initiate a chat session with a specific department called 'Professional' however, when Professional is offline the trigger still fires and a chat session starts but the user has nobody to talk to. So, we need a way to disable the trigger on this specific page if the Professional department is offline... and still allow the user to initiate a chat session with any other agent as it currently works.

    0
  • Sergiu Birca
    Zendesk team member

    Hi Chris,

    Why not use the URL for this? All the HC pages contain /hc/locale_code. If a visitor changes the HC locale, the URL changes as well.

    You could potentially do it in this way:

    if ( window.location.pathname === "/hc/en-us" )
    {

    // perform all the operations part of this locale like set the department, set titles, etc

    }

    else if ( window.location.pathname === "/hc/fr" )

    {//etc}

     

    2 Notes:

    1. Potentially avoid performing certain updates if the user is already chatting:

    https://api.zopim.com/files/meshim/widget/controllers/LiveChatAPI-js.html#isChatting

    2. if you plan to add tags, make sure you remove the "old" ones if they are related to a specific locale that is not currently selected.

     

     

     

    0
  • Nicole S.
    Zendesk Community Team

    Hi Mandvi Singh

    Devan has pinged Sergiu Birca, but we'll need his expertise for this issue. It's not an area of the product either Devan or I are well versed in. But we are working to get your question answered either by Sergiu or someone else who has more expertise in this area!

    0
  • Sergiu Birca
    Zendesk team member

    Hi Rameez,

    Lets take this offline. I will send you an email.

    0
  • Jeremy Pinson

    Hi Sergiu,

    I was sent here by a zopim tech but it doesn't really answer our question. We are trying something similar except we're triggering a proactive chat session. So, our trigger fires and sets a department however, we need to disable the trigger if nobody from the department is online. What currently happens is that if there is nobody online, the trigger still fires and the user ends up trying to chat to nobody. So we still want the user to be able to open a normal chat and talk to our other agents... just not run the proactive chat trigger that goes to a specific department. Hope you can help!

    0
  • Chris Boyd

    Hi Sergiu,

    Yes - the same functionality as the code supplied on this page, basically to:

    - Set the visitor department based on the country of page they are on
    - Set 'offline' departments as unavailable for chat
    - Hide the department field from the chat form

    The code on this page works when you have separate pages for each department (as we do on our website) but it does not work on the Help Centre, where the language is selected from the dropdown based on the value 'current.locale.name' in the header template.  e.g. I can't code the widget to set Department 'Australia' here as it will set the Department to Australia for ALL Help Centre pages.  Hope this explains it better. 

    Thanks for the help!

    0
  • Sergiu Birca
    Zendesk team member

    Hi Jeremy,

    You can not identify the status of the department via triggers. It can only be done via a script that uses the $zopim.livechat.departments.getDepartment  endpoint.

     

    Can you pls explain  in more details your use case:

    - Do you want to display the chat widget on the page automatically for all the users at the page load and at the same time if cernain conditions are met, fire a proactive trigger?

    OR

    you want the widget to be hidden by default and display it to specific customers at a specific time only as a result of a proactive trigger.

     

    - When you say "our other agents" => is the task to:

      - try to allocate the chat to a specific department first, and then, it that dept is offline, allocate it to a different department?

     

     

     

    0
  • industry

    Hi Armi,

    Thanks for the quick reply and the code. I tried the code, but the chat doesn't show the offline form even though it is just the other department, which is online.

    If I console.log I get "undefined" for department_status.status. Somehow this function is unable to get this value. I think this is the root of the problem. I re checked and can confirm that both departments are enabled and the department is spelled right (case sensitive).

    The setStatus function works fine, without the conditions.

     

    Do you have any suggestions?

    Thanks a lot,

    An

     

    0
  • Chris Boyd

    Hi Sergiu/anyone,

    I can get this working on regular pages but it won't work in the Help Center.

    We have one chat department for each locale we offer chat and we can control the department status by using the code above on each of our dedicated language pages.  However, this isn't possible in the Help Center template, as it's the one template for all languages, set via 'current.locale.name'.  Do you have a solution for this?

    Thanks

    0
  • Charles
    Hi Sergiu, what if I don't want to make the widget disappear when the team is offline but switch to the offline "Leave a message" form ? Thanks for your advice. PS : I'm a Zendesk Enterprise user (not a Zopim standalone user).
    0

Please sign in to leave a comment.

Powered by Zendesk