I initially posted this on StackOverflow and promptly got downvoted, which is typical when I post anything there. WebmasterWorld is a lot more welcoming, and its where I cut my teeth on webdev 20 years ago. As time passed I'd mostly forgotten about WW as most of the action is on StackExchange, but I thought of WW today, so here's my post about mobile screen sizes.
I'm making a graphical map for one of my sites and needed to know what the smallest mobile screen size is that I'd need to support, because larger screens means I can use smaller font sizes (the image will stretch the larger screen). But I couldn't find good data on screen size market share elsewhere. All the articles quote StatCounter, but their report lists a whopping 1/3 of the users' size as "other", which is particularly unhelpful.
So I collected data from my own visitors, mostly U.S. I collected the data automatically, with Javascript. I recorded only one size for each IP (so not counting the same user twice if they visit multiple pages), and bots don't skew the results, or skew them much, because most bots don't understand JS, much less have a defined screen size to report. Here are the results for 419 total users.
51% mobile (320-440 pixels)
39% desktop (1020-3440 pixels)
2% intermediate (443-493 pixels)
I don't know what the devices are in the 443-493 pixel range. The user agents are Android, but those sizes don't show up in the viewport size list at Yesviz.com.
Breaking the mobile results down further:
320: 2%
360-361: 10%
375-386: 16%
390: 15%
392-393: 17%
400-412: 13%
414: 7%
421-432: 17%
440: 4%
And even further:
• 361: 12%
• 400: 41%
Here's how I collected the data, and how you can do the same:
(1) Insert some Javascript onto the pages of my site to grab the user's screen width and send it to my server as an AJAX request, to be recorded into an SQLite3 database. The value I captured was window.innerWidth. I also captured the user agent (navigator.userAgent) though I didn't really need it.
(2) Use a server-side script (Python, PHP, or Perl) to record the data into an SQLite3 database. (If you don't have database skills you could append to a text file.) My db fields were recordID, datetime, IP, screen width, and user agent. I had my script check to see if I'd already recorded a value for that IP, so I didn't record the same visitor twice.
Incidentally, this gave me insight into which visitors to my site were bots. Since I recorded datetime and IP, I could compare the records in my screen-width database to my server's visitor logs, and see which requests in the visitor logs didn't make it into the database. Those requests are from bots, that don't understand Javascript.
This is the JS I used to capture the data and send it to the server.
function recordScreenSize() {
// CREATE THE AJAX OBJECT
const ajax = new XMLHttpRequest() // Other popular variables are "xhr" or "xhttp"
// GET THE DATA TO POST
width = window.innerWidth
userAgent = navigator.userAgent
// Encode special chars (esp. semicolons) to prevent the data from being sent
userAgent = encodeURIComponent(userAgent)
// OPEN THE CONNECTION
ajax.open("POST", "https://example.com/recordMobileScreenSize.cgi", true) // true = asynchronous
// SPECIFY THE DATA FORMAT (set to submit like a form)
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
// SEND THE DATA TO THE SERVER
ajax.send("width=" +width+ "&userAgent=" +userAgent)
}