|
515 | 515 | apiEndpoint: 'api.amplitude.com', |
516 | 516 | cookieName: 'amplitude_id', |
517 | 517 | cookieExpiration: 365 * 10, |
518 | | - unsentKey: 'amplitude_unsent', |
519 | | - gupKey: 'amplitude_gup' |
| 518 | + unsentKey: 'amplitude_unsent' |
520 | 519 | }; |
521 | 520 |
|
522 | 521 | var eventId = 0; |
|
531 | 530 | Amplitude.prototype.init = function(apiKey, opt_userId) { |
532 | 531 | this.options = options; |
533 | 532 | options.apiKey = apiKey; |
534 | | - options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || null; |
| 533 | + |
| 534 | + // Load cookie data |
| 535 | + var cookie = Cookie.get(options.cookieName); |
| 536 | + var cookieData = null; |
| 537 | + if (cookie) { |
| 538 | + try { |
| 539 | + cookieData = JSON.parse(LZW.decompress(Base64.decode(cookie))); |
| 540 | + if (cookieData) { |
| 541 | + if (cookieData.deviceId) { |
| 542 | + options.deviceId = cookieData.deviceId; |
| 543 | + } |
| 544 | + if (cookieData.userId) { |
| 545 | + options.userId = cookieData.userId; |
| 546 | + } |
| 547 | + if (cookieData.globalUserProperties) { |
| 548 | + options.globalUserProperties = cookieData.globalUserProperties; |
| 549 | + } |
| 550 | + } |
| 551 | + } catch (e) { |
| 552 | + // Do nothing |
| 553 | + } |
| 554 | + } |
| 555 | + |
| 556 | + options.deviceId = options.deviceId || UUID(); |
| 557 | + options.userId = (opt_userId !== undefined && opt_userId !== null && opt_userId) || options.userId || null; |
| 558 | + saveCookieData(); |
535 | 559 |
|
536 | 560 | //log('initialized with apiKey=' + apiKey); |
537 | 561 | //opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId); |
538 | 562 | eventId = 0; |
539 | | - if ((options.deviceId = Cookie.get(options.cookieName)) == null) { |
540 | | - options.deviceId = UUID(); |
541 | | - Cookie.set(options.cookieName, options.deviceId, options.cookieExpiration); |
542 | | - } |
| 563 | + |
543 | 564 | var savedUnsentEventsString = localStorage.getItem(options.unsentKey); |
544 | | - var savedGlobalUserPropertiesString = localStorage.getItem(options.gupKey); |
545 | | - if (savedGlobalUserPropertiesString) { |
546 | | - options.globalUserProperties = JSON.parse(LZW.decompress(Base64.decode(savedGlobalUserPropertiesString))); |
547 | | - } |
548 | 565 | unsentEvents = (savedUnsentEventsString && JSON.parse(LZW.decompress(Base64.decode(savedUnsentEventsString)))) || []; |
549 | 566 | if (unsentEvents.length > 0) { |
550 | 567 | this.sendEvents(); |
551 | 568 | } |
552 | 569 | }; |
553 | 570 |
|
| 571 | + var saveCookieData = function() { |
| 572 | + Cookie.set(options.cookieName, Base64.encode(LZW.compress(JSON.stringify({ |
| 573 | + deviceId: options.deviceId, |
| 574 | + userId: options.userId, |
| 575 | + globalUserProperties: options.globalUserProperties |
| 576 | + }))), options.cookieExpiration); |
| 577 | + }; |
| 578 | + |
554 | 579 | Amplitude.prototype.setUserId = function(userId) { |
555 | 580 | options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null; |
| 581 | + saveCookieData(); |
556 | 582 | //log('set userId=' + userId); |
557 | 583 | }; |
558 | 584 |
|
559 | 585 | Amplitude.prototype.setGlobalUserProperties = function(globalUserProperties) { |
560 | 586 | options.globalUserProperties = globalUserProperties; |
561 | | - localStorage.setItem(options.gupKey, Base64.encode(LZW.compress(JSON.stringify(globalUserProperties)))); |
| 587 | + saveCookieData(); |
562 | 588 | //log('set globalUserProperties=' + JSON.stringify(globalUserProperties)); |
563 | 589 | }; |
564 | 590 |
|
|
635 | 661 |
|
636 | 662 | window.amplitude = instance; |
637 | 663 | window.Base64 = Base64; |
| 664 | + window.Cookie = Cookie; |
| 665 | + window.LZW = LZW; |
638 | 666 |
|
639 | 667 | // Apply the queued commands |
640 | 668 | for (var i = 0; i < q.length; i++) { |
|
0 commit comments