How do servers come up wth user screen resolution statistics?

Hi,

I was looking to see if one could detect the screen resolution directly with PHP. But all I found were articles that said that that was impossible. That no such information is passed along with the HTTP call header information, and that one had to use javascript for that. But how are the statistics regarding user screen resolution formed then? How do servers know what your resolution is?

Javascript. End of story. Use Javascript to send the information to the server in question.

So there are websites that send the following information to their servers: screen.width = x, screen.height = y? And on the basis of those data the servers compile their statistics? And how exactly would the sites send that code snippet to the servers?

AJAX, I would imagine.
It would be quite simple to fire off a small AJAX request containing any relevant user statistics when the page loads.

Simplest JavaScript code to send the screen width and height to the server.

img = new Image();
img.src = 'process.php?height=' + screen.height + '&width=' + screen.width;
img = undefined;  

That runs process.php on the server passing it the width and height of the screen that the browser is displayed on.

Most scripts would use a lot more code than that because most people don’t realise that if you are just sending data from the web page to the server and don’t need a response back that you can do it this way and only need three lines of code to pass as much information to the server as needed.

Agreed, Dave, it would. But who has such an AJAX script on their site? Have you or anyone you know ever received a request from anyone to include such a script somewhere in the code so that the servers can collect resolution data? And to you the same question, Stephen.

I must admit, no I haven’t.
I think most people who are interested in such things would use Google Analytics.

Pullo has answered Frank’s question. Anyone using Google Analytics (or any other similar script that captures statistics about visitors) has a script in their page that captures such data.

I can’t see any reason why anyone would bother to write code to do it themselves rather than to use a script such as Analytics that does it for you.

In the case of the script that I included in my post above that code was written as an example of how data can be easily sent to the server from JavaScript in situations where no response from the server is required using a lot less code than you need when the JavaScript needs to be able to receive and process a response. I used the screen width and height simply as two fields to represent whatever variables you want to be able to pass from JavaScript to a server side script. It was just a coincidence that the script was an exact fit to what was being asked. See http://javascriptexample.net/ajax08.php for my original use of that script. The example script following that one is a far more useful implementation of that concept of sending data to the server as it provides a way for the server to perform specific processing when someone stops viewing the page (it has nothing to do with what the Op was asking about though).

That’s a neat trick felgall and I must confess I didn’t know it before, so thanks for sharing :slight_smile:
However, saying that, I was vaguely aware that some companies use images in HTML emails to find out if people read them (the emails that is), so I guess this would be a similar tactic, as in this case, too, there is no response needed from the server.

OK, but how many years are there between the resolution stats being readily available and the appearance of Google Analytics? At least a number of years. How did servers compile their stats before GA?

The same way that analytics does it - using an ajax call of one type or another. The version I posted above works in any browser that supports images. The more conventional XMLHttpRequest type of call has been available to use since IE4. The XDomain and XHR2 calls that analytics actually uses now have only been added in more recent browsers.

Basically any browser from Netscape 2 on has had the ability to send the information to the server in one way or another.

OK, but who had that script/code snippet included in their code? Not the normal websites, I would think. Or are the statistics based upon the users of search engines like Google?

It used to be really common for web sites to include a counter on their page that displayed how many visitors the site had received. Capturing any stats about the visitors would have been a part of that script. There were literally hundreds if not thousands of different hit counter scripts around for people to choose from. Hardly anyone used search engines back then as they didn’t find much in the way of useful results. Directories built by real people were far more popular back then. Google didn’t come along until much later.

It used to be really common for web sites to include a counter on their page that displayed how many visitors the site had received. Capturing any stats about the visitors would have been a part of that script. There were literally hundreds if not thousands of different hit counter scripts around for people to choose from.

That sounds plausible. Thanks for clearing it up, Stephen.