Author avatar

Hristo Georgiev

Reactive Analytics Using Keen.IO and ActionCable in Rails 5

Hristo Georgiev

  • Jan 10, 2019
  • 20 Min read
  • Jan 10, 2019
  • 20 Min read
Interesting APIs


Data analytics is one of today's biggest trends. An increasing number of industries rely on metadata analysis to make decisions and gain competitive advantage. As such, more and more software products, from sleep-cycle mobile apps to enterprise logistics software are becoming coupled with in-depth analytics.

However, making sense out of data tends to be a tedious and tricky process. Building a meaningful dashboard requires a lot of changes both to your back-end architecture and to your front-end visualizations. This is where Keen.IO can be very helpful.

Keen.IO is a SaaS (Software-as-a-Service) solution that makes analyzing data and building dashboards easy, fast, and efficient. It comes with a developer-friendly API that covers the whole process: collecting data, analyzing it and visualizing it. It also comes with SDKs that can be used in a plethora of technologies.

In this guide, we are going to creating a reactive web dashboard from scratch by using Ruby on Rails 5 and its newly-introduced ActionCable, as well as Keen.IO's Ruby SDK. Let's get started!

Setting up

Connecting your Rails app with Keen.IO

First, open your terminal and make a new Rails 5 application. You need to have Ruby 2.2.4 up in order to do this:

1rails _5.0.0_ new reactivedashboard

Once the application is generated, go to its directory:

1cd reactivedashboard

Add the following gems to your Gemfile:

3gem "keen"
4gem "dotenv-rails"
5gem 'bootstrap-sass', '~> 3.3.6'
6gem 'sass-rails', '>= 3.2'

Install the gems:

1bundle install

What do these gems do? The keen gem makes Keen.IO's Ruby SDK available in the Rails app, dotenv-rails lets you save your Keen.IO APi credentials securely, and bootstrap-sass gives things a cleaner and crisper look.

In order to set up Boostrap, go to app/assets/stylesheets/application.scss: and insert the following lines:

1// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
2@import "bootstrap-sprockets";
3@import "bootstrap";

Now that the bootstrap CSS is set, let's move onto the more important things:

Getting your credentials

Connect to Keen.IO by using environment variables. Environment variables configure an application's behavior in different environments (development, staging, production, etc.). Environment variables must always be kept secret, since some of them may give out sensitive information about your application. One way to do this is using the dotenv-rails(( gem, which is included in the list of required gems above.

Go to Keen.IO's website and register your free account. Once you register, log in and create your first project. Once you've created this project you will see project credentials in the overview tab. Some things that will be available to you:

  • Project ID: The unique identifier of your project
  • API credentials:
    • Write key - used when you want to publish data to your collections
    • Read key - used for running queries and reading data from your collections
    • Master key - used for deleting records and collections

In the root of your Rails application, create a new .env file.Put the following snippet in it:


Then, open up your first Keen.IO project's dashboard and replace the placeholders with your credentials in the corresponding fields.

With the keen gem installed and your API credentials inserted in your .env file, you are ready to publish, delete and update events in Keen.IO through your Rails app.

Publishing your data

Keen.IO uses collections that represent different types of events which occur in your application. A collection can contain all sorts of data you would like to record, from clicks made by a user to purchases made through your application.

In this guide, Keen.IO will keep track of the instances when a particular model is created. Let's call the collection products or products_created. A product_created event will have a name, a description, the number of times it's been favorited, and a price.

Let's scaffold the product model:

1rails g scaffold product price:decimal name:string description:text favorites:integer

rails g scaffold will generate all the views, controller actions, model and database migrations you need to make to have a CRUDproduct model. Add the model to your database:

1rails db:migrate

To start populating data into Keen.IO, add tollowing snippet to your product model:

3class Product < ApplicationRecord
4  after_save { Keen.publish 'products' , self }

after_save is a callback hook which is executed every time a product in our application's database is created. The Keen.publish method provided by the Keen.IO's Ruby SDK accepts two parameters. The first is the collection to be interacted with and the second parameter is the data/event that we would like to track or publish. In this case, self refers to the particular instance of the model itself. Thus, every time a particular object is created, we are going to send the parameters associated with the Product model (name, price, favorites, etc).

Recording events vs recording entities When you are doing analytics, you must always think about data in terms of events, not collections. In this tutorial, we are not recording the products themselves, we are only recording the event of their creation. The product entity should stay in the database of the application. What's important is the data each event generates. Read more on the topic here.

Start your Rails server

1 rails s

Go to http://localhost:3000/products/new and create some products. After you have added a few products, go to your Keen.IO's project dashboard. Under the overview tab you will be able to see your newly-created collection and event occurrence times.


Analyzing your data

