Hi, getting an error when setting up an event in G...
# ask-questions
b
Hi, getting an error when setting up an event in Growthbook, fetching the event from Mixpanel called web_purchase (named it to Purchase Revenue) when trying to fetch the property "value" (the revenue). See the thread for the full query and our error message. Anyone knows why we're getting this issue?
See below for the full query:
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);
  // [Test] Purchase revenue 
  function isMetric(event) {
    return event.name === "web_purchase";
  }
  // Last 90 days - Metric value ([Test] Purchase revenue )
  return Events({
    "from_date": "2024-11-30",
    "to_date": "2025-03-01",
    "event_selectors": [
      {
        "event": "web_purchase"
      }
    ]
  })
  .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["value"]);
      }
    }
    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 - [Test] Purchase revenue 
    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;
  }));
}
f
"Precondition Failed" .. huh, is that the same error you get in the GrowthBook ui?
b
Yes @fresh-football-47124
Got time for a huddle and I can show you more if you'd like?
The strange thing is when we modified the query and ran it in the JQL section in Mixpanel, it worked
f
Let me see if @flaky-noon-11399 is around, she knows Mixpanel
👍 1
ack 1
b
So it seems to be something off with this automatically generated query in Growthbook in our case
Here's an example of how we modified the query in Mixpanel and made it work, but we can't modify it in Growthbook since it's a built in feature, as far as I know
f
which line was changed?
b
It's re-written from the 96 row long default failing query in Growthbook UI (see first message in Thread above) to this shorter version by our developer (see screenshot) for a specific verification purpose @fresh-football-47124
We just wanted to verify that the event "web_purchase" and the property "value" had in fact values that were numeric, which was the case as shown. So it shouldn't be that causing the issue in Growthbook, but we might be wrong
f
I can look in the code to see if that code is hard coded .. but its hard to tell which line from screen shots
b
Right, let me share more details via DM
f
sounds good
f
Hi 👋 happy to help on the Mixpanel side of things. Please, let me know if I can be of assistance 🌻