HOWTO: Hack Metrics for More Detail

Do you know which of your posts have been viewed most? Which categories (if your blog supports them) are most popular? How about where your visitors are geographically situated?

If you use Performancing Metrics to monitor how your blog is being viewed, you do. I use them, and I’m learning a lot about my readers’ interests.

Today I’m going to look at my ‘Post Views’, i.e., which posts (articles) are attracting the most reader attention. We’ll begin by looking at the default Performancing Metrics script. I use WordPress as my blogging engine so my default script reflects the WordPress API. If you use another blogging engine (such as Drupal or Moveable Type) I’m going to assume that you can translate the concepts in this article into code for your specific blogging engine.


When I first signed-up to use Performancing Metrics, here’s the script they gave me (please note that the line numbers are for our reference only and are not part of the original script):

01    <script type="text/javascript">
02    <?php
03    global $userdata;
04    if ($userdata) {
05        echo "z_user_name=\"" . $userdata->display_name . "\";\n";
06        echo "z_user_email=\"" . $userdata->user_email . "\";\n";
07    }
08    ?>
09    z_post_title="<?php the_title();?>";
10    z_post_category="<?php $c=get_the_category();echo $c[0]->cat_name;?>";
11    </script>
12    <script id="stats_script" type="text/javascript" src=""></script>

Please notice line number 9 (“09”). That line populates a javascript variable called ‘z_post_title’ with the result of a PHP call to a WordPress function called ‘the_title()’, which returns — yes, you guessed it! — the title! You may be wondering, as I did: what title? of the page? of the post? of the archive? of the category? of the search results?

Hm… welcome to the confusion that would soon dominate my blog-metrics world. You see, WordPress’s ‘the_title()’ function returns the title of the current blog post. Which is absolutely perfect if we’re looking at an individual post. The challenge here is that not everyone who visited my blog was visiting individual posts. Some people came to the home page and read entire posts from there… others came through search engine results and went right to the post itself or, in some cases, were directed to the category archive or a monthly archive on my blog.


After using the default Performancing Metrics script for a few weeks, I found myself viewing my ‘Post Views’ panel with some surprise: posts that I thought would be more popular with my readers sometimes appeared as though they weren’t popular, while other posts that I thought would be less interesting seemed to be getting a lot of unanticipated attention.

Here are a couple of screen shots showing my first week’s Performancing Metrics using the default script for a WordPress blog:

Please notice that every post being counted in this ‘Post Views’ panel is identified only by the the post’s title, i.e., the_title(). There’s nothing at all wrong with title-only nomenclature but as we will soon see, it’s hiding some rather significant details.

It turns out that calling WordPress’s ‘the_title()’ function from any page with more than one post on it — whether the blog homepage, or a category archive, or a date archive, or even a search result — will return only the top-most post’s title on the current blog page.

As I thought more carefully about the mysteriously skewed popularity of my posts, I realized what was going on. This is a subtle point that came upon me as an ‘aha!’ moment and if you haven’t already had the same flash of insight, hold on to your hat!

Let’s say I write just 2 posts a day. I check the online news sources for interesting items. I find one and write about it, publishing to my blog sometime around 8:00am. It’s a real gem of a story and I think to myself, “Wow! My readers are gonna LOVE this!”

About 30 minutes later I find another interesting item. It’s not nearly as significant as the first post I made just a half-hour earlier but I think to myself, “This is kinda cool. It’s not as important as the first post today but maybe some of my readers will find the less-intense content refreshing.” So again I write and publish a post, and off to work I go.

At the end of the day I check my Performancing Metrics numbers on the ‘Post Views’ panel and what do I find? Only a few people seemed interested in the first post but a whole lot more seemed to visit the second post.

I feel a bit confused by these numbers. Do I not really understand my target audience? Do I think I’m writing for one audience but am really getting the attention of another altogether? It seems inconceivable to me that my readers would prefer the second, less-intense post over the first one.

But numbers don’t lie, and there before me are the cold, hard facts. How can I argue with numbers?


Well, I can’t. And upon further reflection I realized that I didn’t have to.

That first post had been the top-most post on my blog for all of about 30 minutes, while the second post had been top-most on my blog for the remainder of the day — of course! no wonder the numbers looked like they did!

