Performancing Metrics

I’ve implemented Performancing Metrics on this blog. I used the following code:

1        {capture name=cats}{the_category_rss}{/capture}
2        <script type="text/javascript">
3            z_post_title="{single_post_title}{single_cat_title}{single_month_title}";
4            z_post_category="{$smarty.capture.cats|strip_tags|strip}";
5        </script>
6        <script id="stats_script" type="text/javascript" src=""></script>

Instead of the code given by the system.

Performancing Stats for Blogsome

This post is a Blogsome specific post with information gleaned from HOWTO: Wordpress Visitor Count Plugin Using Metrics API. If you aren’t using Blogsome, then you’ll be better off visiting there. If you are using Performancing Metrics as your system for tracking usage patterns, you may wish to have a counter of some sort. It’s possible to do this, safely and securely with just a little bit of Smarty. First, you’ll need to get your current Performancing Authorisation Key. This will not change unless you change your password, so keep that in mind if this stops working at some stage… To get your API key, you’ll need to visit a URL like the following, but with your username and password.;=some+user&pwd;=mypass If you put this into a Firefox address bar, it will display the XML, but the only part we need is the part inside the <auth> tags. Don’t give this to anyone, else they can see all of your Performancing stats. You can now use this key to get some stats. As a test, we’ll get the basic stats of your blog. Copy and paste the URL below, and insert it into the address bar. Then, replace KEY with your authorisation key, and schinckel with your blogsome ID:;=KEY&blog;

You’ll be presented with some XML that looks like:

 1    <response>
 2        <request>
 3            <auth>KEY</auth>
 4            <blog_domain></blog_domain>
 5            <action>getvisitorstats</action>
 6        </request>
 7        <ChartURL>
 9        </ChartURL>
10        <Visits>20</Visits>
11        <AverageVisitsPerDay>20.0000</AverageVisitsPerDay>
12        <AverageVisitLength>517.9500</AverageVisitLength>
13        <RepeatVisitors>0</RepeatVisitors>
14    </response>

You can then use the ChartURL to see a nice graph, something like: Performancing Stats If this doesn’t work, then check your API key, and all of the URLs you’ve typed in so far. Now, let’s imagine we just want a counter for total blog views. The URL will need to look a bit like (I’ve broken it onto seperate lines first to help you understand it): &auth;=KEY&blog;;=date_range &d1;=start_date&d2;=end_date We’ll look at how to get the current date a bit later: for now, put a date at some point in the future. Mine, sans the authorisation key was:;=KEY&blog;;=date_range&d1;=2006-01-01&d2;=2007-01-01 Which generated the following XML:

 1    <response>
 2        <request>
 3            <auth>KEY</auth>
 4            <blog_domain></blog_domain>
 5            <action>getvisitorstats</action>
 6        </request>
 7        <ChartURL>
 9        </ChartURL>
10        <Visits>9204</Visits>
11        <AverageVisitsPerDay>271.3529</AverageVisitsPerDay>
12        <AverageVisitLength>289.5818</AverageVisitLength>
13        <RepeatVisitors>242</RepeatVisitors>
14    </response>

It’s possible to use the {fetch file="URL" assign="stats"} to grab this data and store it in a variable, so the whole lot isn’t displayed. You may have noticed that the Auth Key is included in the response! Now, the only bit we are really interested in is the

1    <Visits>9204</Visits>

section, so we need some way to extract this. Smarty allows for Regular Expressions, and we want to use one that will match everything up to and including _, and discard this. We'll want to repeat that with _ onwards. Due to a limitation, we’ll need three regexes, since there are newlines in there that much with the simlest method.

1    {$stats|regex_replace:"/[\r\t\n]/":""|regex_replace:"/.*<Visits>/":""|regex_replace:"/<\/Visits>.*/":""}

Thus, my final data is:

<div class="highlight"><pre><code class="html+smarty"><span></span><span class="linenos">1</span>    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{</span><span class="nf">fetch</span> <span class="na">file</span><span class="o">=</span><span class="s2">&quot;;auth=KEY&amp;;m=date_range&amp;d1=2006-01-01&amp;d2=2007-01-01&quot;</span> <span class="na">assign</span><span class="o">=</span><span class="s2">&quot;stats&quot;</span><span class="cp">}</span> <span class="linenos">2</span>        <span class="cp">{</span><span class="nv">$stats</span><span class="o">|</span><span class="na">regex_replace</span><span class="o">:</span><span class="s2">&quot;/[\r\t\n]/&quot;</span><span class="o">:</span><span class="s2">&quot;&quot;</span><span class="o">|</span><span class="na">regex_replace</span><span class="o">:</span><span class="s2">&quot;/.*&lt;Visits&gt;/&quot;</span><span class="o">:</span><span class="s2">&quot;&quot;</span><span class="o">|</span><span class="na">regex_replace</span><span class="o">:</span><span class="s2">&quot;/&lt;\/Visits&gt;.*/&quot;</span><span class="o">:</span><span class="s2">&quot;&quot;</span><span class="cp">}</span> Post views. <span class="linenos">3</span>    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span> </code></pre>


