Lately, I've been delving into Mixpanel's Javascript Query Language to delve deeper into understanding user behavior. Despite creating two variations of the query related to the below problem statement, neither seems to be yielding the desired results. Here is one version I've attempted - can anyone guide me on the correct approach to crafting the query for optimal outcomes?
Issue at hand: Identifying all unique user IDs who have engaged in events labeled "xyz", "abc" & "dfg".
Sample JSON-Formatted Database (dummy dataset showcased, actual database carries substantially more data structured identically):
[
{
"name": "xyz",
"distinct_id": "121"
},
{
"name": "abc",
"distinct_id": "345"
}
]
The Query Attempted:
function main() {
return Events({
from_date: "2017-06-26",
to_date: "2017-06-27"
})
.groupByUser(function(state, events) {
var flag_abc = false, flag_xyz = false, flag_dfg = false, count_unique = 0;
for (var i = 0; i < events.length - 1; i ++){
if (count_unique === 3){
state = true;
return state;
}
if (events[i].name === "abc" && !flag_abc ){
count_unique += 1;
flag_abc = true;
}
else if(events[i].name === "xyz" && !flag_xyz){
count_unique += 1;
flag_xyz = true;
}
else if(events[i].name === "dfg" && !flag_dfg){
count_unique += 1;
flag_dfg = true;
}
}
state = false;
return state;
})
.filter(function(arr){
if(arr.value === false)
return false;
else
return true;
})
}