@@ -10,42 +10,69 @@ import localStorage from './localstorage'; // jshint ignore:line
10
10
import topDomain from './top-domain' ;
11
11
12
12
/**
13
- * MetadataStorage involves SDK data persistance
14
- * storage priority: cookies -> localStorage -> in memory
15
- * if in localStorage, unable track users between subdomains
16
- * if in memory, then memory can't be shared between different tabs
17
- */
18
- class MetadataStorage {
19
- constructor ( { storageKey, disableCookies, domain, secure, sameSite, expirationDays} ) {
13
+ * MetadataStorage involves SDK data persistance
14
+ * storage priority: cookies -> localStorage -> in memory
15
+ * if in localStorage, unable track users between subdomains
16
+ * if in memory, then memory can't be shared between different tabs
17
+ */
18
+ class MetadataStorage {
19
+ constructor ( {
20
+ storageKey,
21
+ disableCookies,
22
+ domain,
23
+ secure,
24
+ sameSite,
25
+ expirationDays,
26
+ } ) {
20
27
this . storageKey = storageKey ;
21
- this . disableCookieStorage = ! baseCookie . areCookiesEnabled ( ) || disableCookies ;
22
28
this . domain = domain ;
23
29
this . secure = secure ;
24
30
this . sameSite = sameSite ;
25
31
this . expirationDays = expirationDays ;
26
- this . cookieDomain = '' ;
32
+
33
+ this . cookieDomain = '' ;
27
34
28
35
if ( ! BUILD_COMPAT_REACT_NATIVE ) {
29
36
const writableTopDomain = topDomain ( getLocation ( ) . href ) ;
30
- this . cookieDomain = domain || ( writableTopDomain ? '.' + writableTopDomain : null ) ;
37
+ this . cookieDomain =
38
+ domain || ( writableTopDomain ? '.' + writableTopDomain : null ) ;
31
39
}
40
+
41
+ this . disableCookieStorage =
42
+ disableCookies ||
43
+ ! baseCookie . areCookiesEnabled ( {
44
+ domain : this . cookieDomain ,
45
+ secure : this . secure ,
46
+ sameSite : this . sameSite ,
47
+ expirationDays : this . expirationDays ,
48
+ } ) ;
32
49
}
33
50
34
51
getCookieStorageKey ( ) {
35
52
if ( ! this . domain ) {
36
53
return this . storageKey ;
37
54
}
38
55
39
- const suffix = this . domain . charAt ( 0 ) === '.' ? this . domain . substring ( 1 ) : this . domain ;
56
+ const suffix =
57
+ this . domain . charAt ( 0 ) === '.' ? this . domain . substring ( 1 ) : this . domain ;
40
58
41
59
return `${ this . storageKey } ${ suffix ? `_${ suffix } ` : '' } ` ;
42
60
}
43
61
44
62
/*
45
- * Data is saved as delimited values rather than JSO to minimize cookie space
46
- * Should not change order of the items
47
- */
48
- save ( { deviceId, userId, optOut, sessionId, lastEventTime, eventId, identifyId, sequenceNumber } ) {
63
+ * Data is saved as delimited values rather than JSO to minimize cookie space
64
+ * Should not change order of the items
65
+ */
66
+ save ( {
67
+ deviceId,
68
+ userId,
69
+ optOut,
70
+ sessionId,
71
+ lastEventTime,
72
+ eventId,
73
+ identifyId,
74
+ sequenceNumber,
75
+ } ) {
49
76
const value = [
50
77
deviceId ,
51
78
Base64 . encode ( userId || '' ) , // used to convert not unicode to alphanumeric since cookies only use alphanumeric
@@ -54,22 +81,18 @@ import topDomain from './top-domain';
54
81
lastEventTime ? lastEventTime . toString ( 32 ) : '0' , // last time an event was set
55
82
eventId ? eventId . toString ( 32 ) : '0' ,
56
83
identifyId ? identifyId . toString ( 32 ) : '0' ,
57
- sequenceNumber ? sequenceNumber . toString ( 32 ) : '0'
58
- ] . join ( '.' ) ;
84
+ sequenceNumber ? sequenceNumber . toString ( 32 ) : '0' ,
85
+ ] . join ( '.' ) ;
59
86
60
87
if ( this . disableCookieStorage ) {
61
88
localStorage . setItem ( this . storageKey , value ) ;
62
89
} else {
63
- baseCookie . set (
64
- this . getCookieStorageKey ( ) ,
65
- value ,
66
- {
67
- domain : this . cookieDomain ,
68
- secure : this . secure ,
69
- sameSite : this . sameSite ,
70
- expirationDays : this . expirationDays
71
- }
72
- ) ;
90
+ baseCookie . set ( this . getCookieStorageKey ( ) , value , {
91
+ domain : this . cookieDomain ,
92
+ secure : this . secure ,
93
+ sameSite : this . sameSite ,
94
+ expirationDays : this . expirationDays ,
95
+ } ) ;
73
96
}
74
97
}
75
98
@@ -105,7 +128,7 @@ import topDomain from './top-domain';
105
128
lastEventTime : parseInt ( values [ 4 ] , 32 ) ,
106
129
eventId : parseInt ( values [ 5 ] , 32 ) ,
107
130
identifyId : parseInt ( values [ 6 ] , 32 ) ,
108
- sequenceNumber : parseInt ( values [ 7 ] , 32 )
131
+ sequenceNumber : parseInt ( values [ 7 ] , 32 ) ,
109
132
} ;
110
133
}
111
134
}
0 commit comments