Tracking Analytics with Google Analytics 4 (GA4)
Learn how to implement custom GA4 events in your Helpjuice knowledge base to measure article engagement, search behavior and more
This guide will walk you through setting up Google Analytics 4 (GA4) custom events in Helpjuice, so you can track article page views, search interactions, and user identification all from within your Helpjuice site.
If you need to track other events, you can follow a similar approach from these examples.
1. Tracking Page Views
To record each page view within your Helpjuice instance, add the following event after your article content is rendered, or within a DOMContentLoaded
event:
gtag("event", "hj_page_view", {
page_title: document.title,
page_path: window.location.pathname
});
This enables you to build reports in GA4 based on specific page viewed, helping you measure knowledge base engagement.
2. Tracking Search Terms and Result Counts
To understand what your users are searching for and how effective your articles are at answering their queries, track each search term along with the number of results returned:
document.addEventListener("DOMContentLoaded", function() {
const searchInput = document.querySelector('#search');
if (searchInput) {
searchInput.addEventListener("change", function() {
const term = searchInput.value;
const interval = setInterval(() => {
const results = document.querySelectorAll(".search-result").length;
const noResults = document.querySelector(".no-results");
if (results > 0 || noResults) {
clearInterval(interval);
gtag("event", "hj_search", {
search_term: term,
result_count: results
});
}
}, 300);
});
}
});
This snippet captures both the search term and the number of results returned (including zero results), giving you insight into what users are looking for and whether their needs are being met.