Getting multiple metrics in a single Keen query

1.7k views Asked by At

I have been playing around with Keen and couldn't find a way to get multiple metrics in a single query. Although I don't mind to query again and merge the results in my app.

I also understand metrics api is simply designed to make visualisations easier where a analysis on a single property make sense.

Just want to know if it's possible in near future ?

1

There are 1 answers

2
terrhorn On BEST ANSWER

It is possible to retrieve multiple metrics in a single query in certain situations.

You can use the Multi-Analysis query type to run multiple types of analyses over the same collection. Multi-Analysis is outlined in greater detail here: https://keen.io/docs/data-analysis/multi-analysis/

Retrieving multiple metrics from separate collections is a bit different. keen-js allows you to pass an array of queries to the .run() function which it then runs simultaneously in the background. While it's true that keen-js is still running multiple queries behind the scenes, passing an array of query objects mimics a single query operation.

keen-js Example:

var count = new Keen.Query("count", {
  eventCollection: "pageviews",
  groupBy: "visitor.geo.country",
  interval: "daily",
  timeframe: "this_21_days"
});

var sum = new Keen.Query("sum", {
  eventCollection: "purchases",
  targetProperty: "total",
  interval: "daily",
  timeframe: "this_21_days"
});

client.run([count, sum], function(response) {
  count = this.data[0];
  sum = this.data[1];

  var pageviews = new Keen.Visualization(count, document.getElementById("pageviews"), {
    chartType: "metric",
    title: "Pageviews"
  });

  var total = new Keen.Visualization(sum, document.getElementById("total"), {
    chartType: "metric",
    title: "Total"
  });
});

There's also an example in keen-js that shows how to combine the results of multiple queries into a single chart: https://github.com/keen/keen-js/blob/master/docs/visualization.md#combine-results-of-two-queries