Doorkeeper
Developer Resources

Event tracking with Ruby on Rails

Mixpanel and KISSMetrics are both great tools to track how people interact with your site. At the core of these services is a simple Javascript API to track events:

kissmetrics.push(['record', 'My Event', {'My Property':'Value'}]);
mixpanel.track('My Event', {'My Property': 'Value'});

At first glance, the Javascript seems so simple a third-party library is not waranted. However, in a Rails application, the thing we are most interested in tracking is the creation of resources. With Rails, there is not normally a "landing page" after you create a resource. So the only place we know when we have just successfully created a resource is in the controller itself.

def create
  @group = current_user.groups.build(params[:group])
  if @group.save
    # track event here
    redirect_to @group
  else
    render action: "new"
  end
end

But the standard pattern in Rails is to redirect to resource you have just successfuly created. So we don't actually want to add the Javascript to the current request, but the request that immediately follows. The event tracker gem lets us do this. It provides a track_event that will dynamically insert the tracking Javascript into the next html page the user views.

Event Tracker Usage

Add the gem to your Gemfile

gem 'event_tracker'

Add your keys in config/application.rb

config.event_tracker.mixpanel_key = "YOUR_KEY"
config.event_tracker.kissmetrics_key = "YOUR_KEY"

Specify an around_filter where you want to enable the services

class ApplicationController < ActionController::Base
  around_filter :append_event_tracking_tags
end

Optionally identify the users to the services

class ApplicationController < ActionController::Base
  def mixpanel_distinct_id
    current_visitor_id
  end

  def mixpanel_name_tag
    current_user && current_user.email
  end

  def kissmetrics_identity
    current_user && current_user.email
  end
end

In your controllers or views start tracking!

track_event("Event Name", optional_property: "value")
register_properties(name: "value")

Event Tracker Method Mapping

event_tracker mixpanel kissmetrics
track_event track record
register_properties register set
mixpanel_name_tag name_tag -
mixpanel_people_set people.set -
mixpanel_alias alias -

For more details, see the source on github.

FAQ

KISSmetrics / Mixpanel already have a Ruby library. Why should I use this instead of just calling the library directly?
  1. Making a request to an API within a request is a bad idea, as the response to the user will be blocked until the request completes. While it is possible to use something like DelayedJob to process the API calls outside of the request, this adds additional complexity. By just using JavaScript, the user's browser does all the work for us.
  2. The JavaScript API's automatically handle things such as managing user identities, tracking additional properties based on the user's browser, and so on, which the Ruby library does not provide.
I'm using Devise. How can I track when a user signs up?

Subclass your own RegistrationsController and overwrite the new and create methods like the following.

class RegistrationsController < Devise::RegistrationsController
  def new
    super
    track_event "Starts sign up"
  end

  def create
    super
    if resource.persisted?
      track_event "Signed up"
    end
  end
end