Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Sarapulov/9125045a5dee420e08936517ee3c336c to your computer and use it in GitHub Desktop.
Save Sarapulov/9125045a5dee420e08936517ee3c336c to your computer and use it in GitHub Desktop.
Zendesk Web Widget Chat departments filtering v 1.0.0
/*
Zendesk Web Widget Chat departments filtering v 1.0.0
Script contains of 2 parts:
1. "departments_config" variable that contains JSON configuration
2. Executable JavaScript
departments_config JSON structure (example):
{
// Department selector will be preset with this value
"default_department":"Workday & Other HR Tools",
// Departments listed here will always be visible regardless of user tags
"always_show": [
"Development & Performance",
"Learning as a Lifestyle",
"Incentives",
"Workday & Other HR Tools"
],
// if Any of tags is presented on user profile the Web Widget will show listed departments
"show_per_country": [
{
"tags": [
"sweden"
],
"departments_to_show": [
"Sweden - Employment related questions",
"Sweden - Recruitment",
"Sweden - Pay & benefits",
"Sweden - Other HR questions"
]
},
{ ... }
]
}
Related docs:
- https://developer.zendesk.com/embeddables/docs/widget/settings#departments.select
*/
// PART 1 - "departments_config" variable that contains JSON configuration
var departments_config = {
"default_department":"Workday & Other HR Tools",
"always_show": [
"Development & Performance",
"Learning as a Lifestyle",
"Incentives",
"Workday & Other HR Tools"
],
"show_per_country": [
{
"tags": [
"admin"
],
"departments_to_show": [
"Latvia - Employment related questions",
"Latvia - Recruitment",
"Latvia - Pay & benefits",
"Latvia - Other HR question",
"Sweden - Employment related questions",
"Sweden - Recruitment",
"Sweden - Pay & benefits",
"Sweden - Other HR questions",
"Norway - Employment and related questions",
"Norway - Recruitment",
"Norway - Pay & benefits",
"Finland - Employment related questions",
"Finland - Recruitment",
"Finland - Pay & benefits",
"Finland - Other HR question",
"Czech Republic - Employment related questions",
"Czech Republic - Recruitment",
"Czech Republic - Pay & benefits",
"Czech Republic - Other HR question",
"Poland - Employment related questions",
"Poland - Recruitment",
"Poland - Pay & benefits",
"Poland - Other HR question",
"India - Employment related questions",
"India - Recruitment",
"India - Pay & benefits",
"India - Other HR question",
"India - Competency Development"
]
},
{
"tags": [
"latvia"
],
"departments_to_show": [
"Latvia - Employment related questions",
"Latvia - Recruitment",
"Latvia - Pay & benefits",
"Latvia - Other HR question"
]
},
{
"tags": [
"sweden"
],
"departments_to_show": [
"Sweden - Employment related questions",
"Sweden - Recruitment",
"Sweden - Pay & benefits",
"Sweden - Other HR questions"
]
},
{
"tags": [
"norway"
],
"departments_to_show": [
"Norway - Employment and related questions",
"Norway - Recruitment",
"Norway - Pay & benefits"
]
},
{
"tags": [
"finland"
],
"departments_to_show": [
"Finland - Employment related questions",
"Finland - Recruitment",
"Finland - Pay & benefits",
"Finland - Other HR question"
]
},
{
"tags": [
"czech"
],
"departments_to_show": [
"Czech Republic - Employment related questions",
"Czech Republic - Recruitment",
"Czech Republic - Pay & benefits",
"Czech Republic - Other HR question"
]
},
{
"tags": [
"poland"
],
"departments_to_show": [
"Poland - Employment related questions",
"Poland - Recruitment",
"Poland - Pay & benefits",
"Poland - Other HR question"
]
},
{
"tags": [
"india"
],
"departments_to_show": [
"India - Employment related questions",
"India - Recruitment",
"India - Pay & benefits",
"India - Other HR question",
"India - Competency Development"
]
},
{
"tags": [
"australia"
],
"departments_to_show": []
},
{
"tags": [
"austria"
],
"departments_to_show": []
},
{
"tags": [
"brazil"
],
"departments_to_show": []
},
{
"tags": [
"canada"
],
"departments_to_show": []
},
{
"tags": [
"china"
],
"departments_to_show": []
},
{
"tags": [
"denmark"
],
"departments_to_show": []
},
{
"tags": [
"estonia"
],
"departments_to_show": []
},
{
"tags": [
"france"
],
"departments_to_show": []
},
{
"tags": [
"germany"
],
"departments_to_show": []
},
{
"tags": [
"lithuania"
],
"departments_to_show": []
},
{
"tags": [
"malaysia"
],
"departments_to_show": []
},
{
"tags": [
"netherlands"
],
"departments_to_show": []
},
{
"tags": [
"russia"
],
"departments_to_show": []
},
{
"tags": [
"serbia"
],
"departments_to_show": []
},
{
"tags": [
"ukraine"
],
"departments_to_show": []
},
{
"tags": [
"uk"
],
"departments_to_show": []
},
{
"tags": [
"usa"
],
"departments_to_show": []
}
]
};
// PART 2 - Executable JavaScript
$(document).ready(function() {
(function(config){
'use strict';
// configuration JSON file is mandatory
if (!config) {
console.log('Unable to initialise Web Widget department logic as configuration is missing or broken. Check config file and try again.');
return false;
}
// Helper functions to handle Arrays
var ulils = {
intersection: function(array1, array2) { // return Arrays intersection
return array1.filter(value => -1 !== array2.indexOf(value));
},
uniq: function(a) { // return de-duplicated Array
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
},
compact: function(arr) { // return Array without falsy values
return arr.filter(Boolean);
}
};
// get user tags Array from Help Center object
var user_tags = (HelpCenter && HelpCenter.user && HelpCenter.user.tags) ? HelpCenter.user.tags : [];
var departments_to_show = [];
// loop through configuration to get array of departments that contain tags from the current user
for (var i = 0; i < config.show_per_country.length; i++) {
var item = config.show_per_country[i];
if (ulils.intersection(user_tags,item.tags).length) departments_to_show = departments_to_show.concat(item.departments_to_show);
}
// join Array configs and clean the result
departments_to_show = departments_to_show.concat(config.always_show);
departments_to_show = ulils.uniq(departments_to_show);
departments_to_show = ulils.compact(departments_to_show);
// Web Widget API call to filter departments in Web Widget
zE('webWidget','updateSettings',{
webWidget: {
chat: {
departments: {
enabled: departments_to_show,
select: departments_to_show && departments_to_show.length ? config.default_department || departments_to_show[0] : false
}
}
}
});
})(departments_config);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment