Using Campaigns to track Category Popularity in WordPress and Google Analytics

universal analytics

In my last post, I discovered a means of tracking WordPress categories by dynamically passing the category names in the script code for Google Analytics. The problem with the approach is that each time you instantiate the tracking function, it results in a page view. So, if you have multiple categories identified, you wind up executing multiple page views. Yuck!

So I did some digging and identified that you can set up a campaign in Google Analytics and capture the category names as keywords for that campaign. With some minor changes in the code, you can easily build a campaign, called “Category”.

Modifying your Analytics Code

Within your site’s footer, you will find your Google Analytics script tag:

<script type="text/javascript">
_uacct = "UA-xxxxxx-x";

And replace that with this code (be sure to substitute your UA- code in lieu of the xxxxxx-x):

<script type="text/javascript">
_uacct = "UA-xxxxxx-x";
<?php if (have_posts()) { while (have_posts()) : the_post(); if($post==$posts[0]) { ?> _uccn="category"; _ucsr="post"; _ucmd="request"; _ucct="1.0"; <?php $kwd = ""; foreach((get_the_category()) as $cat) { $kwd = $kwd.$cat->cat_name.","; } ?> _uctr="<?php echo $kwd; ?>"; <?php } endwhile; } ?> urchinTracker(); &lt/script>

You must now set up a custom filter for your campaign! Within a couple days, you’ll be able to track campaigns in Google Analytics! The campaign name will be “category”, the ad source will be “post”, the ad type will be “request” and the version will be “1.0”!

Google Analytics Campaigns


  1. 1
  2. 2



    …all required or can I get away with just adding a campaign name and campaign terms?


  3. 3

    Thanks Doug, I’m trying to follow your instructions but I’m getting lost at creating the custom filter. How did you set yours up?

    • 4

      Hi Fred,

      I’m still fine-tuning and have changed quite a few things in my filters since writing this. You won’t believe it… but I can’t remember how I set it up! (Doh!) I need to do some reverse engineering.

      Unfortunately, Google Analytics has some terrible documentation!


What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.