51Degrees UAParser
We've forked the popular UAParser.js library (which relies solely on a deprecated User-Agent) to create the 51Degrees UAParser, which has support for both User-Agents and User-Agent Client Hints (UA-CH).
Depending on your environment, you may need to delegate UA-CH to the third-party cloud.51degrees.com domain. There are two ways to tell the browser that it is allowed to pass the Client Hints to the third-party domain:
- Setting the Accept-CH and Permissions-Policy HTTP response headers when serving the page.
- Adding the
<meta http-equiv="Delegate-CH">
tag in the page head.
You can find out more on which method is more suitable for your environment via this UAParser blog. You can also see the 51Degrees UAParser in action with this UAParser demonstration page.
Download the 51D UAParser
The packages are available via:
Migrating from UAParser.js to 51Degrees UAParser
Creating a Resource Key
Before integrating 51Degrees UAParser, you need to register and configure a 51Degrees Resource Key using the Cloud Configurator. The Configurator tool allows you to choose the device properties you would like to get as a result of the call to UAParser. Follow the Configurator explanation for more detail on how to create a Resource Key.
We recommend following this URL https://configure.51degrees.com/S6fGMDKw to create a Resource Key, as it contains the minimum recommended properties that you will need for the UAParser to work.
The minimum recommended properties to include in your Resource Key are:
- IsMobile
- HardwareVendor
- HardwareModel
- HardwareName
- PlatformVendor
- PlatformName
- PlatformVersion
- LayoutEngine
- BrowserVendor
- BrowserName
- BrowserVersion
- JavascriptHardwareProfile
- DeviceType
- SetHeaderBrowserAccept-CH
- SetHeaderHardwareAccept-CH
- SetHeaderPlatformAccept-CH
The majority of the recommended properties are the equivalent to the properties that the original UAParser.js returned. However, the 51Degrees UAParser requires additional properties for accurate device detection. JavascriptHardwareProfile is needed for reliable detection of Apple devices, and the trio of Accept-CH properties are needed for User-Agent Client Hints detection.
Migrating client-side code
To migrate from the original ua-parser-js, you probably had the page code similar to the below:
To migrate this code to use 51Degrees UAParser:
- Download the 51Degrees UAParser package
- We need to either use Accept-CH and Permissions-Policy headers or add a Delegate-CH meta tag within the
<head>
element. We chose the latter in the below migrated code snippet example. - Change the script import statement to use the path to the new package in the src attribute or use a CDN link.
- The new 51Degrees UAParser is inherently asynchronous, thus UAParser is a promise. We prefer resolving promises using the await keyword – in order to use the top-level await, we must set the script type="module" (). It is possible, however, to use the UAParser without await, calling then () instead.
- Here is the code to obtain the result object, notice the await keyword and the Resource Key:
let result = await UAParser("<your resource key>");
- The code below obtaining the result can stay the same, but if you had additional properties configured for this Resource Key, you can access them directly (using lowercase key names) inside the result.device object.
The migrated code snippet looks like this:
Alternatively, if the server adds Accept-CH and Permissions-Policy response headers, the snippet becomes even simpler as you can omit the Delegate-CH meta tag.
Migrating server-side code
A common scenario on the server is to receive a User-Agent as a request header and pass it to the UAParser to recognize the device. The Node.js server code could look something like this:
Here is what we need to change to migrate this to use 51Degrees UAParser:
- We want to use a HTTPS server instead of HTTP so that the browser would send us User-Agent Client Hints (as UA-CH are only sent over HTTPS connections)
- The package name in the require statement should be changed to @51degrees/ua-parser-js.
- The function passed to the server should now be marked async, as we are going to use await inside it.
- The parser call should be prepended with await.
- The parser call now takes the Resource Key and the request header map as a parameter.
Now we assume that this would be a secondary call to the server from within the page that has already been rendered and had an Accept-CH header in the response that listed all the necessary User-Agent Client Hints. The Accept-CH header in the original page would make it send the Client Hints in all subsequent requests to the same origin. This feature is called the Accept-CH cache.
Here is a quick and dirty example where we'll show how to ask the browser to immediately send the User-Agent Client Hints by setting two response headers: Accept-CH and Critical-CH. The latter tells the browser to immediately come back with the Client Hints if it did not send them on the first attempt, so it starts another request before loading the response body.
Live code demo
We hosted a live code demo of the 51Degrees UAParser during a webinar.