The next step is to start making sense out of the whole data that has been entered. At this point, a software development team would try to devise the numerous ways to represent the data in a meaningful format. Thankfully, Keen.IO's data analysis API does all this for you. It gives you options to select the core analysis types:

  • Sum - calculate the sum of numeric values in a collection
  • Average - calculate the average of numeric values in a collection
  • Minimum - return the minimum, of all numeric values in a property
  • Maximum - return the maximum of all numeric values in a property
  • Percentile - calculate a percent of a given property in a colleciton
  • Median - get the median value of all numeric values of a given property
  • Count and count_unique - count either all instances or just the unique occurences of values of a given property
  • Select and select_unique - select a list of values found for a given property

You can also input query parameters to group your data, sort, or filter it in terms of:

  • Timeframe - Calculate values for a given timerframe
  • Interval - Calculate values from the currenty minute up to the current year.
  • Filter - Refine your serach by filtering out values for given properties

You can also combine multiple queries and collections using funnels for more advanced analytics.

Exploring data

You can start playing with the events collected in Keen.IO using its explorer. To access it, go to your project overview and click on the explorer tab on the top right. You can also download the explorer and use it locally .

Using the explorer's UI, you can leverage all the analysis types and apply the filters easily:

Creating and saving queries

In your explorer, select count as the analysis time, products as the collection type, and this month as the time frame. Click on the run button to unveil the results.


Voila! You just created your first query. You can save your query and reuse it any time you want. You can also visualize the data differently (if applicable) by choosing from the different types of visualizations in the top right (next to the field for the query name).

The next step is to click on the embed button on the right and get the code for displaying the query for the next step in the guide:

