Using Campaigns to track Category Popularity in WordPress and Google 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";
urchinTracker();
</script>

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();
</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

Most Commented Posts