In most online application you are likely to use third party statistics solutions, but there might be situations where this isn’t possible or where you only want to gather some basic OS and browser information.
There are several ways to approach this. One way would be to analyse the OS and browser of every visitor (for example with the PHP get_browser() function) and save a +1 count in a MySQL table per OS and browser. The advantage of this is that it doesn’t require a lot of space to save this information.
Another option is to save the entire user-agent string of a visitor (for example with the PHP $_SERVER[‘HTTP_USER_AGENT’] variable) and analyse this later. The advantage of this is that you are more flexible in your analytical calculations later on, for example if you only want OS and browser information of a specific group of logged in users. Saving the user-agent string also means that, as new OSes and browsers come along, you can adapt your analysis later on, but the saved information will always be correct. The downside of this is that saving many user-agent strings can build up in data size.
For an online application that only has several thousands of logged in users a year, I chose the last option.
Let’s say you have a MySQL table called ‘loginhistory’ that contains ‘userid’ and ‘useragent’. To make a count of how many times certain OSes occurred in the user-agent strings, I used the following MySQL query: