Retrieving Browser, OS and Device Type By Parsing User Agent
July 04, 2018
The only way, from the server side, to detect client information like OS and browser type is through the User-Agent String passed along in HTTP headers from client requests. Unfortunately there is no defined standard structure for the User-Agent string and each browser arranges data in the string differently. This makes it difficult to account for all the different permutations of User-Agent string. The good news is that there is a great library that focuses on parsing the most common permutations of the User-Agent string. That library is called UA Parser.
Its important to note that clients can manually change their User-Agent before passing it along and so for critical use cases reading the User Agent String may not be sufficient.
Parsing The User Agent String With UA Parser
UA Parser is an open source project which focuses on reliably parsing the User-Agent string for a variety of device and browser types. As an added bonus, the project has multiple programming language implementations so there is a good chance there is an implementation ready to use in the language of your choice. For this tutorial, we will be using the Java implementation.
Include the dependency in your project
First we must include the dependency in our project. The maven repository can be found here. So we can simply add the dependency in our pom.xml like so.
<dependency><groupId>com.github.ua-parser</groupId><artifactId>uap-java</artifactId><version>1.4.0</version></dependency>
In this case we are using the 1.4 version of the ua-parser java implementation.
Utilize The Parser
Next, we will create a basic rest controller (using SpringBoot) that will read the User-Agent string from request and determine the OS, browser and device type.
@RequestMapping(value = "/parse-user-agent", method = RequestMethod.GET)public String parseUserAgent(HttpServletRequest request) throws IOException {Parser uaParser = new Parser();Client c = uaParser.parse(request.getHeader("User-Agent"));System.out.println("---Browser version");System.out.println(c.userAgent.family); // => "Mobile Safari"System.out.println(c.userAgent.major); // => "5"System.out.println(c.userAgent.minor); // => "1"System.out.println("---OS version");System.out.println(c.os.family); // => "iOS"System.out.println(c.os.major); // => "5"System.out.println(c.os.minor); // => "1"System.out.println("---Device type");System.out.println(c.device.family); // => "iPhone"//For future application, null check all of the fields and if not null concatenate it with its property and log. Separate this logic into another class.return "Parsed User Agent. Check Console for details";}
The controller code above does the following. We get a hold of the HttpServletRequest and get the User-Agent string which is stored in the User-Agent header. Next, we create a Parser object from the UA Parser library. The User-Agent is then passed to the parse method of the Parser. The Parser returns a Client object which contains all the extracted client information. We then extract the family, device, os by calling the corresponding properties.
This simple library is great for parsing and identifying many platforms. There are some limitations when choosing to parse client information from the User-Agent. For one, you will see the device type from requests on a laptop shows up as ‘Other’. This is because requests on laptops, desktops and tablets do not pass device identifying information in the User-Agent.