Since I came across it about a year ago via a posting on Hacker News, I’ve been a big fan of the Chartbeat real-time analytics service. Last summer, I posted on their official blog about a hack I had come up with to generate top ten most viewed story lists from their API: Unlike conventional analytics platforms, Chartbeat doesn’t provide cumulative stats. Instead, their API returns “snapshots” of how many people are actually sitting on pages when you request the data. I wrote a PHP/SQLite script called stats_combiner that runs on a cron and collects and combines this live data. Every hour, it pushes out its flattened plain HTML list of “top ten stories.”
Since then, I’ve gotten a number of requests to open-source or otherwise describe this solution. So, I rewrote the script in Ruby and have added a number of features since the original PHP implementation. It’s now available as a gem, and TPM’s first official open source release.
One of the coolest differences between the gem and the original version is its extensibility. I found I had to keep changing the original script to modify data chartbeat was producing before I could surface it on the live site, such as accounting for subdomains, stripping out query strings and excluding certain records like landing pages and other “non-story” pages. The new version of stats_combiner has a Filterer class for setting these rules. Check the README for more about how it works. If you run a content site, this is the easiest way to set up a top ten widget.
Install it with
gem install stats_combiner.
Additional shameless plug: stats_combiner 0.0.3 now comes with my brand new chartbeat gem which makes it super easy to access all of the Chartbeat API endpoints. With the new chartbeat gem, getting a list of the top ten pages readers are perusing right now is two lines of code:
c = Chartbeat.new :host => 'yourdomain.com', :apikey => 'yourkey' c.toppages :limit => 10
To get the chartbeat gem, run
gem install chartbeat.