How to track retention and other metrics with Rails


As someone who can’t stop coming up with new side-projects written in Rails, I know how crucial it is to give attention to your data.

It’s very important to understand some key metrics, like how many users being registered in a particular week, how many users continue using the product (retention), what are the features being used, etc.

Here’s an example of the real admin dashboard I’m using at Candl (which is a service for book readers).

I’ve found a very easy way to do this in a Rails project. You only need one small model I’m calling Log (but you can call it whatever you want), and then you can build all kinds of metrics and charts using good old SQL queries.

Let me show you how easy it is.

Collecting data

You start by creating a simple migration.

There are only 2 meaningful fields there: user_id and event_name. timestamps is also important because it will allow us to know when a certain action took place.

Now we want to track every controller action, and on each call put data into our new table.

Modify your base Rails controller ApplicationController to look like this:

Now we’re tracking all the action being called, which look like “api/books#create“, “api/reviews#update“, etc.

Analyzing the data

Now it’s very straightforward to get some insights. Here are some examples that could be used to draw some fancy charts:

  • How many users has been active for every week?

  • How many users using a specific feature (e.g. adding book to plans)

Measuring retention

What is retention?

If you suddenly have lot’s of sign-ups because your website got featured on ProductHunt, this doesn’t necessarily mean anything. Many of those users will drop off very soon, and only some will continue to use your project.

So how do you track which users are here to stay?

This is where retention comes in. It shows how many users are really using your product over time.

Let’s say we have a retention period 1 week. We call current users (or real users) those who used your product this week and also used it the week before.

With our little Log model and the data we have it’s really straightforward to calculate that metrics over some period of time.

Here’s a helper class that can give you all the data:

That’s kinda it.

I find it fascinating how much useful data can be extracted from a very simple table with just several fields. Hope, you find it useful as well.

If you have any question feel free to ask in twitter.

Recent Posts