Analytics & TestingContent Marketing

How To Use Google Analytics 4 Events to Track Category Popularity in WordPress

Category popularity can help you understand what content your audience finds most engaging. Tracking this data can help you tailor your content strategy and enhance user experiences. Google Analytics 4 (GA4) offers powerful event tracking capabilities, enabling you to monitor your WordPress website’s category views. In this article, we’ll explore how to implement event tracking to measure the popularity of categories in WordPress using GA4.

Why Tracking Category Popularity Matters

Understanding the popularity of categories on your WordPress site has several benefits:

  1. Content Optimization: You can prioritize content within popular categories, ensuring you cater to your audience’s interests.
  2. User Engagement: By analyzing category popularity, you can identify which topics resonate most with your users, leading to increased engagement.
  3. Targeted Marketing: This data is invaluable for tailoring your marketing efforts and advertising strategies.
  4. User Experience: Prominently promoting content from popular categories on your website can enhance the user experience (UX).

How to Track Category Popularity with GA4 in WordPress

If you’d like to track the popularity of categories that you’re writing posts for in WordPress, you can create an event that captures that data and passes it to Google Analytics 4. Here’s the code that you can add to your child theme’s functions.php file that will generate the event. You are limited to the number of categories you can capture, so I’ve added an exception for posts that are assigned more than 5 categories.

function track_category_popularity() {
  if (is_single()) { // Check if it's a single post page
    global $post;
    $post_id = $post->ID;
    $post_title = get_the_title($post);
    $categories = wp_get_post_categories($post_id);
    if (!empty($categories)) {
      $category_count = count($categories);
      $itemData = array(
        "id" => $post_id,
        "name" => $post_title,
        "category" => "category",
        "list_name" => "post",
        "list_id" => "request",
        "item_id" => "1.0",
        "item_name" => "Category",
        "item_category" => get_cat_name($categories[0]),
        "item_category2" => ($category_count > 1) ? get_cat_name($categories[1]) : "",
        "item_category3" => ($category_count > 2) ? get_cat_name($categories[2]) : "",
        "item_category4" => ($category_count > 3) ? get_cat_name($categories[3]) : "",
        "item_category5" => ($category_count > 4) ? get_cat_name($categories[4]) : ""

      // Check if there are more than 5 categories
      if ($category_count > 5) {
        $itemData["item_category"] = "Multiple Categories";
        $itemData["item_category2"] = "";
        $itemData["item_category3"] = "";
        $itemData["item_category4"] = "";
        $itemData["item_category5"] = "";

      <script type="text/javascript">
        if (typeof gtag === 'function') {
          gtag('event', 'view_item', {
            "items": [<?php echo json_encode($itemData); ?>]
add_action('wp_footer', 'track_category_popularity');

In this code:

  • We define a function named track_category_popularity.
  • Inside the function, we check if it’s a single post page using is_single().
  • We use WordPress functions to capture the post’s ID, title, and categories.
  • We create an associative array named $itemData that contains the item data, including category-related fields.
  • We check if there are more than 5 categories and set the appropriate values.
  • We output the tracking script directly in the HTML body of the page using wp_footer action hook. This script sends the ‘view_item’ event to GA4.

Tracking category popularity in WordPress using GA4 provides valuable insights for optimizing content, enhancing user engagement, and tailoring your marketing efforts. Following the steps outlined in this article, you can effectively track and analyze category views, making data-driven decisions to improve your website’s performance and user experience.

Douglas Karr

Douglas Karr is the founder of the Martech Zone and CEO of DK New Media. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.

Related Articles


Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.