Google Analytics and WordPress: Measuring Category Popularity

UPDATE: This methodology will pass multiple page views for a single event and may not be the right direction to go! Please check my next blog post on how to utilize Google Analytics Campaigns to Track Categories as Keywords.

After spending a few hours of analyzing my blog’s post data and combining it with the Analytics data from Google, I decided there had to be an easier way. And there is!

Google Analytics allows you to register additional data with each page that’s viewed. If you look at your code in each of your pages, it looks like this:

>script type=”text/javascript”>
_uacct = “UA-xxxxxx-x”;
urchinTracker();
>/script>

You can pass information dynamically to Google by passing the information in the urchinTracker function, like:

urchinTracker(‘Track This Information’);

With WordPress, it’s a little more complex since each page is dynamically generated. A post may belong to multiple categories so you need to register each. On a multiple post page, you probably only want to push the first posts’ Categories. On a single page, you pass whatever categories there are for that post or page. With a good template, though, all of that code resides in the footer so the code can get a little complex.

Category Code

Here’s what I came up with:

>script type=”text/javascript”>
_uacct = “UA-xxxxxx-x”;
urchinTracker();
>?php if (have_posts()) { ?>
>?php while (have_posts()) : the_post(); ?>
>?php if($post==$posts[0]) { ?>
>?php
foreach((get_the_category()) as $cat) {
echo “urchinTracker(‘/category/”.$cat->cat_name.”‘);\n”;
} ?>
>?php } ?>
>?php endwhile; ?>
>?php } ?>
>/script>

On pages with posts, the code will loop through the categories for the first post and register those categories with Google Analytics.

Google Analytics Content Drilldown

In content drilldown, I can simply click ‘category’ and I get a count of views by category… whether it’s a single post, my home page with multiple posts, or if someone actually does click on category!

Google Analytics Categories

If you copy and paste the code, be sure to clean up the apostrophes and use your own UA (Google Analytics Code) identifier!