Hello team, I'm trying to create a revenue metric ...
w
Hello team, I'm trying to create a revenue metric based on a mixpanel event, the event has an attribute for the total value of the purchase, in the 2nd image is the query error I'm getting, what could be the problem? Thank you in advance.
f
Hi Amr
Can you share the full query from the second image?
w
Hello Graham
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); // Complete Purchase Revenue function isMetric(event) { return event.name === "Complete Purchase"; } // Last 90 days - Metric value (Complete Purchase Revenue) return Events({ "from_date": "2022-09-29", "to_date": "2022-12-28", "event_selectors": [ { "event": "Complete 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["Total Price"]); } } 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 - Complete 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}; dates[d.date].count += d.count; dates[d.date].sum += d.sum; }) }); events.forEach(e=>{ const date = (new Date(e.value.date)).toISOString().substr(0,10); dates[date] = dates[date] || {count:0, sum:0}; dates[date].count++; dates[date].sum += e.value.metricValue; }); 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
does
``complete purchase``
have the amount the purchase was?
w
Yes it's in the events properties with the attribute
``Total Price``
event's*
f
I see
w
here's an example
the last prop