5

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 ?

Dinesh Yadav
  • 75
  • 1
  • 5

1 Answers1

6

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

terrhorn
  • 510
  • 4
  • 6
  • Thanks terrhorn. Unfortunately I wanted it in ruby. thanks anyway. – Dinesh Yadav Dec 11 '14 at 09:58
  • 1
    Just to clarify @alloyking's link to the ruby documentation for multi-analysis. You can only use multi-analysis if the underlying data is the same for both metrics, e.g., you want both a sum and a count from a single event collection with the same filters. The accepted answer above queries multiple collections which is _not_ possible with multi-analysis. – terrhorn Mar 11 '16 at 17:12