Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

send events in batches of 50 to reduce requests #9

Open
john-doherty opened this issue May 6, 2021 · 0 comments
Open

send events in batches of 50 to reduce requests #9

john-doherty opened this issue May 6, 2021 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@john-doherty
Copy link
Owner

john-doherty commented May 6, 2021

Why -> #5 (comment)
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

@john-doherty john-doherty added enhancement New feature or request help wanted Extra attention is needed labels May 6, 2021
@john-doherty john-doherty changed the title Send offline tracking events in batches send events in batches of 50 to reduce requests May 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant