Implementing an RSS hit counter

Jul 28 2004

Since setting up Shaun Inman’s Shortstat for this site, hits to all blog entries, articles and pages are recorded for later analysis.

The only exception to this is the RSS feed; as it is generated automatically by my blogging software as an XML stream, there was no way to add the Shortstat tracking code, so I have now rectified this by replacing the direct link with a PHP script that writes the hit to the Shortstat database, before serving the XML.

<?php
@include_once($_SERVER["DOCUMENT_ROOT"]
."/shortstat/inc.stats.php");
$name = "/?rss=1&section=blog";
$fp = fopen($name, "r");
header("Content-Type: text/xml");
fpassthru($fp);
exit;
?>

Feel free to use this on your own site (if you’re not using Shortstat, replace the first line with your own tracking code).

One side-effect of adding RSS hit tracking is that the number of hits will increase dramatically due to blog aggregators hitting the site each time they are used. I will add an RSS-filter to Shortstat to counter this effect (and also maybe add an RSS-only filter, to allow me to isolate RSS accesses).

Could anyone who is subscribed to my RSS feed please update their link to http://www.thewatchmakerproject.com/rss (yes, that means both of you!)

Please let me know if any problems are experienced when using this new feed.

UPDATE : As pointed out by Stuart over at The Bomb Site, if your RSS feed is already a PHP file (which Wordpress’ is), you don’t need to use the above; simply add the tracking include to the top of your existing feed file.

Filed under: The Site, Design.

Digg this article

Bookmark this article with del.icio.us

Previously: Half sunk, a shatter'd visage lies

Next: Images off/CSS on Image Replacement


Comments

Stuart
2853 days ago
Will this work for any feed, I’m thinking WordPress here? And why have you changed your feed addy? Is this a necessity?
#1
Matthew Pennell
2853 days ago
Stuart:

As far as my knowledge of RSS feeds goes, yes, it should work for any feed.

An RSS feed is just an XML file – it won’t have any features specific to particular blogging systems (this is how sites like Bloglines can interpret everyone’s different feeds).

I had to change the URL of my feed to point to the new PHP file, so that hits could be recorded – it wouldn’t be possible to add the tracking code to the XML file itself, it had to go through a file that is parsed as PHP (although the new URL is just /rss, I’ve got my .htaccess set up to parse that file as PHP).

Hope that helps – feel free to email me if you need a hand with anything.
#2