Here’s the subtle but very important conclusion: every time someone visited my blog’s homepage, they would have a chance to read both stories but… the default Performancing Metrics script would capture ‘the_title()’ of the top-most post on the page and tally a view for that, and only that, post.

Both articles may’ve been read… neither one might’ve been read… perhaps only one or the other was read but I couldn’t tell from the Performancing Metrics tally because a view to my blog’s homepage was equivalent — from the Metrics standpoint — with a view of that post alone.

By now you might’ve guessed that if a reader were to have visited that second post’s page, the tally would accrue just as it had for visiting the homepage when that second post was top-most on the page. This effect was magnified even more if a day went by where I didn’t post at all: the last post from the previous day would seem to jump off the scale in popularity with every visit to my blog’s homepage.


Although I’ve made my living as a software developer since the mid 1980’s, web development hasn’t been a part of my job description. If I was going to attempt to modify my default Performancing Metrics script so that it would give me finer distinctions in the numbers it tracked, I was going to have to learn the PHP scripting language. Specifically, I was going to have to research the WordPress API functions that I’d need to call in order to get this increased granularity that I desired from my Metrics numbers.

If all that I’ve described makes sense to you so far… if you’re nodding your head up and down in agreement that you also want to benefit from this additional level of detail in your Metrics, then let me tell ya: have I got a solution for you!

It took me a few days of reading and experimenting with PHP and with WordPress function calls. I often interrupted a co-worker who was much more experienced with PHP than I was, in order to get an informed sense of direction or a quick answer to an otherwise newbie question. The result of all this time and effort is this modified version of the original Performancing Metrics script (it is a mixture of javascript and php; for those of you unfamiliar with either or both, let’s discuss questions in the comments vs. me trying to anticipate questions and answer them here; this, in order to keep the article as brief as possible — thanks in advance for your understanding!):

001    <script type="text/javascript">
002    /* --------------------------------------------------------------------------------------------
003        2006-03-31~rch: use custom title and category labels to distinguish between actual page
004        visits and accidental visits previously tracked when an archive (homepage, search, monthly
005        or daily archives, etc.) are browsed; script developed with help from I.McFadden.
007        original code:
009            z_post_title="<?php the_title();?>";
010            z_post_category="<?php $c=get_the_category();echo $c[0]->cat_name;?>";
012        new code:
013    -------------------------------------------------------------------------------------------- */
015    <?php
016        if ( is_home() ) {
017    ?>
018            // adds "Homepage (" prefix and ")" suffix to post title, and
019            // sets category tracker to custom string (in this case "~HomePage"):
020            z_post_title="Homepage (<?php the_title();?>)";
021            z_post_category="~HomePage";
023    <?php
024        } elseif ( is_single() ) {
025    ?>
026            // just prints the title all by itself while setting
027            // the category to the first one listed in the POST's
028            // category array
029            // NOTE: this is the default Performancing Metrics behavior.
030            // --- TO DO --- is there a way to list all categories assigned to
031            //               a post such that all categories, collectively, are
032            //               being tracked by Performancing ???
033            z_post_title="<?php the_title();?>";
034            z_post_category="<?php $c=get_the_category();echo $c[0]->cat_name;?>";
036    <?php
037        } elseif ( is_category() ) {
038    ?>
039            // adds "Category: " prefix and prints only the category of
040            // the PAGE (vs. of the POST); no title printed because seems
041            // irrelevant to the tracking of category views vs. specific
042            // post views:
043            z_post_title="Category: <?php the_category();?>
044            z_post_category="<?php single_cat_title();?>";
046    <?php
047        } elseif ( is_search() ) {
048    ?>
049        // adds "Search: " prefix and user's actual search term(s), while
050        // setting category to custom string "~Search":
051        z_post_title="Search: <?php echo wp_specialchars($s);?> (<?php the_title(); ?>)";
052        z_post_category="~Search";
054    <?php
055        } elseif ( is_month() ) {
056    ?>
057            // adds "Month: " prefix followed by the actual date that, for
058            // my taste, is formatted as "31 March 2006", and sets Category
059            // to "~Month":
060            z_post_title="Month: [<?php echo date("d M Y");?>]";
061            z_post_category="~Month";
063    <?php
064        } elseif ( is_day() ) {
065    ?>
066            // adds "Day: " prefix followed by the actual date that, for
067            // my taste, is formatted as "31 March 2006", and sets Category
068            // to "~Day":
069            z_post_title="Day: [<?php echo date("d M Y");?>]";
070            z_post_category="~Day";
072    <?php
073        } else {
074    ?>
075            z_post_title="(?<?php the_title();?>)";
076            z_post_category="~Unknown";
078    <?php
079        }
080    ?>
081    // end change.
083    </script>
085    <script id="stats_script" type="text/javascript" src=""></script>