1var client = new Keen({
6Keen.ready(function() {
7  var query = new Keen.Query("count", {
8    eventCollection: "product",
9    timeframe: "this_14_days",
10    timezone: "UTC"
11  });
13  client.draw(query, document.getElementById("my_chart"), {
14    // Custom configuration here
15  });

You can continue playing with the explorer and create one or two more queries. Make sure you save them because you are going to need the code for embedding in the next step of the guide.

Visualising your data reactively

After collecting data and analyzing it, we will build the dashboard. First, include the Keen.IO JavaScript SDK in the Ruby on Rails application. There are many ways to do this, but for the sake of simplicity, you can simply include the javascript file from a CDN (content delivery network) into your application layout:

1<!-- app/views/layouts/application.html.erb -->
3  <head>
4    <!-- Paste this line in the head section of the layout -->
5    <%= javascript_include_tag "//" ,'data-turbolinks-track': 'reload'%>
6  </head>
7  <!-- content -->

Next, let's create an action and a view for the dashboard:

2class ProductsController < ApplicationController
3  #...
4  def dashboard
5  end
6  #...

Add the following line to routes.rb in order to make the dashboard the root action of the application:

2Rails.application.routes.draw do
3  root to: 'products#dashboard' #set the dashboard as your root
4  resources :products

Lastly, create a view for your dashboard action:

1<!-- app/views/products/dashboard.html.erb -->
2<nav class="navbar navbar-default">
3  <div class="container-fluid">
4    <div class="navbar-header">
5      <a class="navbar-brand" href="#">
6        Keen Dashboard
7      </a>
8    </div>
9    <ul class="nav navbar-nav navbar-right">
10      <li><%= link_to 'All products', products_path%></li>
11    </ul>
12  </div>
15<div class="container-fluid">
16  <div id="alert-placeholder"></div>
17  <div class="row">
18    <div class="col-md-6" id="chart-wrapper">
20    </div>
22    <div class="col-md-6" id="pie-wrapper">
24    </div>
25  </div>
26  <div class="row">
27    <div class="col-md-6" >
28      <div class="col-md-6" id="total-wrapper">
30      </div>
31    </div>
32  </div>

The view contains a simple bootstrap header and a few columns with id. The div tags with id chart-wrapper, pie-wrapper, and total-wrapper are going to be used as references for putting the analytics elements into the document.

Implementing reactivity

Reactive analytics means that our data visualization strategy will change automatically (without refreshing the page) as the data in Keen.IO changes. Ruby on Rails 5 make this easy through ActionCable. ActionCable is a Rails 5 module that introduces an API for working with WebSockets in Ruby on Rails.

The first step is to generate a channel. In your terminal, execute:

1 rails g channel analytics

The command will create a server-side representation of the channel in app/channels directory and a coffeescript file in app/assets/channels for handling the client side of the channel.

First, you need to explicitly define the name of the channel through which the analytics information is going to be streamed.

2class AnalyticsChannel < ApplicationCable::Channel
3 def subscribed
4   stream_from "analytics_channel"
5 end
6 #...

A channel usually has multiple instances. As such, steam_from is used to further specify the channel. For example, one channel can have instances that broadcast to multiple users. In such cases, the parameters entered in stream_from would include a unique identifier. But, in this guide, we are doing it the simpler way, by hardcoding a string.

Second, you need to create a job that is going to broadcast to the analytics_channel every time the an event is published to Keen.IO. We are putting our broadcast in a job because jobs can run parallel to other application processes. This ensures that your application will be able to queue multiple simultaneous requests for broadcasts.

Open your terminal and generate the job.

1 rails g job UpdateAnalytics

In order to maintain reactivity, the job has to be queued to broadcast every time that an event is published to Keen.IO. In this guide, we are doing it only once -- when creating a new product:

2class Product < ApplicationRecord
3  after_create_commit do
4    #publish the product creation event to Keen.IO
5    Keen.publish 'products' , self
6    #broadcast the job to update the analytics
7    UpdateAnalyticsJob.perform_later self
8   end
10    #...

In the job itself, add a call to broadcast to the analytics_channel:

2class UpdateAnalyticsJob < ApplicationJob
3  queue_as :default
5  def perform(product)
6    ActionCable.server.broadcast 'analytics_channel', product: product
7  end

That's it! The back-end is set up. Let's move to the front-end and put the Keen.IO JavaScript SDK library that was previously added in the appication's layout to use.

Visualizing queries

Go to app/assets/javascripts/channels/

There, you can see three functions: connected(), disconnected(), and received().

connected() is called when the client connects to the server-side websocket, disconnected() is called in the opposite case, and received() is called when data is broadcasted from the server.

Outside of these functions, declare a global function that is going to be called to reload all the queries and visualize them on the dashboard:

1 = App.cable.subscriptions.create "AnalyticsChannel",
2  connected: ->
3      loadAnalytics();
5  disconnected: ->
6    # Called when the subscription has been terminated by the server
8  received: (data) ->
9      loadAnalytics();
12@loadAnalytics = () =>
13  #paste the embed code from Keen.IO here

@loadAnalytics (@ denotes a global function in CoffeeScript) is going to be called when the client connects to the server-side channel, when there is data received from the channel, and when displayed in the view itself.

Remember that we've already established the server-side and client-side operations, so the only thing missing is the call from the view itself. Let's add it:

1<!-- app/views/products/dashboard.html.erb -->
3<!-- put this in the bottom of the document -->
4    <script>
5        loadAnalytics()
6    </script>

Now, go to your Keen.IO explorer and start getting the embed code from your favorite queries. Use to transpile the javascript to coffeesscript.


Here is an example with three queries displayed in the different div wrappers:

1@loadAnalytics = () =>
2  client = new Keen(
4    readKey: 'YOURKEENREADKEY')
5  Keen.ready ->
7    allProducts = new Keen.Query("count", {
8      eventCollection: "product",
9      timeframe: "this_1_months",
10      timezone: "UTC"
11    });
14    favoritesDist = new Keen.Query("count_unique", {
15      eventCollection: "product",
16      groupBy: [
17        "name"
18      ],
19      targetProperty: "favorites",
20      timeframe: "this_14_days",
21      timezone: "UTC"
22    });
24    productsCreatedToday =  new Keen.Query("sum", {
25      eventCollection: "product",
26      interval: "daily",
27      targetProperty: "id",
28      timeframe: "this_14_days",
29      timezone: "UTC"
30    });
32    client.draw productsCreatedToday , document.getElementById('chart-wrapper'), {}
33    client.draw favoritesDist, document.getElementById('pie-wrapper'), {}
34    client.draw allProducts, document.getElementById('total-wrapper'), {}

First, we initialize the client with the credentials of the project provided by Keen.IO. Keen.ready is called when the HTML document is loaded and the queries are ready to be visualized. Keen.Query takes two arguments. The first argument is the analytics type, and the second argument is an object that contains details about the collection, interval and filters. Have look at Keen.IO's JavaScript SDK for more information on creating queries.

client.draw is the function that does the drawing. The first argument is the query, and the second argument is the selected element from the HTML document.

Go to http://localhost:3000 and see the results. You now have a functioning dashboard that displays and automatically synchronizes each time that the product collection is modified.

To test out reactivity, open browser windows with http://localhost:3000/products/new and http://localhost:3000. Then, try to create a new product.


Creating a dashboard through Keen.IO

If you'd want to test out a dashboard layout or see how your queries visualize all at once, Keen.IO offers a dashboard builder.



Dashboard items are distributed in rows in columns, the same way it is done when building a web applicaiton.You can pick your saved queries and distribute them in the dashboard.

That was all! I hope you found this tutorial interesting. If you missed out some of the parts or need a reference on the code, I have created a GitHub repository that contains the code from the tutorial.

Please favorite this tutorial and leave your comments and feedback in the comments below!