Skip to content

Commit 96ce02f

Browse files
committed
added batchEvents option
1 parent 65eef18 commit 96ce02f

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* Add setting to batch events to send to in a single request.
4+
35
## 2.2.1 (Aug 13, 2015)
46

57
* Fix bug where multi-byte unicode characters were hashed improperly.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ You can configure Amplitude by passing an object as the third argument to the `i
9797
| uploadBatchSize | Maximum number of events to send to the server per request. | 100 |
9898
| includeUtm | If `true`, finds utm parameters in the query string or the __utmz cookie, parses, and includes them as user propeties on all events uploaded. | `false` |
9999
| includeReferrer | If `true`, includes `referrer` and `referring_domain` as user propeties on all events uploaded. | `false` |
100+
| batchEvents | If `true`, events are uploaded only when the number of unsent events is greater than `eventUploadThreshold`. | `false` |
101+
| eventUploadThreshold | Minimum number of events to send to the server per request if `batchEvents` is `true`. | 30 |
100102

101103

102104
# Advanced #

amplitude.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ var DEFAULT_OPTIONS = {
139139
saveEvents: true,
140140
sessionTimeout: 30 * 60 * 1000,
141141
unsentKey: 'amplitude_unsent',
142-
uploadBatchSize: 100
142+
uploadBatchSize: 100,
143+
batchEvents: false,
144+
eventUploadThreshold: 30
143145
};
144146
var LocalStorageKeys = {
145147
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -188,10 +190,14 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
188190
if (opt_config.includeReferrer !== undefined) {
189191
this.options.includeReferrer = !!opt_config.includeReferrer;
190192
}
193+
if (opt_config.batchEvents !== undefined) {
194+
this.options.batchEvents = !!opt_config.batchEvents;
195+
}
191196
this.options.platform = opt_config.platform || this.options.platform;
192197
this.options.language = opt_config.language || this.options.language;
193198
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
194199
this.options.uploadBatchSize = opt_config.uploadBatchSize || this.options.uploadBatchSize;
200+
this.options.eventUploadThreshold = opt_config.eventUploadThreshold || this.options.eventUploadThreshold;
195201
this.options.savedMaxCount = opt_config.savedMaxCount || this.options.savedMaxCount;
196202
}
197203

@@ -222,7 +228,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
222228
}
223229
}
224230
}
225-
if (this._unsentEvents.length > 0) {
231+
if (this.shouldSendEvents()) {
226232
this.sendEvents();
227233
}
228234

@@ -255,6 +261,13 @@ Amplitude.prototype.nextEventId = function() {
255261
return this._eventId;
256262
};
257263

264+
Amplitude.prototype.shouldSendEvents = function() {
265+
var batchEvents = this.options.batchEvents;
266+
var numEvents = this._unsentEvents.length;
267+
var threshold = this.options.eventUploadThreshold;
268+
return (!batchEvents && numEvents > 0) || (batchEvents && numEvents >= threshold);
269+
};
270+
258271
var _loadCookieData = function(scope) {
259272
var cookieData = Cookie.get(scope.options.cookieName);
260273
if (cookieData) {
@@ -477,7 +490,9 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
477490
this.saveEvents();
478491
}
479492

480-
this.sendEvents();
493+
if (this.shouldSendEvents()){
494+
this.sendEvents();
495+
}
481496

482497
return eventId;
483498
} catch (e) {
@@ -557,7 +572,7 @@ Amplitude.prototype.sendEvents = function() {
557572
}
558573

559574
// Send more events if any queued during previous send.
560-
if (scope._unsentEvents.length > 0) {
575+
if (scope.shouldSendEvents()) {
561576
scope.sendEvents();
562577
}
563578
} else if (status === 413) {

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ var DEFAULT_OPTIONS = {
2727
saveEvents: true,
2828
sessionTimeout: 30 * 60 * 1000,
2929
unsentKey: 'amplitude_unsent',
30-
uploadBatchSize: 100
30+
uploadBatchSize: 100,
31+
batchEvents: false,
32+
eventUploadThreshold: 30
3133
};
3234
var LocalStorageKeys = {
3335
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -76,10 +78,14 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
7678
if (opt_config.includeReferrer !== undefined) {
7779
this.options.includeReferrer = !!opt_config.includeReferrer;
7880
}
81+
if (opt_config.batchEvents !== undefined) {
82+
this.options.batchEvents = !!opt_config.batchEvents;
83+
}
7984
this.options.platform = opt_config.platform || this.options.platform;
8085
this.options.language = opt_config.language || this.options.language;
8186
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
8287
this.options.uploadBatchSize = opt_config.uploadBatchSize || this.options.uploadBatchSize;
88+
this.options.eventUploadThreshold = opt_config.eventUploadThreshold || this.options.eventUploadThreshold;
8389
this.options.savedMaxCount = opt_config.savedMaxCount || this.options.savedMaxCount;
8490
}
8591

@@ -110,7 +116,7 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
110116
}
111117
}
112118
}
113-
if (this._unsentEvents.length > 0) {
119+
if (this.shouldSendEvents()) {
114120
this.sendEvents();
115121
}
116122

@@ -143,6 +149,13 @@ Amplitude.prototype.nextEventId = function() {
143149
return this._eventId;
144150
};
145151

152+
Amplitude.prototype.shouldSendEvents = function() {
153+
var batchEvents = this.options.batchEvents;
154+
var numEvents = this._unsentEvents.length;
155+
var threshold = this.options.eventUploadThreshold;
156+
return (!batchEvents && numEvents > 0) || (batchEvents && numEvents >= threshold);
157+
};
158+
146159
var _loadCookieData = function(scope) {
147160
var cookieData = Cookie.get(scope.options.cookieName);
148161
if (cookieData) {
@@ -365,7 +378,9 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
365378
this.saveEvents();
366379
}
367380

368-
this.sendEvents();
381+
if (this.shouldSendEvents()){
382+
this.sendEvents();
383+
}
369384

370385
return eventId;
371386
} catch (e) {
@@ -445,7 +460,7 @@ Amplitude.prototype.sendEvents = function() {
445460
}
446461

447462
// Send more events if any queued during previous send.
448-
if (scope._unsentEvents.length > 0) {
463+
if (scope.shouldSendEvents()) {
449464
scope.sendEvents();
450465
}
451466
} else if (status === 413) {

test/amplitude.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('Amplitude', function() {
260260
assert.deepEqual(amplitude2._unsentEvents, []);
261261
});
262262

263-
it('should batch events sent', function() {
263+
it('should limit events sent', function() {
264264
amplitude.init(apiKey, null, {uploadBatchSize: 10});
265265

266266
amplitude._sending = true;
@@ -287,6 +287,30 @@ describe('Amplitude', function() {
287287
assert.deepEqual(events[5].event_properties, {index: 100});
288288
});
289289

290+
it('should batch events sent', function() {
291+
var threshold = 10;
292+
var additional = threshold/2;
293+
amplitude.init(apiKey, null, {batchEvents: true, eventUploadThreshold: threshold});
294+
295+
for (var i = 0; i < (threshold + additional); i++) {
296+
amplitude.logEvent('Event', {index: i});
297+
}
298+
299+
assert.lengthOf(server.requests, 1);
300+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
301+
assert.lengthOf(events, threshold);
302+
assert.deepEqual(events[0].event_properties, {index: 0});
303+
assert.deepEqual(events[9].event_properties, {index: 9});
304+
305+
server.respondWith('success');
306+
server.respond();
307+
308+
assert.lengthOf(server.requests, 1);
309+
var unsentEvents = amplitude._unsentEvents;
310+
assert.lengthOf(unsentEvents, additional);
311+
assert.deepEqual(unsentEvents[additional - 1].event_properties, {index: threshold + additional - 1});
312+
});
313+
290314
it('should back off on 413 status', function() {
291315
amplitude.init(apiKey, null, {uploadBatchSize: 10});
292316

0 commit comments

Comments
 (0)