Skip to content

shair-tech/browser_sense

Repository files navigation

BrowserSense

Log information about the browsers accessings your Rails application.

Installation

BrowserSense monitors which browser is accessing your Ruby on Rails app.

Add the gem to your application’s Gemfile and then bundle:

gem "browser_sense"
bundle

To enable logging, add browser_sense to the application_controller.rb of your app, i.e.:

class ApplicationController < ActionController::Base
  [...]

  include BrowserSenseFilter
  browser_sense

  [...]
end

Data logged

Each invocation of a route of your application generates a line in the log storing the following details:

  1. Browser name
  2. Platform
  3. Device name
  4. Controller being invoked
  5. Method of the controller being invoked
  6. Request format (e.g. html, json)
  7. Hashed IP, which allows to track requests from the same IP, while preserving privacy
  8. Timestamp
  9. IP
  10. browser_version
  11. platform_version
  12. bot?
  13. search_engine?
  14. bot name

Information is stored in the application log, each line prefixed by BrowserSense and data presented in CSV.

Example

Before 1.10:

$ cat production.log
[...]
[<production log header>] BrowserSense: "Firefox","linux","Unknown","Devise::SessionsController","new","12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0","2022-06-09T10:08:09+02:00"
[...]

From 1.10 on (we add some field and use CSV for rendering the CSV, which removes unnecessary quotes). (Notice that the last field, bot_name is empty. Hence the final comma.

$ cat production.log
[...]
[<production log header>] BrowserSense: Firefox,linux,Unknown,UsageController,index,html,12ca17b49af2289436f303e0166030a21e525d266e209267433801a8fd4071a0,2025-12-22T09:29:58+01:00,127.0.0.1,145,0,false,false,
[...]

Analyzing Data

(Option 1) When you want to analyze data, extract the information with:

grep BrowserSense production.log | cut -f2- -d: > data.csv

and then, e.g., open the resulting file in a spreadsheet.

(Option 2) If you prefer you can use

  • BrowserSenseParser.match?(line) which returns true if the line is a line produced by BrowserSense
  • BrowserSenseParser.parse(line) which returns a Hash with all the fields

(Option 3) There is a Ruby script browser_sense. The script takes as input the pathname of the file with the log and prints to stdout the result of various analyses. The script is a quick hack, wrapping sed, grep and miller (Miller), which must be installed on your machine for the script to succeed.

(Option 4) If you want more reports and plots, you can use the log_sense gem (https://rubygems.org/gems/log_sense), which understands the data generated by this gem.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org

Contributing

TODO

Changelog

./CHANGELOG.org

Authors and Contributors

Shair.Tech

Known Bugs

We have been running BrowserSense for quite a few years with no particular issues. There are no known bugs; there is an unknown number of unknown bugs.

You are most welcome to report issues and missing features, using the Issue tracker.

License

The gem is available as open source under the terms of the MIT License.

About

Log and analyze data about browsers connecting to your Ruby On Rails app

Resources

License

Stars

Watchers

Forks

Contributors