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 and Order By syntax to:

  • Calculate results that are grouped by one or more properties (like customerId or postId)
  • Return those results in ascending or descending order, ordering by the results or by group names
  • Optionally limit the results to a subset (like the Top 10)

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


import Keen from 'keen-js';

const client = new Keen({
  projectId: 'PROJECT_ID',
  readKey: 'READ_KEY'
});

client
  .query('count', {
    event_collection: "user_action",
    // Group players by their user.name
    group_by: 'user.name',
    // Order by result in descending order 
    order_by: {'property_name': 'result', 'direction': 'DESC'},
    // limit to the top 10
    limit: 10,
    timeframe: {
      start: '2013-12-01',
      end: '2014-12-01'
    }
  })
  .then(res => {
    // Handle results
  })
  .catch(err => {
    // Handle errors
  });