Setting Up WordPress Category Tracking in Google Tag Manager for GA4

Reading Time: 5 minutes

Last updated June 2026

If you run a content-heavy WordPress website, there is a good chance you care about more than just web page views. You may also want to know which content topics actually drive sessions, engagement, and returning visitors.

That is where category tracking can help. In my case, I wanted Google Analytics 4 to capture the WordPress Category assigned to each article so I could analyze traffic by topic. This is especially useful when your content spans areas like analytics, martech, paid advertising, SEO, content marketing, and more.

The important detail is that WordPress Categories and WordPress Tags are different. If your website uses Categories as the main topic label on articles, your Google Tag Manager setup needs to pull from the Category link, not from a Tag link.

What this setup does

This approach reads the article’s visible WordPress Category from the web page, normalizes it into a GA4-friendly value, and sends it with your page_view event as a custom parameter.

For example:

Martech becomes martech

Paid Advertising becomes paid-advertising

Search Engine Optimization (SEO) becomes search-engine-optimization-seo

Artificial Intelligence (AI) becomes artificial-intelligence-ai

Why use Category tracking in GA4?

Once this is set up, you can analyze website performance by topic instead of just by URL.

This lets you answer questions like:

Which categories attract the most sessions?

Which categories drive the longest engagement time?

Which categories perform best for case studies, calculators, or other content types?

If you already have page type tracking in place, category tracking becomes even more powerful because you can compare format and topic together.

Before you start

This walkthrough presumes:

1. You are using WordPress.

2. Your article category appears visibly on the web page as a link.

3. The category link uses a URL structure containing /category/.

4. You already have Google Tag Manager installed.

5. Your GA4 page_view tag is firing through GTM.

Step 1: Confirm that your website uses Categories, not Tags

This part matters more than people realize.

If your website displays the topic label under the title and that label links to a URL like /category/martech/, then your GTM script should look for Categories.

If your setup uses WordPress Tags instead, the link would usually contain /tag/.

Step 2: Create a Custom JavaScript Variable in Google Tag Manager

In Google Tag Manager, go to Variables and create a new User-Defined Variable.

Choose Custom JavaScript as the variable type.

Name it something like:

Content Topic

Then use this script:

Some tutorials detect categories using URL patterns like /category/. That works on some WordPress websites, but many themes remove the category base from URLs. A more reliable approach is to target the category element directly in the HTML.

function() {
  var category = document.querySelector('.ast-terms-link a');
  if (!category || !category.textContent) return 'not-classified';

  return category.textContent
    .trim()
    .toLowerCase()
    .replace(/[()]/g, '')
    .replace(/\s+/g, '-')
    .replace(/[^a-z0-9-]/g, '')
    .replace(/-+/g, '-')
    .replace(/^-|-$/g, '');
}

Note: The selector .ast-terms-link a works for Astra theme. If you are using a different theme, inspect the category link on the web page and adjust the selector accordingly.

How the script works

This script does five things:

1. It looks for the first link on the web page that contains /category/.

2. It grabs the visible text of that link.

3. It trims extra spaces.

4. It converts the value to lowercase.

5. It removes special characters and replaces spaces with hyphens.

If no category is found, it returns not-classified.

This acts as a deliberate “other” bucket. Instead of mixing uncategorized traffic with GA4 labels like not set, you can clearly see what portion of your content taxonomy is missing or incomplete. Over time, the goal is not to eliminate not-classified, but to keep it at a healthy percentage.

Step 3: Add the parameter to your GA4 page_view tag

Next, open the GA4 page_view tag in Google Tag Manager.

If you have a standard page_view tag and a separate internal version, make sure you update both so the data stays consistent.

In the Event Parameters section, add a new parameter:

content_topic = {{Content Topic}}

That tells GTM to send the normalized category value with every page_view event.

Step 4: Preview the changes in GTM

Before publishing, use Preview mode in Google Tag Manager.

Open a few different article web pages and confirm that the Content Topic variable returns values you expect from your WordPress Categories.

For example, on a Martech article, the variable should return:

martech

On a Paid Advertising article, it should return:

paid-advertising

If you see a value that does not match one of your approved categories, your selector may be pulling from the wrong part of the web page.

Step 5: Publish the GTM container

Once preview mode looks good, publish the container.

At this point, GTM is sending the custom parameter to GA4, but Google Analytics 4 still needs one more step before you can use it in reports.

Step 6: Register the custom dimension in GA4

In GA4, go to Admin, then Custom definitions.

Create a new custom dimension with the following settings:

Dimension name: Content Topic

Scope: Event

Event parameter: content_topic

Save the custom dimension.

From that point forward, GA4 will store and report on the content_topic parameter.

Important note about historical data

GA4 custom dimensions are not retroactive.

That means this setup will only classify data collected after the custom dimension is created and GTM is published.

If you want historical analysis, you will need to build it manually using a spreadsheet or another reporting layer.

How to use the data in GA4

After the data starts flowing, the easiest place to analyze it is in Explorations.

A simple starting report is:

Rows: Content Topic

Columns: Page Type

Values: Sessions

This makes it easy to see which topics are driving traffic and how those topics map to different kinds of content.

Examples might include:

Case studies in martech

Book summaries in leadership

Articles in analytics

Calculators in website-related topics

Why this works well for WordPress websites

The biggest advantage of this setup is that it uses the taxonomy you already maintain in WordPress.

You are not inventing a separate analytics classification system. You are simply exposing your existing editorial structure to GA4.

That makes the reporting much easier to trust.

Can this work outside WordPress?

Yes, but the implementation details change.

The broader concept is the same: identify the topic label on the web page, extract it with GTM, normalize it, and send it to GA4 as a custom parameter.

What changes is the selector. Instead of looking for a WordPress Category link containing /category/, another platform might use a different class name, data attribute, or metadata element.

So the process is portable, but the selector is platform-specific.

Final takeaway

If your WordPress website uses Categories as the primary topic label for articles, GTM should pull from Categories and not Tags. That one detail can be the difference between clean, trustworthy GA4 topic reporting and a messy dataset you cannot rely on.

Once you set this up, GA4 becomes much more useful for understanding what your content is really doing by subject area, not just by individual URL.

Leave a Comment

Your email address will not be published. Required fields are marked *