Improving ShortStat

Tuesday Apr 5 2005

ShortStat
Tonight I played with Shaun Inman's nice stats generator named ShortStat. I immediately loved the way it looks but concluded that I couldn't use it because Pivot doesn't allow you to include external PHP code in all pages. But I wanted !

Dark Matter Pro: a premium photoblog template.

Affiliate program available

I've thought about how to tackle this problem for a while and quite quickly concluded that Javascript was going to solve my problem. In it's original form shortstat requires you to add the following snippet of PHP code to all your pages you want to track:

<?php @include_once($_SERVER["DOCUMENT_ROOT"]."/shortstat/inc.stats.php"); ?>

Nice, but what do we do if we either cannot include PHP or if the page is a HTML page instead of a php page?

Tackling the problem



Let's take a look.

First of all you can download my version of shortstat 0.36b HERE

I eliminated the need for a PHP include by creating a dummy script that outputs a 1x1 pixel GIF file. It would be called with something like:

<img src="/shortstat/stat.php" alt="" />


It seemed nice at first. However this posed a new problem: Gone was the referrer information. In order to get it back I decided to feed the fake-gif-presenting PHP file the info it needs. Now all that was left to do was call it properly. Since javascript can pass both the referer and the originating page I achieved my goal. Shortstat can now be used on any page, it can run from a completely different webserver and therefore even do stats for sites that don't have PHP installed at all.

How to use my version of ShortStat


Step 1 is simple: install it and follow Shaun's instructions. except for including the PHP stuff in your templates. There's no need for this anymore. Instead, edit the following two files:
  • stat.php
  • stat.js
You'll find these files in the zip file. If your ShortStat installation is anywhere else than in /shortstat/ you'll have to change paths accordingly.

Step 2 is to add the following to your templates, html pages, asp scripts or whatever else you want to track:

<script language="javascript" type="text/javascript" src="/shortstat/stat.js"></script>


Of course the path should be reflecting your situation once again.

You're done now! You've got 's ShortStat without the need for PHP includes.

Look mommy, No spam!


As an added bonus this version of ShortStat is virtually spam-proof too. In order to invoke stat tracking some javascript needs to execute. bots usually just perform a simple GET operation on one or more of your pages which won't execute the . Therefore: referrer spam attempts will fail!

I hope this is useful to some. It is to me!
bookmarking

Commentary

Join the discussion! Leave a comment through the comment form below!

Got something to add to this?

Feel free to leave a comment on this site. You can use Textile and Emoticons. Your email address is only used to show a gravatar. Please stay on-topic and use common decency. Spammers will be shot in front of a live studio audience.

If you plan on posting code, use pastebin please and post a URL to the code. The comment processing doesn't deal very well with code. Sorry for the inconvenience.

Human comment spammers: don't bother posting your crap here. Comments are moderated and I won't let any of your shit through.

Remember personal info?
Yes
No

Trackbacks

If you have an interesting related post on your own site you can leave a trackback. As they say: 'a little AJAX a day keeps the spammers away' which is why you'll have to click below to generate a trackback key. The key will be valid for 15 minutes and can be used only once.

 

  • Featured Links
RockySomewhere near the Orion NebulaBookalicio.usGolden Gate BridgeThames River BankJackie and mePimpin' it