Web dev

From bugbear knows something
Jump to navigation Jump to search

This is just a go to page when talking about anything related to your/my own website before I figure out what else to call it and how to organise this kind of stuff (don't worry, there will be redirections when that happens)


OS statistics

Sometimes I wonder what do statistics of the OS usage look like among the visitors, but I don't wanna use some tracking-level cookies. So instead I decided to use a lot simpler method, which is far less precise, too. Instead of asking browsers what's the host's system and adding a cookie to the browser, I simply ask about the system the moment they land on the main index page (idea being that one doesn't really need to go there more than once a day) and that's basically it. This however means, that I can't distinguish people repeatedly coming to the main index page and so if one just kept refreshing the page, it'd be added every single time. But then, this is just for my curiosity and nothing else so I don't mind that much.

finding out what OS is used

// we prepare a variable
var OSName="null";
// check what does navigator parameter say and set OSName accordingly
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX/Linux";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
// and OSName into a request for a file
var picsrc = "img/poziarnik.jpg?os="+OSName;
// add a new element to the index
var pic = new Image();
// set the image's src to our request
pic.src = picsrc;

As you can see, the whole thing is quite simple. After we find out what's the OSName, we add that string to a src of an image that browser will request. This will show up in our access.log and then we can just go through the file to find out how many times MacOS host accessed the file (as of now it seems there've been 0 cases for me).

going through access.log

This is up to you, but I personally like to keep it simple and just cat it, grep it, wc it :P Basically I run this command:

cat access.log | grep -o 'MacOS' | wc -l

Cat will read the file (don't forget to use the right path to access.log), grep will look for all instances of MacOS and wc -l counts them and outputs it into the stdout. Pretty simple :) "|" is pipeline to use output of one program as an input for another one.