In contrast with the earlier ‘Post Views’ panel we saw, here’s how Performancing Metrics now labels my blog’s numbers:

Whereas before every entry was a post title and nothing more, now these descriptors include context markers like “Homepage (” in front of the title to show, when a user viewed the homepage, which post was at the top of the page at the time. It also shows things like when a user has viewed a date-driven archive by prefixing either “Month: [” or “Day: [” in front of the post title. Similar results are obtained if the user views by category or by searching for something.

I thought that was pretty useful for my purposes. I also realize that there’s probably a lot more that one could add to this script to polish and tailor it even more so if any of you PHP or WordPress or javascript gurus want to build on this, or refine it further, I welcome your changes.

That’s all, folks! Enjoy!

11 thoughts on “HOWTO: Hack Metrics for More Detail

  1. How do I tweak your new script to work w/ Blogger? Unfortunately, I’m not the programmer that most people are. Thanks.


  2. Not a problem at all. Had I been aware of Dave’s plugin I’d have probably just dived in along side his efforts instead of re-inventing the wheel 🙂

    I always enjoy getting into the code so it wasn’t a waste at all. That said, I do feel a bit silly posting this relatively small advancement in functionality when it had already been overcome some time ago.

    Folks like you, and even Dave, have been more than gracious in their responses to me and my neophyte article and for that I’m grateful, even if a little embarrassed 🙂

    Blog on!

  3. I was going to reply about the Davester’s pMetrics plugin as well but didn’t want to rain on your hard-won parade! Let’s hope you found doing the code enjoyable for its own sake, Richard.

    I’ve been a user of the plugin from the beginning – Dave and his work are terrific.

  4. I downloaded it earlier today and am anxious to see how it works (just anxiously waiting

    One thing, Ahmed. If you’re already using the Performancing Metrics script in your WordPress template, I confirmed with Dave Reid that you need to remove that original script.

    Otherwise, you’ll be double-reporting from both the original script *and* the PMetric plugin.


  5. I’m downloading David’s PMetrics plugin right now I wasnt aware that his plugin supported this.


  6. Hi, Ahmed.

    Sounds like you had the kind of ‘aha’ realization that I had. Are you blogging with the WordPress engine or something else?

    If WordPress, you may want to have a look at David’s PMetrics plugin. It sounds to me like he’s done all that I’ve done and a whole lot more (I’m going to give it a look, too, because I can’t see working to accomplish things that his plugin has already done).

    If you’re not using WordPress, maybe you can adapt the ideas I wrote about into another blogging engine-specific implementation and share it with us?


  7. Thanks, Chris.

    I’ve been reading your posts here on and have benefitted much from them. I apologize for not taking the time to feedback to you. Going forward, I’m going to give the kind of acknowledgement and feedback (to you and others) that I’d appreciate in response to my own posts.

    Thanks again!

  8. Hi, David.

    I replied to your private message just a moment ago. I had used the blockquote tag to delineate what you wrote from my responses to you (because the ‘Formatting guidelines’ at the bottom of the ‘compose’ window said it was allowed) but when I viewed what I sent you, the blockquote formatting is missing; hope you’ll be able to make sense of what I wrote.

    As for your PMetrics WordPress plugin, I wish I’d taken notice of it earlier! It sounds like you’ve already done all that I’ve done and much, much more! Very soon I’m going to take a good look at what you’ve done with your plugin because there doesn’t seem to be any sense in me re-inventing the wheel that you’ve already tackled! 🙂

  9. All of this functionality (plus Ultimate Tag Warrior integration for tag archives, author archives, 404s, etc) has been in the PMetrics WordPress plugin since the first version. Guess I just haven’t advertised it as well as I should have.

  10. Damn…

    so THAT’s where the page views are going…hmmm.

    A good way to promote your blog too

    Now all you need to do is to create a WP plugin that inserts this script automatically (and gives the user more options) and you’ll be set to go.

Comments are closed.