wondering if someone could help me, looking to add...
# ask-questions
c
wondering if someone could help me, looking to add a duration metric, and i'm getting a precondition failed error, previous reports of this point to trying on mixpanel with manual JQL query, however it appears that Mixpanel no longer supports doing that via the UI anywhere that I can find, and I found this in their docs
query being used by metric, this is a simple one with event set, one property, and
sum(values)
for aggregation
Copy code
function main() {
  // Helper aggregation functions
  const sum = (arr) => arr.reduce((sum, x) => sum + x, 0);
  const count = (arr) => arr.length;
  const countDistinct = (arr) => new Set(arr).size;
  const min = (arr) => Math.min(...arr);
  const max = (arr) => Math.max(...arr);
  const avg = (arr) => count(arr)>0?(sum(arr)/count(arr)):0;
  const percentile = (arr, p) => {
    const s = [...arr].sort((a,b)=>a-b);
    if(!s.length) return 0;
    if(p<=0) return s[0];
    if(p>=100) return s[arr.length-1];
    const r = (s.length-1)*p/100;
    const rf = Math.ceil(r) - r;
    return s[Math.floor(r)]*rf + s[Math.ceil(r)]*(1-rf);
  };
  const median = (arr) => percentile(arr, 50);
  // Order Booked - Time on Page
  function isMetric(event) {
    return event.name === "Order Completed";
  }
  // Last 90 days - Metric value (Order Booked - Time on Page)
  return Events({
    "from_date": "2023-12-02",
    "to_date": "2024-03-02",
    "event_selectors": [
      {
        "event": "Order Completed"
      }
    ]
  })
  .filter(function(event) {
    if(isMetric(event)) return true;
    return false;
  })
  // Metric value per user
  .groupByUser(function(state, events) {
    state = state || {date: null, metricValue: []};
    for(var i=0; i<events.length; i++) {
      state.date = state.date || events[i].time;
      const event = events[i];
      if(isMetric(event)) {
        state.metricValue.push(event.properties["timeOnPage"]);
      }
    }
    return state;
  })
  // Remove users that did not convert
  .filter(function(ev) {
    return ev.value.date && ev.value.metricValue.length > 0;
  })
  // Aggregate metric values per user
  .map(function(user) {
    // Metric - Order Booked - Time on Page
    user.value.metricValue = !user.value.metricValue.length ? 0 : (
      (values => sum(values))(user.value.metricValue)
    );
    return user;
  })
  .reduce([
    // Overall summary metrics
    mixpanel.reducer.numeric_summary('value.metricValue'),
    // Summary metrics by date
    (prevs, events) => {
      const dates = {};
      prevs.forEach(prev => {
        prev.dates.forEach(d=>{
          dates[d.date] = dates[d.date] || {count:0, sum:0, sum_squares:0};
          dates[d.date].count += d.count;
          dates[d.date].sum += d.sum;
          dates[d.date].sum_squares += d.sum_squares;
        })
      });
      events.forEach(e=>{
        const date = (new Date(e.value.date)).toISOString().substr(0,10);
        dates[date] = dates[date] || {count:0, sum:0, sum_squares:0};
        dates[date].count++;
        dates[date].sum += e.value.metricValue;
        dates[date].sum_squares += Math.pow(e.value.metricValue, 2);
      });
      return {
        type: "byDate",
        dates: Object.keys(dates).map(d => ({
          date: d,
          ...dates[d]
        }))
      };
    }
  ])
  // Transform into easy-to-use objects
  .map(vals => vals.map(val => {
    if(val.count) return {type: "overall", ...val};
    return val;
  }));
}
after some more playing around, it seems the "run analysis" button errors on the metric, but adding the metric to an experiment works as expected 🤷
f
interesting
that might be a bug on our end
and I've spoken to the folks on Mixpanel, and while they want you to move to SQL, the JQL isn't going away soon.
👍 1
c
and can confirm, metric still functioning as expected a couple days into experiment, but run analysis is still erroring
image.png