Q:

Ignoring bots when tracking events in Rails app

I am tracking events through Mixpanel in my Rails app. For example, my controller looks something like
class HomeController < ApplicationController
  def index
    track_event "Visitor: View Landing Page"
  end
end

The problem is that the app gets hit by a number of bots, most notably Pingdom (performance tracking service we use). Is there a clean way to ignore tracking when it is a bot that hits my app?
Note: I am interested in tracking unique visitors, so I assign a cookie to each visitor with a unique id. Bots obviously don't store cookies.

A:

The traditional way to do this is to build an index of bot user agents, and to simply ignore your tracking event code when the UA matches a bot.
bots = /Googlebot|Pingdom|.../
unless request.env["HTTP_USER_AGENT"].match(bots)
  track_event(...)
end

This is a pain to maintain, but it's generally effective. It obviously isn't effective for anyone lying about their UA, but there's not a lot you can do about that other than doing heuristic detection to determine that they aren't a bot, but that's likely not worth the effort in this case.

