1
1
import Constants from './constants' ;
2
2
import cookieStorage from './cookiestorage' ;
3
+ import MetadataStorage from './metaDataStorage' ;
3
4
import getUtmData from './utm' ;
4
5
import Identify from './identify' ;
5
6
import localStorage from './localstorage' ; // jshint ignore:line
@@ -32,7 +33,6 @@ if (BUILD_COMPAT_REACT_NATIVE) {
32
33
*/
33
34
var AmplitudeClient = function AmplitudeClient ( instanceName ) {
34
35
this . _instanceName = utils . isEmptyString ( instanceName ) ? Constants . DEFAULT_INSTANCE : instanceName . toLowerCase ( ) ;
35
- this . _legacyStorageSuffix = this . _instanceName === Constants . DEFAULT_INSTANCE ? '' : '_' + this . _instanceName ;
36
36
this . _unsentEvents = [ ] ;
37
37
this . _unsentIdentifys = [ ] ;
38
38
this . _ua = new UAParser ( navigator . userAgent ) . getResult ( ) ;
@@ -76,37 +76,63 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
76
76
}
77
77
78
78
try {
79
- this . options . apiKey = apiKey ;
80
- this . _storageSuffix = '_' + apiKey + this . _legacyStorageSuffix ;
79
+ _parseConfig ( this . options , opt_config ) ;
81
80
82
- var hasExistingCookie = ! ! this . cookieStorage . get ( this . options . cookieName + this . _storageSuffix ) ;
83
- if ( opt_config && opt_config . deferInitialization && ! hasExistingCookie ) {
84
- this . _deferInitialization ( apiKey , opt_userId , opt_config , opt_callback ) ;
85
- return ;
81
+ if ( this . options . cookieName !== DEFAULT_OPTIONS . cookieName ) {
82
+ utils . log . warn ( 'The cookieName option is deprecated. We will be ignoring it for newer cookies' ) ;
86
83
}
87
84
88
- _parseConfig ( this . options , opt_config ) ;
85
+ this . options . apiKey = apiKey ;
86
+ this . _storageSuffix = '_' + apiKey + ( this . _instanceName === Constants . DEFAULT_INSTANCE ? '' : '_' + this . _instanceName ) ;
87
+ this . _storageSuffixV5 = apiKey . slice ( 0 , 6 ) ;
89
88
90
- if ( type ( this . options . logLevel ) === 'string' ) {
91
- utils . setLogLevel ( this . options . logLevel ) ;
92
- }
89
+ this . _oldCookiename = this . options . cookieName + this . _storageSuffix ;
90
+ this . _unsentKey = this . options . unsentKey + this . _storageSuffix ;
91
+ this . _unsentIdentifyKey = this . options . unsentIdentifyKey + this . _storageSuffix ;
93
92
94
- var trackingOptions = _generateApiPropertiesTrackingConfig ( this ) ;
95
- this . _apiPropertiesTrackingOptions = Object . keys ( trackingOptions ) . length > 0 ? { tracking_options : trackingOptions } : { } ;
93
+ this . _cookieName = Constants . COOKIE_PREFIX + '_' + this . _storageSuffixV5 ;
96
94
97
95
this . cookieStorage . options ( {
98
96
expirationDays : this . options . cookieExpiration ,
99
97
domain : this . options . domain ,
100
98
secure : this . options . secureCookie ,
101
99
sameSite : this . options . sameSiteCookie
102
100
} ) ;
101
+
102
+ this . _metadataStorage = new MetadataStorage ( {
103
+ storageKey : this . _cookieName ,
104
+ disableCookies : this . options . disableCookies ,
105
+ expirationDays : this . options . cookieExpiration ,
106
+ domain : this . options . domain ,
107
+ secure : this . options . secureCookie ,
108
+ sameSite : this . options . sameSiteCookie
109
+ } ) ;
110
+
111
+ const hasOldCookie = ! ! this . cookieStorage . get ( this . _oldCookiename ) ;
112
+ const hasNewCookie = ! ! this . _metadataStorage . load ( ) ;
113
+ this . _useOldCookie = ( ! hasNewCookie && hasOldCookie ) && ! this . options . cookieForceUpgrade ;
114
+ const hasCookie = hasNewCookie || hasOldCookie ;
103
115
this . options . domain = this . cookieStorage . options ( ) . domain ;
104
116
105
- if ( ! BUILD_COMPAT_REACT_NATIVE ) {
106
- if ( this . _instanceName === Constants . DEFAULT_INSTANCE ) {
117
+ if ( this . options . deferInitialization && ! hasCookie ) {
118
+ this . _deferInitialization ( apiKey , opt_userId , opt_config , opt_callback ) ;
119
+ return ;
120
+ }
121
+
122
+ if ( type ( this . options . logLevel ) === 'string' ) {
123
+ utils . setLogLevel ( this . options . logLevel ) ;
124
+ }
125
+
126
+ var trackingOptions = _generateApiPropertiesTrackingConfig ( this ) ;
127
+ this . _apiPropertiesTrackingOptions = Object . keys ( trackingOptions ) . length > 0 ? { tracking_options : trackingOptions } : { } ;
128
+
129
+ if ( this . options . cookieForceUpgrade && hasOldCookie ) {
130
+ if ( ! hasNewCookie ) {
107
131
_upgradeCookieData ( this ) ;
108
132
}
133
+ this . cookieStorage . remove ( this . _oldCookiename ) ;
109
134
}
135
+
110
136
_loadCookieData ( this ) ;
111
137
this . _pendingReadStorage = true ;
112
138
@@ -508,75 +534,31 @@ AmplitudeClient.prototype._setInStorage = function _setInStorage(storage, key, v
508
534
storage . setItem ( key + this . _storageSuffix , value ) ;
509
535
} ;
510
536
511
- var _upgradeCookieData = function _upgradeCookieData ( scope ) {
512
- // skip if already migrated to 4.10+
513
- var cookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _storageSuffix ) ;
514
- if ( type ( cookieData ) === 'object' ) {
515
- return ;
516
- }
517
- // skip if already migrated to 2.70+
518
- cookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _legacyStorageSuffix ) ;
519
- if ( type ( cookieData ) === 'object' && cookieData . deviceId && cookieData . sessionId && cookieData . lastEventTime ) {
520
- return ;
521
- }
522
-
523
- var _getAndRemoveFromLocalStorage = function _getAndRemoveFromLocalStorage ( key ) {
524
- var value = localStorage . getItem ( key ) ;
525
- localStorage . removeItem ( key ) ;
526
- return value ;
527
- } ;
528
-
529
- // in v2.6.0, deviceId, userId, optOut was migrated to localStorage with keys + first 6 char of apiKey
530
- var apiKeySuffix = ( type ( scope . options . apiKey ) === 'string' && ( '_' + scope . options . apiKey . slice ( 0 , 6 ) ) ) || '' ;
531
- var localStorageDeviceId = _getAndRemoveFromLocalStorage ( Constants . DEVICE_ID + apiKeySuffix ) ;
532
- var localStorageUserId = _getAndRemoveFromLocalStorage ( Constants . USER_ID + apiKeySuffix ) ;
533
- var localStorageOptOut = _getAndRemoveFromLocalStorage ( Constants . OPT_OUT + apiKeySuffix ) ;
534
- if ( localStorageOptOut !== null && localStorageOptOut !== undefined ) {
535
- localStorageOptOut = String ( localStorageOptOut ) === 'true' ; // convert to boolean
536
- }
537
-
538
- // pre-v2.7.0 event and session meta-data was stored in localStorage. move to cookie for sub-domain support
539
- var localStorageSessionId = parseInt ( _getAndRemoveFromLocalStorage ( Constants . SESSION_ID ) ) ;
540
- var localStorageLastEventTime = parseInt ( _getAndRemoveFromLocalStorage ( Constants . LAST_EVENT_TIME ) ) ;
541
- var localStorageEventId = parseInt ( _getAndRemoveFromLocalStorage ( Constants . LAST_EVENT_ID ) ) ;
542
- var localStorageIdentifyId = parseInt ( _getAndRemoveFromLocalStorage ( Constants . LAST_IDENTIFY_ID ) ) ;
543
- var localStorageSequenceNumber = parseInt ( _getAndRemoveFromLocalStorage ( Constants . LAST_SEQUENCE_NUMBER ) ) ;
544
-
545
- var _getFromCookie = function _getFromCookie ( key ) {
546
- return type ( cookieData ) === 'object' && cookieData [ key ] ;
547
- } ;
548
- scope . options . deviceId = _getFromCookie ( 'deviceId' ) || localStorageDeviceId ;
549
- scope . options . userId = _getFromCookie ( 'userId' ) || localStorageUserId ;
550
- scope . _sessionId = _getFromCookie ( 'sessionId' ) || localStorageSessionId || scope . _sessionId ;
551
- scope . _lastEventTime = _getFromCookie ( 'lastEventTime' ) || localStorageLastEventTime || scope . _lastEventTime ;
552
- scope . _eventId = _getFromCookie ( 'eventId' ) || localStorageEventId || scope . _eventId ;
553
- scope . _identifyId = _getFromCookie ( 'identifyId' ) || localStorageIdentifyId || scope . _identifyId ;
554
- scope . _sequenceNumber = _getFromCookie ( 'sequenceNumber' ) || localStorageSequenceNumber || scope . _sequenceNumber ;
555
-
556
- // optOut is a little trickier since it is a boolean
557
- scope . options . optOut = localStorageOptOut || false ;
558
- if ( cookieData && cookieData . optOut !== undefined && cookieData . optOut !== null ) {
559
- scope . options . optOut = String ( cookieData . optOut ) === 'true' ;
560
- }
561
-
562
- _saveCookieData ( scope ) ;
563
- } ;
564
-
565
537
/**
566
538
* Fetches deviceId, userId, event meta data from amplitude cookie
567
539
* @private
568
540
*/
569
541
var _loadCookieData = function _loadCookieData ( scope ) {
570
- var cookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _storageSuffix ) ;
542
+ if ( ! scope . _useOldCookie ) {
543
+ const props = scope . _metadataStorage . load ( ) ;
544
+ if ( type ( props ) === 'object' ) {
545
+ _loadCookieDataProps ( scope , props ) ;
546
+ }
547
+ return ;
548
+ }
571
549
550
+ var cookieData = scope . cookieStorage . get ( scope . _oldCookiename ) ;
572
551
if ( type ( cookieData ) === 'object' ) {
573
552
_loadCookieDataProps ( scope , cookieData ) ;
574
- } else {
575
- var legacyCookieData = scope . cookieStorage . get ( scope . options . cookieName + scope . _legacyStorageSuffix ) ;
576
- if ( type ( legacyCookieData ) === 'object' ) {
577
- scope . cookieStorage . remove ( scope . options . cookieName + scope . _legacyStorageSuffix ) ;
578
- _loadCookieDataProps ( scope , legacyCookieData ) ;
579
- }
553
+ return ;
554
+ }
555
+ } ;
556
+
557
+ const _upgradeCookieData = ( scope ) => {
558
+ var cookieData = scope . cookieStorage . get ( scope . _oldCookiename ) ;
559
+ if ( type ( cookieData ) === 'object' ) {
560
+ _loadCookieDataProps ( scope , cookieData ) ;
561
+ _saveCookieData ( scope ) ;
580
562
}
581
563
} ;
582
564
@@ -594,19 +576,19 @@ var _loadCookieDataProps = function _loadCookieDataProps(scope, cookieData) {
594
576
}
595
577
}
596
578
if ( cookieData . sessionId ) {
597
- scope . _sessionId = parseInt ( cookieData . sessionId ) ;
579
+ scope . _sessionId = parseInt ( cookieData . sessionId , 10 ) ;
598
580
}
599
581
if ( cookieData . lastEventTime ) {
600
- scope . _lastEventTime = parseInt ( cookieData . lastEventTime ) ;
582
+ scope . _lastEventTime = parseInt ( cookieData . lastEventTime , 10 ) ;
601
583
}
602
584
if ( cookieData . eventId ) {
603
- scope . _eventId = parseInt ( cookieData . eventId ) ;
585
+ scope . _eventId = parseInt ( cookieData . eventId , 10 ) ;
604
586
}
605
587
if ( cookieData . identifyId ) {
606
- scope . _identifyId = parseInt ( cookieData . identifyId ) ;
588
+ scope . _identifyId = parseInt ( cookieData . identifyId , 10 ) ;
607
589
}
608
590
if ( cookieData . sequenceNumber ) {
609
- scope . _sequenceNumber = parseInt ( cookieData . sequenceNumber ) ;
591
+ scope . _sequenceNumber = parseInt ( cookieData . sequenceNumber , 10 ) ;
610
592
}
611
593
} ;
612
594
@@ -628,7 +610,12 @@ var _saveCookieData = function _saveCookieData(scope) {
628
610
if ( AsyncStorage ) {
629
611
AsyncStorage . setItem ( scope . _storageSuffix , JSON . stringify ( cookieData ) ) ;
630
612
}
631
- scope . cookieStorage . set ( scope . options . cookieName + scope . _storageSuffix , cookieData ) ;
613
+
614
+ if ( scope . _useOldCookie ) {
615
+ scope . cookieStorage . set ( scope . options . cookieName + scope . _storageSuffix , cookieData ) ;
616
+ } else {
617
+ scope . _metadataStorage . save ( cookieData ) ;
618
+ }
632
619
} ;
633
620
634
621
/**
0 commit comments