-4

Is there some reliable way to detect device/browser/OS of web page visitors except using the user agent string?

This is not for rendering/functionality of the web page/application, but only for statistics (how many percentage of visitors use ipad, iphone, pc, mac, chrome, edge, firefox etc).

This can be done either by JavaScript client side or .net server side.

2

First off, you can't trust User Agent Strings. Browsers can, and will, lie to you - especially mobile browsers, as they often come with features to spoof being the desktop version as an easy-to-find checkbox. But they are the only way you can get any system information from every browser.

I'm assuming that, since you're looking at the statistics, you'll be saving all the UA strings you recieve somewhere.

Thanks to decades of inconsistent standards you need to resign yourself to the fact browser vendors have stuffed their UA strings with a whole load of nonsense and that you're going to have to unpick that mess yourself and do some parsing and, even putting a lot of time in, it will never be perfect.

First you need to understand the format UA strings are in. UA Strings should be of the format:

Mozilla/5.0 (platform(comment); ...; ...) extension/version ... ... 

Internet Explorer reports itself under the 'Platform' section, in the colon-deliminated list in brackets, most other browsers reports themselves in the 'Extension' area.

According to the User-Agent strings guide on MDN and the extensive list on UserAgentString.com the following hold true right now:

  • Edge's last extension is 'Edge (version)'
  • Internet Explorer contains the string 'MSIE (version)' in the 'Product' section
    • Some browsers, such as the AOL embedded browser, do report MSIE, and follow it up with AOL (version)
  • Safari always ends with 'Safari/(version)', and does not include 'Chrome' as an extension
  • Firefox always ends with 'Firefox/(version)'
  • Chrome always ends with 'Chrome/(version) Safari/(version)'
  • Other, less popular, browsers (such as Opera) often mimic Chrome or Firefox's UA string but add their own browser to the end, so it would be 'Firefox/(version) (MyBrowser)/(version)'

From here you should be able to write a programme that'll figure out, with a decent accuracy, what each browser is telling you they are. You're going to get false positives, and probably some false negatives, but it's up to you to decide how much work to put in to improve your accuracy.

For example, the UA string you posted in reply to Jorg

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"

is more than likely from Chrome, not Edge, as it doesn't end with Edge/(version). Indeed it doesn't include Edge anywhere in the UA string. Again, if it did come from Edge, then there's likely been some UA spoofing going on and if that is the case, you can't reliably discerne those.

[Edit] (As per the comment below, Edge doesn't always report 'Edge', but it will report "Windows NT 10.0; Win64; x64" in its Platform area.)

Now, if that seems like a lot of work (and it is) you could just go with an off-the-shelf system, like Google Analytics*, that have already put in the work for you and work quite well and do a whole lot more than just browser usage reporting.

*(other analytics services are available)

  • Thank you for your answer. Just a side note, the user agent string was from my own edge browser, not modified. – Mr Zach Feb 6 '18 at 20:06
  • According to this stackoverflow.com/questions/31697157/… Edge also reports "Windows NT 10.0" in its Platform area even when it doesn't reference Edge. – GeeItSomeLaldy Feb 7 '18 at 9:00
0

No.

This is the fundamental nature of message-based communication. You can only observe what the other party chooses to tell you. If they choose to lie to you (for whatever reason), then there is no way for you to find out the truth, in fact, there isn't even a way for you to know that this isn't the truth (and even if you were told the truth, there would be no way for you to know that).

You can guess, you can use heuristics, you can choose to always trust the other party, but there cannot possibly be a "reliable" solution.

  • I dont care if they lie in the sense of changing their user agent string, but when all browser by default "lie" with the user agent string, then its useless. For example this is the user agent string of Edge "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36". Edge only add AppleWebKit, chrome, safari etc to make it "compatible" with crappy designed web pages and that's why the user agent string is not reliable – Mr Zach Feb 2 '18 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.