Of course, that’s with a hard-coded finish date. What if we want to finish on today? Again, there’s a way to do it in $smarty: {$|date_format:"%Y-%m-%d"} To add this to the end of the URL, use:

1    {assign var="URL" value=""}
2    {assign var="today" value=$|date_format:"%Y-%m-%d"}
3    {fetch file=$URL|cat:$today assign="stats"}

It’s also simple to get a current day’s post hit count, using single_date instead of date_range. I’ll leave that one up to you.

Don't Dirty Stats with your Visits

I often spend a heap of time tweaking stuff on my blog, and these show up as hits in the Referrers list, as well as on Performancing Metrics, a system of tracking readership. Now, it’s rather simple to stop your visits counting towards your stats. First, visit your site, and ensure you are logged in. In your address bar at the top of your browser, type in javascript:document.cookie, and press return. Find the cookie entry that starts with wordpressuser_, and copy the garbage-looking string after it - this is the md5 hash of your server name - to the clipboard. Find the location of your stat-gathering code. Before it starts, enter: {if $smarty.cookie.wordpressuser_md5string != "username"} and after it ends, enter: {/if} Make sure to replace md5string with your md5 hash! • This could also be used to have any text show/not show when you visit your own blog. Like Google AdSense ads, for instance.

Better Performancing Stats

This is a Blogsome specific version of the post that can be found at Hack Metrics for More Detail. The default script that I modified to generate Performancing Metrics stats for my Blogsome blog has some pretty serious limitations. The main one is that there is no differentiation between single posts, and the various types of archive pages that can be view: Category, Author, and Date (which includes Yearly, Monthly and Daily archive pages). To overcome this, I’ve come up with the following rationale:

  • Test to see if it’s a Page. Set the title to the Page title, and the category to “Page”.
  • Test to see if it’s the Homepage. If so, the title should be “Homepage”, as should the category. I’ve also been thinking about whether to append the title of the newset post, which would then enable some idea of how often the front page is viewed with particular posts. Then, using the category “Homepage” would enable me to still see how many homepage views there are in total. This is turning out to be harder than expected, due to some apparent malfunctioning of {rewind_posts}.
  • Test to see if it’s a category page. This should have the title and the category both set to the category name.
  • Test to see if it’s a date archive page. If it is, set the category to Archive, and the title to the date. Be a bit clever, and have different titles for Monthly, Daily and Yearly archives.
  • Test to see if it’s a search page. If so, set the category to Search, and the title to the search terms, prefixed by Search:.
  • Finally, it must be a single post page. Set the title to the post title. This is where the system breaks a little, as there doesn’t seem to be the ability to have multiple categories. This seems to be a limitation of performancing metrics.

Anyway, here’s the code:

 1        <script type="text/javascript">
 2            {if $smarty.server.SCRIPT_NAME == "/wp-inst/pages"}
 3                z_post_title="{single_post_title}";
 4                z_post_category="Page";
 5            {elseif $smarty.server.REQUEST_URI == "/"}
 6                z_post_title="Homepage"; // ({rewind_posts}{the_title}) {* Fix - gets last post title, not first *}
 7                z_post_category="Homepage";
 8            {elseif $smarty.server.REQUEST_URI|truncate:10:"":1 == "/category/"}
 9                z_post_title="{single_cat_title}";
10                z_post_category="{single_cat_title}";
11            {elseif $smarty.request.s != ""}
12                z_post_title = "Search: {$smarty.request.s}";
13                z_post_category = "Search";
14            {elseif $ == ""} {* Date Archive Page *}
15                {if $ != ""} {* Daily Archive *}
16                    z_post_title="{$}{single_month_title prefix=' '}";
17                {elseif $smarty.request.monthnum != ""} {* Monthly Archive *}
18                    z_post_title="{single_month_title prefix=' '}";
19                {else} {* Must be a Yearly Archive then! *}
20                    z_post_title="{$smarty.request.year}";
21                {/if}
22                z_post_category="Archive";
23            {else} {* Single Post, or unknown *}
24                z_post_title="{single_post_title}";
25                {capture name=cats}{the_category seperator=","}{/capture}
26                {assign var=cats value=$smarty.capture.cats|strip_tags:false|replace:', ':'","'}
27                z_post_category=Array("{$cats}");
28                z_post_category="{$smarty.capture.cats|strip_tags:false}"; //Remove _this_ when performancing is fixed.
29            {/if}
30            //z_user_name=_"{$}"_;
31            //z_user_email=_"{$smarty.capture.author_email}"_;
32        </script>
33        <script id="stats_script" type="text/javascript" src=""></script>

This may change at any time, as I tweak it and so on. Visit my Template page for the most recent version (click on Main Page, and then scroll down to the bottom).