Jayd Saucedo

Blog

Javascript Statistics
I really haven't worked on any personal projects in quite some time. If you can think of something, based off of my previous posts, that I should be working on, then that project is probably dead. The last thing I was working on was a sort of Javascript Statistic type thing. I was working on it over winter break to give me something to do. I didn't really have much interest in the final result. I was making it because I thought it might be a useful tool for someone, while I personally had no use for it. Nevertheless, I worked on it to a point where it can actually pass off as an actual website analysis program.

So the first thing I did was consider methods in which I can tell if javascript is turned off. I thought about embedding an image in the html "noscript" tags but then I wouldn't have any views to base it off of. I eventually decided to embed an image on a page, which upon loading, would activate some AJAX to record that javascript is turned on. This way, even if the javascript didn't load, I could still tell if someone visited the page. After that I questioned how many people actually had images turned on, so I ended up making that part of my analysis.

I'm not going to post the javascript, because it's just a simple AJAX function that sends the users IP to the PHP program which then checks it against the database.
More interestingly, this is what my HTML was:

<script src="http://www.saurdo.com/js_perc.php?t=js" type="text/javascript"></script> <img src="http://www.saurdo.com/js_perc.php?t=gif" onload="js_perc()">

Unfortunately, posting HTML doesn't provide any neat colorful syntax highlighting (it might if I used a different program). So in this I execute a function called "js_perc" when my image is finished loading. On the page, which is in PHP, I have add the users IP to the database if it doesn't already exist. Before my image can finish loading the PHP that generates the image finds that users IP to the database and logs him as having images on. Once the image loads it executes my javascript function which uses AJAX to contact the PHP script with the users IP and the PHP script logs that user as having Javascript enabled! Simple enough!

Truth be told most of the fun in this script is in the section that displays the statistics. I tried developing methods that would be able to compare multiple components at once. For example, say I wanted to compare people with images on, and people with images off. Now, out of those people with images on, I want to compare how many had JS on as well! This is a bad example because anyone with images off wouldn't be recorded as having javascript on as is the nature of the logging script. However, it gets more interesting when you factor in browers, operating systems, and pages visited.

This is my huge mess of a function that I didn't get around to optimizing:

// hs = haystack, ft = key value(s) to check, exp_val value(s) that ft is checked against,
 rcount tells it whether to take count of values with the same key (that is all it will do),
 $key = key to return;
function get_vals($hs$ft false$exp_val false$rcount true$key false){
    
$narr = array();
    
    foreach(
$hs as $hsk => $v){
        if(
is_array($ft)){
            for(
$n=0$r=true$n count($ft); $n++){
                if(
$v[$ft[$n]] != $exp_val[$n]){$r false; break;}
                else{ 
$r true;}
            }
        }
        elseif(
$v[$ft] == $exp_val){$r true;}
        elseif(!
$ft && !$exp_val){$r true;}
        else{
$r false;}
        
        if(
$key){$hsk $v[$key];}
        elseif(
$rcount && !is_array($v)){$hsk $v;}
        
        if(
$narr[$hsk] > && $r && $rcount){$narr[$hsk]++;}
        elseif(
$r && $rcount){$narr[$hsk] = 1;}
        elseif(
$r){ $narr[] = $hsk; }
    }
    if(
$rcount){arsort($narr);}
    return 
$narr;
}

Gah! How many lines of code did I just post? 24? Oh well, this function is recursive! So it would be much longer if I hadn't made it so. I haven't seen this function in well over a month, so dissecting it is only a little easier for me than it is for you. It can do multiple things, first it can aggregate data of the same type into one array key. That way I can get a sample of what the most blocked pages are and what not. The other thing it can do is take an array and isolate the keys you want, based on their values, into a new array. It can do this with multiple keys! It is amazing!

Using this I can do things like this! (where $vis is a huge array where every visitor has an array of their own and in that array is information collected on them)

$pages get_vals($vis, array('js''img'), array(1,1), true'page');
echo 
"|h4>Most blocked pages - IMG on && JS off|/h4>";
echo 
"|ol>";
foreach(
$pages as $page => $num){
    echo 
"|li>".$page." => ".$num."|/li>";
}
echo 
"|/ol>";

And get results like this!

Most blocked pages - IMG on && JS off

  1. /index.php => 1945
  2. /gallery/index.php => 668
  3. /404.php => 120
  4. /gallery/lb/index.php => 18
  5. /hire/index.php => 3

Check out more examples here: http://saurdo.com/js_perc_stats.php

That one function can provide me with any statistic I wish! What I was planning to develop from there was a way to send requests to the function without having to edit the PHP each time, in other words I wanted to make an HTML interface. I probably won't get around to that unless someone decides they really want it and are willing to pay for it.