Search code examples
javascripthtmlgoogle-analyticszendeskzendesk-api

Can't get article properties from web page published in Zendesk


When I add the following in script.js in a ZenDesk template, user properties are returned successfully:

 var userRole = HelpCenter.user.role;
 console.log(userRole)

This works also

 var isAdmin = HelpCenter.user.is_admin;
 console.log(isAdmin)

However, I can't get article properties to return. For example, drawing from this documentation, the following code returns TypeError: Cannot read properties of undefined (reading 'id'):

var articleId = HelpCenter.article.id;
console.log(articleId)

And, following this documentation the following code returns TypeError: Cannot read properties of undefined (reading 'name'):

var categoryName = HelpCenter.category.name;
console.log(categoryName)

I use the first two code snippets to send the user role and admin status to custom definitions in Google Analytics 4.

I would like to send the page category to Google Analytics 4 as well.

If there is a better way to send article properties like the page category to Google Analytics 4, I would be grateful to know.

Thank you for any time.


Solution

  • The HelpCenter object you are accessing only provides information about the zendesk account and current user, it's not supposed to have any article or category property.

    The documentation you linked refers to objects that can be used in help center templates. They can't be used the way you are trying to, they are added as placeholders in your html. See this page for more info.

    In order to access article or category information you need to use Zendesk's help center api and/or add the information somewhere in the html using placeholders and then retrieve with javascript. E.g., in your article page template you could add the article id like this:

    <article id="main-content" class="article" data-articleId={{article.id}}>
    

    Then, with any script running on an article page, access it like this:

    const articleId = document.querySelector("article").dataset.articleId