Measure mobile sdk usage and report per user stats in your Rails app, in 10 minutes, using and

The Background

I’ve been helping InfoMeters develop their on-boarding and user management flows. We realized things can go a lot faster if we got their development outside of Rails, so they could build the logic they wanted inside apps meant to deliver the same functionality, without needing to code everything by hand. Turns out that the growth / analytics world has tools ready for the job.

The Features

  • Track each API user’s usage of the app
  • Show each user a chart of their own usage over time

The Tools

Since we want to do this with as little coding as possible, we’ll lean on the following popular 3rd party tools:

  • Segment.IO fans data and events out to the relevant tools which each perform their own role. We’re only using one tool in this example, but, trust me, you want to integrate these guys first so other tools can be simply turned on/off when you decide you want to try them.
  • Keen.IO acts as the data warehouse, logging event and usage data and performing and visualizing all kinds of queries with their simple SDK.

1. Register for Segment.IO and Keen IO

Put your Keen IO secret key into your integrations panel, and turn on the green slider.

2. Pick up your secret key

It’s on their Ruby setup guide. You’ll need this in the next step.

3. Setup into Rails

  1. Add Segment.IO to your Gemfile
    gem ‘analytics-ruby’
  2. Initialize Segment.IO from inside config/initializers/analytics-ruby.rb
    Analytics.init(secret: ‘[Your Secret Key]’)

4. Track important activity when users perform those actions

In your Rails code, wherever you want to track a special event:

You can also include other user attributes in the :properties hash. InfoMeters is using this approach, tracking each use of the SDK, passing up a client API key that’s known to both their web application and their Android/iOS SKDs, to combine per-client analytics reported from each environment. Keen will let us filter events for those matching properties (like a certain API key) which lets us look at users’ data individually, later.

5. Visualize each user’s usage on their dashboard (or any page you’d like)

Keen offers a visualization SDK (a javascript library) that builds nice looking charts from the data you’ve reported.

Unfortunately, it’s very complex. Luckily, friendly open-source folks at CultivateStudios built a wrapper around Keen’s SDK called Keentivate, which provides a very simple interface to the same code. Let’s use that. =^]

5.1 Download keentivate.min.js from the Keentivate repo to app/assets/javascripts/keentivate/keentivate.min.js

5.2 Initialize the library with the following javascript in app/assets/javascripts/keentivate/initialize.js: (Get your projectId and readKeys from your account)

5.3 Add a manifest file (app/assets/javascript/keentivate/index.js) that will load these files in order:

5.4 And add to your main app/assets/javascript/application.js manifest (sigh, le Rails):

5.5 Now, where you want the chart to show up, add the right HTML:

The following chart should show a line graph of Used Feature X events, performed by the current user over time.

Special thanks to @calvinfo for helping me sort out some of the technical mashingups between and, @michellewetzler for introducing me to the idea of keen-powered per-user analytics, @Joe_Wegner for the Keentivate library and to the and teams for great products with amazing service.


Liked that post? Try this one next:

Jordan is a freelance engineer with full-stack chops, and an eye for analytics and growth.