Assistance with code to count specific characters in ZenDesk KB articles | Community
Skip to main content

Assistance with code to count specific characters in ZenDesk KB articles

  • January 22, 2024
  • 2 replies
  • 0 views

Tammy12

For our KB Product Release Notes - we have specific categories that we want to count automatically. I settled on the solution of hiding 'counting' characters on the sections we want to count.

The initial code I developed was in a loop that was not obvious, so I sought help to redevelop the code to this but now it does not count at all. I am convinced it's something obvious that I am missing - because this works on a normal website.  The folk that were assisting me before are not familiar with zendesk - so I thought to reach out to this community.

This is the script function:

<script>
function findOccurrences(string){
 return [...document.querySelector("div").textContent.matchAll(string)].length;
};

document.addEventListener("DOMContentLoaded",()=>document.getElementById("anm_tot").textContent=
findOccurrences('¤'));

document.addEventListener("DOMContentLoaded",()=>document.getElementById("asm_tot").textContent=
findOccurrences('§'));

document.addEventListener("DOMContentLoaded",()=>document.getElementById("akb_tot").textContent=
findOccurrences('~'));
</script>

Then within the page:

 <span id="anm_tot" class="wysiwyg-font-size-normal">&nbsp;</span> (as an example).

What the page currently shows is a result of 0 in the place a count is expected.

2 replies

Tammy12
  • Author
  • January 22, 2024

I thought I should also include that everything is contained within the outermost <div> of the page.


Brandon12
  • January 26, 2024

Hey Tammy,

Isn't working with the code fun?  This one is going to be a bit harder to troubleshoot, as you've already figured out it works on a normal website.  That said, here are some potential issues and suggestions for fixing them:

  1. Selector Scope: The document.querySelector("div") is very broad and might not be targeting the specific area of your KB articles where these characters are located. You need to ensure that the selector targets the specific container that holds your KB content.

  2. Multiple DOMContentLoaded Listeners: You're adding three separate event listeners for the same DOMContentLoaded event. It's more efficient and clearer to add only one event listener that handles all your counts.

  3. Character Escaping: Ensure that the special characters are properly escaped if necessary, especially if they have special meanings in regex (like ~).

  4. Debugging: Add console.log statements to help debug the issue. This can show you what part of the content is being read and whether the match is finding anything.

Here's a revised version of your script:

<script>
function findOccurrences(containerSelector, string) {
    const container = document.querySelector(containerSelector);
    if (!container) {
        console.error("Container not found:", containerSelector);
        return 0;
    }
    return [...container.textContent.matchAll(string)].length;
}

document.addEventListener("DOMContentLoaded", () => {
    // Adjust these selectors to target the specific parts of your KB content
    const contentSelector = '#main-content'; // Example selector

    document.getElementById("anm_tot").textContent = findOccurrences(contentSelector, /¤/g);
    document.getElementById("asm_tot").textContent = findOccurrences(contentSelector, /§/g);
    document.getElementById("akb_tot").textContent = findOccurrences(contentSelector, /~/g);
});
</script>


//In this script, replace #main-content with the selector that accurately targets the content area of your KB articles where the characters are located. The script now handles all counts in a single DOMContentLoaded event listener and logs an error if the specified container is not found, which will help in debugging.

Make sure that your span elements (<span id="anm_tot">, etc.) are also placed correctly on the page where you want the counts to be displayed.

This code also assumes that all the content you're interested in counting is within the outermost <div> of the page.

Hope this points you in the right direction!

Brandon