|
467 | 467 | apiEndpoint: 'api.amplitude.com', |
468 | 468 | cookieName: 'amplitude_id', |
469 | 469 | cookieExpiration: 365 * 10, |
470 | | - unsentKey: 'amplitude_unsent' |
| 470 | + unsentKey: 'amplitude_unsent', |
| 471 | + saveEvents: true |
471 | 472 | }; |
472 | 473 |
|
473 | 474 | var eventId = 0; |
|
479 | 480 | return eventId; |
480 | 481 | }; |
481 | 482 |
|
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) { |
483 | 491 | this.options = options; |
484 | 492 | options.apiKey = apiKey; |
| 493 | + if (opt_config) { |
| 494 | + if (opt_config.saveEvents !== undefined) { |
| 495 | + options.saveEvents = !!opt_config.saveEvents; |
| 496 | + } |
| 497 | + } |
485 | 498 |
|
486 | 499 | // Load cookie data |
487 | 500 | var cookie = Cookie.get(options.cookieName); |
|
513 | 526 | //opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId); |
514 | 527 | eventId = 0; |
515 | 528 |
|
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 | + } |
523 | 537 | } |
524 | 538 | } |
525 | 539 | if (unsentEvents.length > 0) { |
|
535 | 549 | })), options.cookieExpiration); |
536 | 550 | }; |
537 | 551 |
|
| 552 | + var saveEvents = function() { |
| 553 | + try { |
| 554 | + localStorage.setItem(options.unsentKey, JSON.stringify(unsentEvents)); |
| 555 | + } catch (e) { |
| 556 | + //log(e); |
| 557 | + } |
| 558 | + }; |
| 559 | + |
538 | 560 | Amplitude.prototype.setUserId = function(userId) { |
539 | 561 | options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null; |
540 | 562 | saveCookieData(); |
|
580 | 602 | // phone_carrier: null |
581 | 603 | }; |
582 | 604 | 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(); |
587 | 607 | } |
588 | 608 | //log('logged eventType=' + eventType + ', properties=' + JSON.stringify(customProperties)); |
589 | 609 | this.sendEvents(); |
|
606 | 626 | if (status == 200 && JSON.parse(response).added == numEvents) { |
607 | 627 | //log('sucessful upload'); |
608 | 628 | 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(); |
613 | 631 | } |
614 | 632 | if (unsentEvents.length > 0) { |
615 | 633 | scope.sendEvents(); |
|
0 commit comments