Customer-Facing Analytics

Keen IO’s very powerful analytics platform empowers you to track whatever data you care about, from anywhere, and then analyze and present valuable results in any environment. This flexibility makes it very easy to build customer-facing analytics for your users. This guide will show you how to build customer-facing analytics into your product with Keen!

This guide will cover:

Data Modeling

The first thing you need to nail down is the data model. Conveniently, most data models already work really well for customer-facing analytics.

Set a user id property for every event

For instance, let’s pretend that you sell internet-connected fitness watches, and you want to show a user their fitness stats. Whenever someone buys one of your watches, you assign a customer ID to that watch. That customer ID is also probably stored in the database that serves your website. Your fitness watch tracks every step that its owner takes, and stores that event in Keen.

Whenever you track a step, you want to make sure to include that customer ID property. Later on, when we’re ready to start analyzing data, we will use the customer ID to pull data for each specific customer. Your data model for each event sent might look something like this:

{
  location: {
    latitude: 37.773972,
    longitude: -122.431297
  },
  velocity: {
    speed: 6.32, # meters per second
    direction: 43.25 # degrees from north
  },
  customer_id: 1872
}
If you’re going to be displaying massive amounts of data (1M+) to each customer, there are a few data modeling tricks we can share with you. Reach out to us, and we’ll get you on the right track!

Data Analysis

Ask questions and return answers with the Query API

The Query API enables you to ask any question you want. Let’s say we want to show our watch customers what their average running speed is. This is an easy query, using Keen’s average API and a filter for the customer ID.

Here’s an example of how to run a query with keen-js:

var average_speed = new Keen.Query("average", {
  event_collection: "step",
  target_property: "velocity.speed",
  timeframe: "this_7_days",
  filters: [
    {
      property_name: 'customer_id',
      operator: 'eq',
      property_value: customer_id // This is a dynamic variable, that stores the current customers' ID
    }
  ]
});

// Send query
client.run(average_speed, function(err, res){
  if (err) {
    // there was an error!
  }
  else {
    console.log("Your average speed is:" + res.result);
  }
});

The important part of that query is the filters section. I’m doing a filter to make sure that I only average over data where customer_id is equal to the current user’s customer_id. On your customers' dashboard, you will want to set that filter dynamically using a variable from your server.

Security

Scoped keys allow you to create separate API keys for each of your individual users. You can apply global filters to a scoped key, so that all queries done with that scoped key will have a filter applied. This allows you to create customer-specific keys that automatically add the customer filter.

This examples shows how Scoped Keys are applied in our Ruby client, but most of our SDKs support generating scoped keys.

scoped_key = Keen::ScopedKey.new(master_key, { "filters" => [{ # master_key should be a variable that contains your Keen master key
  "property_name" => "customer.id",
  "operator" => "eq",
  "property_value" => current_user.id # current_user.id should be a variable that contains the customer ID
}]}).encrypt!

Once you’ve generated a scoped key, you will likely want to store it in your user database so it can be reused in the future. Afterwards, you can inject the scoped key into your customer-facing visualizations, instead of using your regular API keys. That will automatically add the customer filter, so you don’t have to anymore.

Here’s an example of what our users average speed query will look like when we use scoped keys:

var average_speed = new Keen.Query("average", {
  event_collection: "step",
  target_property: "velocity.speed",
  timeframe: "this_7_days"
  // Filters removed, because scoped key automatically adds it
});

// Send query
client.run(average_speed, function(err, res){
  if (err) {
    // there was an error!
  }
  else {
    console.log("Your average speed is:" + res.result);
  }
});

Data Visualization

Now that you have all of the pieces to efficiently collect and analyze user metrics it’s time to visualize them in your product! We have a few resources that you can use to build in-product visualizations for your customers:

These visualizations tools work directly with the Keen Query API, making building visualizations a breeze!

Here’s an example of how we customized Keen’s dashboard templates to display average running speed inside of our fitness app using Keen’s javascript library:

FITime Dashboard

That’s it! These changes were done entirely in CSS, and took less than 5 minutes!

Visual learner? Check out our Customer-Facing Analytics video tutorial!

If you have questions about building customer-facing analytics, reach out to us or ping us on Slack! Our team of data engineers are ready to help.