Skip to content

Commit aac1d57

Browse files
author
Curtis
committed
Add saveEvents configuration option
1 parent baee9d1 commit aac1d57

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/amplitude.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@
467467
apiEndpoint: 'api.amplitude.com',
468468
cookieName: 'amplitude_id',
469469
cookieExpiration: 365 * 10,
470-
unsentKey: 'amplitude_unsent'
470+
unsentKey: 'amplitude_unsent',
471+
saveEvents: true
471472
};
472473

473474
var eventId = 0;
@@ -479,9 +480,21 @@
479480
return eventId;
480481
};
481482

482-
Amplitude.prototype.init = function(apiKey, opt_userId) {
483+
/**
484+
* Initializes Amplitude.
485+
* apiKey The API Key for your app
486+
* opt_userId An identifier for this user
487+
* opt_config Configuration options
488+
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
489+
*/
490+
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
483491
this.options = options;
484492
options.apiKey = apiKey;
493+
if (opt_config) {
494+
if (opt_config.saveEvents !== undefined) {
495+
options.saveEvents = !!opt_config.saveEvents;
496+
}
497+
}
485498

486499
// Load cookie data
487500
var cookie = Cookie.get(options.cookieName);
@@ -513,13 +526,14 @@
513526
//opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId);
514527
eventId = 0;
515528

516-
var savedUnsentEventsString = localStorage.getItem(options.unsentKey);
517-
var unsentEvents = []
518-
if (savedUnsentEventsString) {
519-
try {
520-
unsentEvents = JSON.parse(savedUnsentEventsString);
521-
} catch (e) {
522-
//log(e);
529+
if (options.saveEvents) {
530+
var savedUnsentEventsString = localStorage.getItem(options.unsentKey);
531+
if (savedUnsentEventsString) {
532+
try {
533+
unsentEvents = JSON.parse(savedUnsentEventsString);
534+
} catch (e) {
535+
//log(e);
536+
}
523537
}
524538
}
525539
if (unsentEvents.length > 0) {
@@ -535,6 +549,14 @@
535549
})), options.cookieExpiration);
536550
};
537551

552+
var saveEvents = function() {
553+
try {
554+
localStorage.setItem(options.unsentKey, JSON.stringify(unsentEvents));
555+
} catch (e) {
556+
//log(e);
557+
}
558+
};
559+
538560
Amplitude.prototype.setUserId = function(userId) {
539561
options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null;
540562
saveCookieData();
@@ -580,10 +602,8 @@
580602
// phone_carrier: null
581603
};
582604
unsentEvents.push(event);
583-
try {
584-
localStorage.setItem(options.unsentKey, JSON.stringify(unsentEvents));
585-
} catch (e) {
586-
//log(e);
605+
if (options.saveEvents) {
606+
saveEvents();
587607
}
588608
//log('logged eventType=' + eventType + ', properties=' + JSON.stringify(customProperties));
589609
this.sendEvents();
@@ -606,10 +626,8 @@
606626
if (status == 200 && JSON.parse(response).added == numEvents) {
607627
//log('sucessful upload');
608628
unsentEvents.splice(0, numEvents);
609-
try {
610-
localStorage.setItem(options.unsentKey, JSON.stringify(unsentEvents));
611-
} catch (e) {
612-
//log(e);
629+
if (options.saveEvents) {
630+
saveEvents();
613631
}
614632
if (unsentEvents.length > 0) {
615633
scope.sendEvents();

0 commit comments

Comments
 (0)