ScreenSize for SlimStat

SlimStat is code for php/mysql websites that collects stats about visitors. It was inspired by Shaun Inman’s ShortStat project. Shaun’s gone on to Mintier things and doesn’t support ShortStat anymore. In fact, Mint grabs screen sizes – but you have to pay $30 to use it. I’m cheaper than that.

Stephen Wettone has picked up ShortStat and continued with it, and the free world rejoices.

One thing however, that hasn’t been added yet is screen sizes of the visitors it logs. I saw someone request that feature at the SlimStat Google Group and decided to try and put together a plugin that’ll do the job.

So without further ado, here it is:
Download ScreenSize for SlimStat (5kb).

It’s my first plugin, go easy on me.

How It Works

SlimStat works by calling it’s own php code on every page you’ve included
it on. For instance, I put it in my WordPress (http://wordpress.org)
footer so it gets called on every php page someone visits at my site.
I’m just greedy, I guess.

None of the information SlimStat can gather includes the Screen Resolution
of the visitor. Browsers just don’t volunteer that stuff. Lousy browsers.
So I’ve whipped together this plugin for SlimStat that sends that Screen
info to your site in a way that SlimStat can gather.

Basically, we add a new javascript include file wherever the SlimStat include
file was added. When your web page renders on the visitor’s browser (and
they have javascript enabled), this new little function detects their screen
size and draws a 1px by 1px image on the page (shouldn’t be noticeable to
them). The source (the “src” attribute) for the image is special – it
includes their screen resolution (width)x(height) in the url, and then
requests a special little php page (“x.php”) that only includes the SlimStat
code. That way, SlimStat can store the url they requested – and poof, now you
know their screen size.

But… if the screen size is in the url, and that can vary quite a bit, how
does it ever get to the right “x.php” file? Well, I’m not smoking crack.
It works: BUT your server has to honor the .htaccess file included in that
screensize directory. It tells Apache servers “Whenever anyone requests ANY
file in this directory or its descendants that ends in “x.php”, send them
THIS “x.php” file. .htaccess files are cool.

Consequently (and maybe I’m just too lazy to fix this) the image breaks.
There’s no actual image to display in their browser. Like I said, a 1px
image shouldn’t be noticeable, so I didn’t bother fixing this. Anyone is
welcome to do that, if they send me a fixed copy of the plugin. 🙂

The rest of the magic happens in the Screensize plugin that appears in the
SlimStat page. If you’re sharp, you’ve probably noticed by now that you’ll
be accumulating a lot of extra page views in your stats that report on the
screen size. That info isn’t really about a page request, so it skews your
data.

To fix that, every time you view the Screensize plugin page in SlimStat,
the requests for that “x.php” file get sucked out of the SlimStat tables and
summarized in a new Screensize table. Then the Screensize stats are shown,
naturally.

The new Screensize table I just mentioned is created any time you view the
Screensize plugin page that the table doesn’t already exist. If the
creation attempt fails, you’ll be notified that you have to create the
table yourself (standard SlimStats behavior).

Just giving credit where it’s due

I got the idea for logging screen sizes this way from experience with the AWStats log analyzer program. It grabs screen resolutions of site visitors a very similar way. I got code snippets for putting this together from a combination of PPK’s Quirksmode (cookie handling) and the Slimview plugin (plugin architecture).

If you appreciate this plugin, drop me a line to say “Hi”. You’re under no obligation to name children after me, send me donations, or Christmas ornaments, etc. Of course, I wouldn’t turn any of that down, either. 🙂

9 replies on “ScreenSize for SlimStat”