Top Results of a Table

Say you want to…

  • determine which of your customers spend the most
  • rank your blog posts based on number of pageviews
  • figure out which customers are making the most API requests

You can use the Keen IO Group By analysis type to get the data you need to answer those questions, then do some processing on your end to rank them or make a top ten list.

In this example, we want to find the top ten most active users in our game between December 1, 2013 and December 1, 2014:

var client = new Keen({
  projectId: "YOUR_PROJECT_ID",
  readKey: "YOUR_READ_KEY"
});

Keen.ready(function() {
  // Create a new Query instance
  var query = new Keen.Query("count", {
    // Group players by their user.name
    event_collection: "user_action",
    group_by: "user.name",
    timeframe: {
      start: "2013-12-01",
      end: "2014-12-01"
    }
  });

  // Create a new Dataviz instance
  var table = new Keen.Dataviz()
    .chartType('table')
    .el(document.getElementById('table'))
    // Optional styling
    .chartOptions({
      width: '100%'
    });

  // Run the query and the result
  client.run(query, function(err, res) {
    table
      .parseRequest(this)
      .call(function() {
        // Rename the header row (Optional)
        this.dataset.updateRow(0, function(value, index){
          return ['Player Name', 'Score'][index];
        });        
        // Sort and filter results before rendering
        this.dataset.sortRows("desc", function(row) {
          return row[1];
        });
        this.dataset.filterRows(function(row, index) {
          return index < 11;
        });
      })
      .render();
  });
});

See the full example with the table visualized in this JSFiddle.

Want to see an example not using Javascript? Here’s a gist of a simple Ruby script illustrating how to implement Redis-style ZRANK on the result of a Keen IO Group By query.