mixpanel-lite
mixpanel-lite copied to clipboard
send events in batches of 50 to reduce requests
Why -> https://github.com/john-doherty/mixpanel-lite/issues/5#issuecomment-833741169
How -> Mixpanel multi-event
Logic: something like this needs adding into the send()
method before we turn them into requests
var temp = [];
/**
* to reduce network requests, we batch up events in sequence into one request (50 per batch)
* https://developer.mixpanel.com/reference/events#track-events-in-batch
* event batching logic
* --------------------
* is this an event?
* - yes
* - was the previous request a batch with less than 50?
* - yes
* - append to previous request
* - no
* - is the next request an event?
* - yes
* - make this a batch request
* - no
* - process this event as normal
* - no
* - process this event as normal
*/
for (var i = 0, l = items.length; i < l; i++) {
// is this an event?
if (items[i].event) {
// is the previous item an event batch with less than 50?
if (temp[i - 1] && Array.isArray(temp[i - 1]) && temp[i - 1][0].event && temp[i - 1].length < 50) {
// append to previous batch
temp[i - 1].push(items[i]);
}
// is the next item an event?
else if (temp[i + 1] && temp[i + 1].event) {
// make this one a batch in prep for the next
temp.push([items[i]]);
}
// otherwise, process as single event
else {
temp.push(items[i]);
}
}
// otherwise, append as normal
else {
temp.push(items[i]);
}
}
We also need to be mindful of how we clean up (remove) successful events posted to Mixpanel. At the moment, it's cleaning them up by going through the requests one at a time checking for __completed
. That will no longer work if 50 events are sent at once