← All Articles

Using Mixpanel in your Rails app with Resque

Khash SajadiKhash Sajadi
Jan 1st 12Updated Jul 27th 17
Ruby on Rails

At CloudBlocks, we have looked at many
different application analytics providers from Google
Analytics
to
KISSmetrics and
Mixpanel. Finally we setteled down for Mixpanel
for several reasons:

  • Google Analytics is good for website metrics, but not event tracking
    and cohort
    reports
    . Also it
    doesn’t support named user tracking very well.
  • KISSmetrics is very good but also expensive and somewhat a bit pushy
    on their marketing side which always puts us off.
  • Mixpanel is very good at event tracking and has a decent free tier
    which always helps. Their JS library is very good but their Rails
    support is not comprehensive as they rely on a couple of outsider
    projects to have RoR support.

After choosing Mixpanel as our analytics provider of choice, we tried
using their native JS library to track the events. But some of the
events we would like to track for CloudQuartz, our job scheduling and
automation products, are not achievable by the JS library. For example,
we would like to know how many CloudQuartz Agents are registered per day
by OS. This means integrating Mixpanel events in our API endpoints.

We decided to integrate Mixpanel event tracking into our Rails
application. For that, we looked at Mixpanel’s suggested Rails
solutions
for this
but none were good enough.

Requirements

  • Use outside controller
  • Use Resque for async tracking
  • Allow full user identification at any level (work with
    Deviseor
    Warden)

The Mixpanel Gem does a good job
at integrating the JS into the page but the samples for integrating it
with Resque don’t work, can’t be
used outside of the controller and doesn’t do user tracking except for
the IP address.

Here is how we built our own Mixpanel RoR solution using some of
Mixpanel gem’s code and integrated with Resque.

Our solution works with Resque for async event logging and can be used
outside the controller.

The Solution

To track page views and users, use the native Mixpanel solution
explained
here. Just
simply include the Mixpanel script on your layout. Make sure you get the
API token from your YAML file so you can separate the events based
onenvironment.

Here is an example:

This goes right before the </body> tag.

Now for the application integration:

Create a Resque job like this:

Some of the code here is taken from the Mixpanel gem and credit is due.

Note: We user SettingsLogic
gem
to handle the
configurations, but you can just use the native way.

This job takes in the name of the event, the optional parameters, the
user_id of the current user (or nil if not signed in) and the optional
user IP address.

Optimization: Eliminate User db lookup by using a cache or passing the
full object in.

This job adds user identification (from Devise) and other bits and bobs to the call and then calls Mixpanel with the event.

Now, to log an event, just do the following:


Try Cloud 66 for Free, No credit card required