Skip to content

Instantly share code, notes, and snippets.

@dineshprabu-freshdesk
Created October 24, 2016 07:01
Show Gist options
  • Save dineshprabu-freshdesk/e56a61bf2d4c48f29aadbea9084ab76a to your computer and use it in GitHub Desktop.
Save dineshprabu-freshdesk/e56a61bf2d4c48f29aadbea9084ab76a to your computer and use it in GitHub Desktop.
Solution Articles Tracker.
//To the Beginning of page layout - Functions for cookie access
<script>
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(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;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
//Adds Solution IDs based on the user_id, if different user-id clears cookie an proceeds.
function addSolutionID(sid, user_id){
var existingCookie = readCookie('s_visited');
if (existingCookie == null){
createCookie('s_visited', btoa(JSON.stringify({'sids':[sid.toString()]})));
createCookie('s_uid', btoa(user_id));
}
else{
if(user_id == atob(readCookie('s_uid'))){
existingIDList = JSON.parse(JSON.parse(atob(existingCookie)).sids);
if(existingIDList.length == 5){
existingIDList.shift()
}
existingIDList.push(sid.toString())
existingIDList = existingIDList.uniq()
createCookie('s_visited', btoa(JSON.stringify({'sids':existingIDList})))
}
else{
eraseCookie('s_visited');
eraseCookie('s_uid');
addSolutionID(sid, user_id)
}
}
}
function getSolutionIDList(){
var existingCookie = atob(readCookie('s_visited'))
if (existingCookie == null){
return []
}
else{
return JSON.parse(JSON.parse(existingCookie).sids);
}
}
</script>
//To the End of Page Layout - For adding a cookie on every hit on solution articles.
<script>
jQuery(document).ready(function(){
var current_user_id = "{{portal.user.id}}"
if(current_user_id == ""){
current_user_id = "no_login";
}
jQuery('section.article-list li div.ellipsis a').livequery(function(){
jQuery('section.article-list li div.ellipsis a').on('click',
function(){
addSolutionID(jQuery(this).attr('href').split('/').pop(), current_user_id.toString());
});
});
jQuery('div.article-title a').livequery(function(){
jQuery('div.article-title a').on('click',
function(){
addSolutionID(jQuery(this).attr('href').split('/').pop(), current_user_id.toString());
});
});
});
</script>
//Add this to new ticket Page
<script>
jQuery('label:contains("Visited Articles")').parent().hide()
jQuery(document).ready(function(){
var addToTicket = getSolutionIDList().uniq().join(',');
jQuery('label:contains("Visited Articles")').parent().find('textarea').val(addToTicket.replace(/\n/g, ""));
})
</script>
//Use FreshApp on the Admin side.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment