Nov 182012
Article Analytics

Most blogs based on WordPress are organized in categories, and every post in the blog is assigned one or several categories. OpenAlfa blog, for instance, has categories for different programming languages, data formats, etc.

But generic tools commonly used to analyze traffic in a web site, such as Google Analytics or AWStats, do not include the concept of a WordPress “category”, and therefore do not offer by default the possibility to generate reports detailed by category.

However, Google Analytics implements “Custom Variables” and “Advanced Segments”. Combining these two functionalities, we can manage to generate reports detailed by WordPress category, as explained in this post.

Define a Custom Variable for categories.

First of all, we must identify the script in the wordpress code where the Google Analytics tracking code is placed. The location of this code is dependent on the theme, and perhaps on the plugin employed to add it to pages in the site. In the case of OpenAlfa, it is located inside the “header.php” script of our theme.

Inside this script, the code to add Google Analytics is:

Just before this code, we add some PHP code to assign to a variable “$txt_categories” the name of the category (or categories) where the post being rendered belongs:

Inside the Analytics javascript tracking code, we add the custom variable before the call to ‘_trackPageview’:

Define and Advanced Segment per Category

Now we log into Google Analytics and select “Administrator”, to the right of the upper menu. Next, we select the profile for our web site (in the sample image, “”):

Inside the profile, select the “Assets” tab, and then “Advanced Segments”. Above the list of advanced segments displayed, select “+ New Segment”:

Next, we will define an advanced segment “Perl Category” as the set of pages where the value of the Custom Variable named “Category” is “Perl”:

Fill the fields as shown in the sample image above, and press “Save Segment” at the bottom of the screen.

Once the new advanced segment has been defined, we can start using it. Select “Standard Reporting” in the upper menu, and “Advanced Segments”. In the dialog box displayed, select the “Perl Category” advanced segment just created:

Needless to say, we must wait some time, until Google Analytics has gathered enough data to start showing these traffic reports.

 Posted by at 5:43 pm

 Leave a Reply