breaking into the unknown…

loging user agent details

Leave a comment

Recently, our application throwing some exception in production, when request trigger from certain browser on certain OS. For example a javascript code working on IE8 + window,  when Iam testing in India, but QA person form USA reporting error in IE8. To reproduce the issue, I have to login to there machine remotely and find that they are using different build of IE8. So it is decided to log the browser and OS detail from which user is accessing our application, so that we can prioritize the bug to be fixed.

You can do it by capturing the request when a user login to your application. request a object in itself in rails and available in all the controller.

You can get list of all method available to this request object by calling methods method on it as available for any object in ruby.

request.methods #return a array of methods

[:ignore_accept_header, :ignore_accept_header=, :auth_type, :gateway_interface, :path_translated, :remote_host, :remote_ident, :remote_user, :remote_addr, :server_name, :server_protocol, :accept, :accept_charset, :accept_encoding, :accept_language, :cache_control, :from, :negotiate, :pragma, :key?, :request_method, :request_method_symbol, :method, :method_symbol, :get?, :post?, :put?, :delete?, :head?, :headers, :original_fullpath, :fullpath, :original_url, :media_type, :content_length, :xml_http_request?, :xhr?, :ip, :remote_ip, :uuid, :server_software, :raw_post, :body, :form_data?, :body_stream, :reset_session, :session=, :session_options=, :GET, :query_parameters, :POST, :request_parameters, :authorization, :local?, :deep_munge,  :trace?, :parseable_data?, :[], :[]=, :values_at, :referer, :referrer, :user_agent, :cookies, :base_url, :path, :trusted_proxy?, :parse_multipart]

The method of our concern is user_agent . It will return the user’s browser and os detail from which he is making the request to your application.

Some of the sample output is as below :

request.user_agent # below is the output for different browser

From firefox

“Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0”

from chrome

“Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5”

So you can see that, it has all the detail of browser, its version and Operating system. you just need to parse it to retrieve all the detail.

Well, you do not need to do that also, as user_agent gem is available for that. Add below to your Gemfile and run bundle install

gem 'useragent', '0.2.3', :git => "" # I have specified 
the git url as I found that there is also another gem with the same name, and I do not want 
to get into any conflict

Now you can retrieve various detail as below :

user_agent = request.user_agent # It will return the user agent

“Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:18.0) Gecko/20100101 Firefox/18.0”

parsed_user_agent = UserAgent.parse(user_agent) #the gem will parse above string for you returning value you need


So, once you get all these detail, you can just save it in login_histories table as below.

    LoginHistory.create(:user_id=>, :login_time =>, :login_ip => request.remote_ip,
      :browser => parsed_user_agent.browser, :browser_version => parsed_user_agent.version,
      :platform => parsed_user_agent.platform, :os => parsed_user_agent.os)

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s