Question

JQL : Average number for an action/event fired with respect to users

  • 10 September 2019
  • 1 reply
  • 54 views

Badge

Hello,

I am trying to get average number for an action/event fired by all active users.

I tried something like this. But it's not working. Can anyone suggest a way to find a way to calculate number of user within the same query so that it can be used to calculate the average.

function main() {

var events = getEvents();

var userCounts = getUserCount(events);

return getEvents()
.groupBy(["name"], mixpanel.reducer.count())
.map(function(item) {
return {
"Plan": item.key[0],
"Action Count": item.value / userCounts[0]
}
}).sortDesc("Action Count");
}

function getEvents() {
return Events({
from_date: '2019-09-09',
to_date: '2019-09-10'
});
}

function getUserCount(events) {
return events.groupByUser(mixpanel.reducer.count())
.reduce(mixpanel.reducer.count());
}

It gives the following error:

[Validate failed]Uncaught exception Error: Attempting to apply multiple transformations to an intermediate collection.
 return events.groupBy(["name"], mixpanel.reducer.count())
               ^

Stack trace:
Error: Attempting to apply multiple transformations to an intermediate collection.
   at main (<anonymous>:7:17)


1 reply

Badge

Hi Arpit,


To clarify, did you want the average number of events per user and the total users for each event name? One nice thing we can do is add multiple reducers to a reducer function. So you can do something like this


function main() {
  return Events({
    from_date: '2019-09-01',
    to_date: '2019-09-13'
  })
  .groupByUser(['name'], mixpanel.reducer.count()) // get event count for each user/event name combination
  .groupBy(['key.1'], [mixpanel.reducer.count(), mixpanel.reducer.avg('value'), mixpanel.reducer.sum('value')]) // get the user count, average events per user, and total events per event name
}


Hope that helps!


Thanks,

Evan


Reply