DonMB
DonMB

Reputation: 2728

Check device type via user_agent (rails)

I want to know whether my users are browsing a page in my rails application with

I digged through many different solutions. Here are my favorites:

  1. The ua-parser gem: https://github.com/ua-parser/uap-ruby which seems to be very clean but unfortunately it always plots Other when I use parsed_string.device - I can detect the OS and browser with it very well.
  2. Writing it from scratch

Writing from scratch ended up in sth like this:

if request.user_agent.downcase.match(/mobile|android|iphone|blackberry|iemobile|kindle/)
  @os = "mobile"
elsif request.user_agent.downcase.match(/ipad/)
  @os = "tablet"
elsif request.user_agent.downcase.match(/mac OS|windows/)
  @os = "desktop"
end

However, what I miss is a complete documentation of the user agent 'device' definitions.

For example: What patterns do I need to look at if my user is browsing on a tablet/mobile device or desktop? I can't just guess and checking e.g. the ua-parser regex is not helping me either (very complicated): https://github.com/tobie/ua-parser/blob/master/regexes.yaml

Is there any simple solution to solve my problem? How does google analytics do it? I tried to research but could not find it. They're also displaying devices (desktop/tablet/mobile).

Upvotes: 9

Views: 6589

Answers (2)

Rednael
Rednael

Reputation: 370

I'm looking to do the 2nd option, because I need it as lean-n-mean as possible. There'se a lot of information in the User-Agent string, that I just don't need. And I don't want a function that tries to parse it all. Just simply: bot, desktop, tablet, mobile and other.

It's kind of a lot to read, but I'm looking for keywords using this extensive list.

So far, the following keywords seems to work for me. It's regular expressons in php, but you'll get the idea.

//try to find crawlers
//  https://developers.whatismybrowser.com/useragents/explore/software_type_specific/crawler/
if (preg_match('/(bot\/|spider|crawler|slurp|pinterest|favicon)/i', $userAgent) === 1)
  return ['type' => 'crawler'];

//try to find tablets
//  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/tablet/
//  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/ebook-reader/
if (preg_match('/(ipad| sm-t| gt-p| gt-n|wt19m-fi|nexus 7| silk\/|kindle| nook )/i', $userAgent) === 1)
  return ['type' => 'tablet'];

//try to find mobiles
//  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/mobile/
//  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/phone/
if (preg_match('/(android|iphone|mobile|opera mini|windows phone|blackberry|netfront)/i', $userAgent) === 1)
  return ['type' => 'mobile'];

//try to find desktops
//  https://developers.whatismybrowser.com/useragents/explore/hardware_type_specific/computer/
if (preg_match('/(windows nt|macintosh|x11; linux|linux x86)/i', $userAgent) === 1)
  return ['type' => 'desktop'];

return ['type' => 'other'];

Upvotes: 0

rmcsharry
rmcsharry

Reputation: 5562

The browser gem has a suggestion to do this, but until that is added you could still use the gem to figure it out by using browser.device?

Upvotes: 1

Related Questions