Device brand discovery

I am working on web analytics. I use the client side of JavaScript and the NodeJS server. I know that we can find out the type of device using userAgent , but how to determine the brand of the device (client or server)?

+6
source share
6 answers

You cannot do this directly; the userAgent field simply does not contain a provider. However, you can create a library of userAgent tokens that map to specific providers. However, this will require a lot of research and testing on a wide range of devices.

For example, anything with "iOS", "iPhone" or "iPad" in userAgent, you can safely match "Apple". Then the Samsung Galaxy S3, for example, has the model number in userAgent, which is the “GT-I9300”. Then you can match it with Samsung. ... and then you will need to repeat this for any other device that you want to recognize. Searches for user agent strings on the Internet are probably the fastest way.

+1
source

One of the libraries that analyzes this for you is Platform.js , and you can use it either on the client side or on the server side, as described on their opening page, the links to which are given above.

Here is a client side example:

  <script type='text/javascript' src='platform.js'></script> <script type='text/javascript'> alert('you are using ' + platform.description + ' on an ' + (platform.manufacturer || 'unknown vendor') ) </script> 

Please note that you will not get the manufacturer in regular browsers, but you must get the brand on mobile devices, for example, Apple, Samsung, etc.

+5
source

On the server side, you can do this very easily using the npm module of the device, and if you use Express.js then it is as easy as a cake.

Install the module.

 npm i --S express-device 

Add to express code as middleware.

 var express = require('express'); var app = express(); var device = require('express-device'); app.use(device.capture()); app.get('/hello',function(req,res) { res.send("Hi to "+req.device.type.toUpperCase()+" User"); }); app.listen(3000); console.log("Listening to Port 3000"); 

Simple and sweet.

Link: https://codeforgeek.com/2016/07/how-to-detect-device-type-in-nodejs/

+1
source

Detection through UserAgent strings may result in errors in your discovery. You can change the browser line to whatever you want. In most cases, it does not tell you which device it is.

0
source

All the data that you can get from your client may be deceived, so do not expect each user to transmit only 100% correct data.

But you can get req.headers['user-agent'] clients through req.headers['user-agent'] in nodeJS, and here is a list of many mobile browsers .

0
source

In my case, ua-parser-js was the best free solution. The minimized version is at https://github.com/faisalman/ua-parser-js/tree/master/dist

What you need to do is how

 <script src="ua-parser.min.js" charset="utf-8"></script> <script type="text/javascript"> var parser = new UAParser(); var result = parser.getResult(); alert(result.device.vendor); </script> 

Before that, I tried platform.js and mobile-detect.js, but their detection range was too narrow for my application.

0
source

Source: https://habr.com/ru/post/944192/


All Articles