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 (
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

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,

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. πŸ™‚

Join the Conversation


  1. Hello there, I noticed your message on google group, so here I am. I’m developing a wordpress plugin which is a porting of original SlimStat. Well, currently the source code is totally rewritten, but the original idea still remains. Since a couple of users asked for screen resolution in wp-slimstat, I’d like to include your code in one of my next releases. Please feel free to try my plugin and send me some feedback! Have a nice day.

  2. Hi Coolmann,

    That’s fine by me – like the license says:

    There is no license, there is no warranty. Use it however you want…
    Including improving it and taking credit for the improvements.

    In fact, I looked at your WP plugin earlier, and the only reason I haven’t tried it out yet is not knowing if it’d display the new Screensize table data, and having too much other work to do to tweak it. πŸ™‚

    Let me know if you have any problems or suggestions and I’ll see what I can do.

  3. Thanks so much, I’ll work on that next week, hopefully πŸ™‚ I had a similar idea looking at awstats (i use it for tracking my job website) but now with your plugin everything is simpler now! I’m also working on some Ajax interaction… lot of new features eh eh!

  4. Hello JB,

    thanks for your work on this. I just downloaded this and the slimview plugin and now have both working on my local machine and live site.

    I’ve been wanting to track visitor browser res for a while and this looks like the answer for me.

    I would like to integrate this into Textpattern so based on the above license I shall go ahead!

    Once again thank you.

  5. It is not showing the screen size statics for me..

    i have followed the installation instructions, also installed slimview..

    any thoughts why ??

  6. I visited the page linked to your name here, and looked at the source code for your page, but didn’t see the javascript include “screensize.js” in your code. That file is necessary for visiting browsers to report back what screen resolution their using, and is probably why you’re not seeing stats.

    Look in the source code of this web page at the very bottom and you’ll see how I’ve got that include present so that your browser reports back to my server what size screen you have.

  7. Pingback:
Leave a comment

Your email address will not be published.