amplitude / Amplitude-JavaScript

JavaScript SDK for Amplitude
MIT License
314 stars 132 forks source link

Using batch event upload api manually #227

Open n1rna opened 4 years ago

n1rna commented 4 years ago

I am not sure about this one being a feature-request or not. But I would like to be able to send multiple events batched in one single request to amplitude, without needing to wait for a specific amount of time or a behavior similar to batchEvents option.

There are times that I want to submit more than one (sometimes 20-100) events based on a user's single action. What do you offer to do in such scenarios?

I would like to be able to do something like this:

let events = []; // list of events, each element includes eventProperties and eventType
let amp = amplitude.getInstance();
amp.logBatchEvents(events);

And Identity (User) information should be the same for all the events.

haoliu-amp commented 4 years ago

I think you want all the events to be sent immediately?

Something like our private api _sendEventsIfReady? https://github.com/amplitude/Amplitude-JavaScript/blob/master/src/amplitude-client.js#L462

We could introduce an API called, flushEvents and you can use like this.

pseudo code:

for () {
logEvent(event)
}
flushEvents()
n1rna commented 4 years ago

Yes! That's exactly what I mean.

I took a look at the source and I think using batchEvents config will cause some inconsistencies. I have introduced forceDiffer argument to logEvent prototype method, so one can differ a call manually even if batchEvents config is set to false.

haoliu-amp commented 4 years ago

Cool, I plan to add flushEvents to other platforms too. So this is definitely a valid feature request. Will do it when I cleared out other priorities.

n1rna commented 4 years ago

I hope the PR gets merged sooner :)) I really need this feature in my current project.

haoliu-amp commented 4 years ago

We need to discuss this internally.