11var Cookie = require ( './cookie' ) ;
22var JSON = require ( 'json' ) ; // jshint ignore:line
33var language = require ( './language' ) ;
4- var Storage = require ( './storage ' ) ; // jshint ignore:line
4+ var localStorage = require ( './localstorage ' ) ; // jshint ignore:line
55var md5 = require ( 'JavaScript-MD5' ) ;
66var object = require ( 'object' ) ;
77var Request = require ( './xhr' ) ;
@@ -30,25 +30,19 @@ var DEFAULT_OPTIONS = {
3030 savedMaxCount : 1000 ,
3131 saveEvents : true ,
3232 sessionTimeout : 30 * 60 * 1000 ,
33+ unsentKey : 'amplitude_unsent' ,
34+ unsentIdentifyKey : 'amplitude_unsent_identify' ,
3335 uploadBatchSize : 100 ,
3436 batchEvents : false ,
3537 eventUploadThreshold : 30 ,
3638 eventUploadPeriodMillis : 30 * 1000 // 30s
3739} ;
38-
39- var StorageKeys = {
40+ var LocalStorageKeys = {
4041 LAST_EVENT_ID : 'amplitude_lastEventId' ,
4142 LAST_IDENTIFY_ID : 'amplitude_lastIdentifyId' ,
4243 LAST_SEQUENCE_NUMBER : 'amplitude_lastSequenceNumber' ,
4344 LAST_EVENT_TIME : 'amplitude_lastEventTime' ,
44- SESSION_ID : 'amplitude_sessionId' ,
45- UNSENT_EVENTS : 'amplitude_unsent' ,
46- UNSENT_IDENTIFYS : 'amplitude_unsent_identify' ,
47-
48- // Previously stored in cookie
49- DEVICE_ID : 'amplitude_deviceId' ,
50- USER_ID : 'amplitude_userId' ,
51- OPT_OUT : 'amplitude_optOut'
45+ SESSION_ID : 'amplitude_sessionId'
5246} ;
5347
5448/*
@@ -59,7 +53,6 @@ var Amplitude = function() {
5953 this . _unsentIdentifys = [ ] ;
6054 this . _ua = new UAParser ( navigator . userAgent ) . getResult ( ) ;
6155 this . options = object . merge ( { } , DEFAULT_OPTIONS ) ;
62- this . storage = new Storage ( ) . getStorage ( ) ;
6356 this . _q = [ ] ; // queue for proxied functions before script load
6457} ;
6558
@@ -116,17 +109,42 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config, callback) {
116109 domain : this . options . domain
117110 } ) ;
118111 this . options . domain = Cookie . options ( ) . domain ;
119- this . _upgradeStoredData ( ) ;
120- this . _loadStoredData ( ) ;
121112
122- this . setUserId ( opt_userId || this . options . userId || null ) ;
123- this . setDeviceId ( ( opt_config && opt_config . deviceId ) || this . options . deviceId || UUID ( ) ) ;
113+ _loadCookieData ( this ) ;
114+
115+ this . options . deviceId = ( opt_config && opt_config . deviceId !== undefined &&
116+ opt_config . deviceId !== null && opt_config . deviceId ) ||
117+ this . options . deviceId || UUID ( ) ;
118+ this . options . userId = ( opt_userId !== undefined && opt_userId !== null && opt_userId ) || this . options . userId || null ;
119+ _saveCookieData ( this ) ;
120+
121+ //log('initialized with apiKey=' + apiKey);
122+ //opt_userId !== undefined && opt_userId !== null && log('initialized with userId=' + opt_userId);
123+
124+ if ( this . options . saveEvents ) {
125+ this . _loadSavedUnsentEvents ( this . options . unsentKey , '_unsentEvents' ) ;
126+ this . _loadSavedUnsentEvents ( this . options . unsentIdentifyKey , '_unsentIdentifys' ) ;
127+ }
124128
125129 this . _sendEventsIfReady ( ) ;
126130
127131 if ( this . options . includeUtm ) {
128132 this . _initUtmData ( ) ;
129133 }
134+
135+ this . _lastEventTime = parseInt ( localStorage . getItem ( LocalStorageKeys . LAST_EVENT_TIME ) ) || null ;
136+ this . _sessionId = parseInt ( localStorage . getItem ( LocalStorageKeys . SESSION_ID ) ) || null ;
137+ this . _eventId = localStorage . getItem ( LocalStorageKeys . LAST_EVENT_ID ) || 0 ;
138+ this . _identifyId = localStorage . getItem ( LocalStorageKeys . LAST_IDENTIFY_ID ) || 0 ;
139+ this . _sequenceNumber = localStorage . getItem ( LocalStorageKeys . LAST_SEQUENCE_NUMBER ) || 0 ;
140+ var now = new Date ( ) . getTime ( ) ;
141+ if ( ! this . _sessionId || ! this . _lastEventTime || now - this . _lastEventTime > this . options . sessionTimeout ) {
142+ this . _newSession = true ;
143+ this . _sessionId = now ;
144+ localStorage . setItem ( LocalStorageKeys . SESSION_ID , this . _sessionId ) ;
145+ }
146+ this . _lastEventTime = now ;
147+ localStorage . setItem ( LocalStorageKeys . LAST_EVENT_TIME , this . _lastEventTime ) ;
130148 } catch ( e ) {
131149 log ( e ) ;
132150 }
@@ -146,135 +164,15 @@ Amplitude.prototype.runQueuedFunctions = function () {
146164 this . _q = [ ] ; // clear function queue after running
147165} ;
148166
149- /**
150- * Set an item in local storage. Decorate item key with the api key
151- * to avoid collisions.
152- *
153- * @property item: name of the item to store
154- * @property value: the value to store
155- */
156- Amplitude . prototype . setInStorage = function ( item , value ) {
157- var key = item + '_' + this . options . apiKey . slice ( 0 , 6 ) ;
158- if ( value ) {
159- this . storage . setItem ( key , value ) ;
160- return ;
161- }
162- // if value is null, then remove from storage
163- this . storage . removeItem ( key ) ;
164- } ;
165-
166- /**
167- * Fetch an item from local storage. Decorate item key with api key
168- * to avoid collisions.
169- *
170- * @property item: name of the item to fetch
171- * @property defaultValue: default value to return if the item does not exist
172- */
173- Amplitude . prototype . getFromStorage = function ( item , defaultValue ) {
174- var key = item + '_' + this . options . apiKey . slice ( 0 , 6 ) ;
175- return this . storage . getItem ( key ) || defaultValue ;
176- } ;
177-
178- Amplitude . prototype . _upgradeStoredData = function ( ) {
179- // migrate data from cookie to local storage
180- var cookieData = Cookie . get ( this . options . cookieName ) ;
181- if ( cookieData ) {
182- if ( cookieData . deviceId ) {
183- this . setInStorage ( StorageKeys . DEVICE_ID , cookieData . deviceId ) ;
184- }
185- if ( cookieData . userId ) {
186- this . setInStorage ( StorageKeys . USER_ID , cookieData . userId ) ;
187- }
188- if ( cookieData . optOut !== undefined ) {
189- this . setInStorage ( StorageKeys . OPT_OUT , cookieData . optOut ) ;
190- }
191- Cookie . remove ( this . options . cookieName ) ;
192- }
193-
194- // update local storage keys to prevent conflicts
195- var lastEventId = localStorage . getItem ( StorageKeys . LAST_EVENT_ID ) ;
196- if ( lastEventId ) {
197- this . setInStorage ( StorageKeys . LAST_EVENT_ID , lastEventId ) ;
198- localStorage . removeItem ( StorageKeys . LAST_EVENT_ID ) ;
199- }
200-
201- var lastIdentifyId = localStorage . getItem ( StorageKeys . LAST_IDENTIFY_ID ) ;
202- if ( lastIdentifyId ) {
203- this . setInStorage ( StorageKeys . LAST_IDENTIFY_ID , lastIdentifyId ) ;
204- localStorage . removeItem ( StorageKeys . LAST_IDENTIFY_ID ) ;
205- }
206-
207- var lastSequenceNumber = localStorage . getItem ( StorageKeys . LAST_SEQUENCE_NUMBER ) ;
208- if ( lastSequenceNumber ) {
209- this . setInStorage ( StorageKeys . LAST_SEQUENCE_NUMBER , lastSequenceNumber ) ;
210- localStorage . removeItem ( StorageKeys . LAST_SEQUENCE_NUMBER ) ;
211- }
212-
213- var lastEventTime = localStorage . getItem ( StorageKeys . LAST_EVENT_TIME ) ;
214- if ( lastEventTime ) {
215- this . setInStorage ( StorageKeys . LAST_EVENT_TIME , lastEventTime ) ;
216- localStorage . removeItem ( StorageKeys . LAST_EVENT_TIME ) ;
217- }
218-
219- var sessionId = localStorage . getItem ( StorageKeys . SESSION_ID ) ;
220- if ( sessionId ) {
221- this . setInStorage ( StorageKeys . SESSION_ID , sessionId ) ;
222- localStorage . removeItem ( StorageKeys . SESSION_ID ) ;
223- }
224-
225- var unsentEventsString = localStorage . getItem ( StorageKeys . UNSENT_EVENTS ) ;
226- if ( unsentEventsString ) {
227- this . setInStorage ( StorageKeys . UNSENT_EVENTS , unsentEventsString ) ;
228- localStorage . removeItem ( StorageKeys . UNSENT_EVENTS ) ;
229- }
230-
231- var unsentIdentifysString = localStorage . getItem ( StorageKeys . UNSENT_IDENTIFYS ) ;
232- if ( unsentIdentifysString ) {
233- this . setInStorage ( StorageKeys . UNSENT_IDENTIFYS , unsentIdentifysString ) ;
234- localStorage . removeItem ( StorageKeys . UNSENT_IDENTIFYS ) ;
235- }
236- } ;
237-
238- Amplitude . prototype . _loadStoredData = function ( ) {
239- if ( this . options . saveEvents ) {
240- this . _unsentEvents = this . _loadSavedUnsentEvents ( StorageKeys . UNSENT_EVENTS ) ;
241- this . _unsentIdentifys = this . _loadSavedUnsentEvents ( StorageKeys . UNSENT_IDENTIFYS ) ;
242- }
243-
244- try {
245- this . options . deviceId = this . getFromStorage ( StorageKeys . DEVICE_ID , this . options . deviceId ) ;
246- this . options . userId = this . getFromStorage ( StorageKeys . USER_ID , this . options . userId ) ;
247- this . options . optOut = ( this . getFromStorage ( StorageKeys . OPT_OUT , String ( this . options . optOut || false ) ) === 'true' ) ;
248-
249- this . _lastEventTime = parseInt ( this . getFromStorage ( StorageKeys . LAST_EVENT_TIME ) ) ;
250- this . _sessionId = parseInt ( this . getFromStorage ( StorageKeys . SESSION_ID ) ) ;
251- this . _eventId = parseInt ( this . getFromStorage ( StorageKeys . LAST_EVENT_ID , 0 ) ) ;
252- this . _identifyId = parseInt ( this . getFromStorage ( StorageKeys . LAST_IDENTIFY_ID , 0 ) ) ;
253- this . _sequenceNumber = parseInt ( this . getFromStorage ( StorageKeys . LAST_SEQUENCE_NUMBER , 0 ) ) ;
254-
255- var now = new Date ( ) . getTime ( ) ;
256- if ( ! this . _sessionId || ! this . _lastEventTime || now - this . _lastEventTime > this . options . sessionTimeout ) {
257- this . _newSession = true ;
258- this . _sessionId = now ;
259- this . setInStorage ( StorageKeys . SESSION_ID , this . _sessionId ) ;
260- }
261- this . _lastEventTime = now ;
262- this . setInStorage ( StorageKeys . LAST_EVENT_TIME , this . _lastEventTime ) ;
263- } catch ( e ) {
264- log ( e ) ;
265- }
266- } ;
267-
268- Amplitude . prototype . _loadSavedUnsentEvents = function ( unsentKey ) {
269- var savedUnsentEventsString = this . getFromStorage ( unsentKey ) ;
167+ Amplitude . prototype . _loadSavedUnsentEvents = function ( unsentKey , queue ) {
168+ var savedUnsentEventsString = localStorage . getItem ( unsentKey ) ;
270169 if ( savedUnsentEventsString ) {
271170 try {
272- return JSON . parse ( savedUnsentEventsString ) ;
171+ this [ queue ] = JSON . parse ( savedUnsentEventsString ) ;
273172 } catch ( e ) {
274173 //log(e);
275174 }
276175 }
277- return [ ] ;
278176} ;
279177
280178Amplitude . prototype . isNewSession = function ( ) {
@@ -330,6 +228,29 @@ Amplitude.prototype._sendEventsIfReady = function(callback) {
330228 return false ;
331229} ;
332230
231+ var _loadCookieData = function ( scope ) {
232+ var cookieData = Cookie . get ( scope . options . cookieName ) ;
233+ if ( cookieData ) {
234+ if ( cookieData . deviceId ) {
235+ scope . options . deviceId = cookieData . deviceId ;
236+ }
237+ if ( cookieData . userId ) {
238+ scope . options . userId = cookieData . userId ;
239+ }
240+ if ( cookieData . optOut !== undefined ) {
241+ scope . options . optOut = cookieData . optOut ;
242+ }
243+ }
244+ } ;
245+
246+ var _saveCookieData = function ( scope ) {
247+ Cookie . set ( scope . options . cookieName , {
248+ deviceId : scope . options . deviceId ,
249+ userId : scope . options . userId ,
250+ optOut : scope . options . optOut
251+ } ) ;
252+ } ;
253+
333254Amplitude . _getUtmParam = function ( name , query ) {
334255 name = name . replace ( / [ \[ ] / , "\\[" ) . replace ( / [ \] ] / , "\\]" ) ;
335256 var regex = new RegExp ( "[\\?&]" + name + "=([^&#]*)" ) ;
@@ -378,8 +299,8 @@ Amplitude.prototype._getReferringDomain = function() {
378299
379300Amplitude . prototype . saveEvents = function ( ) {
380301 try {
381- this . setInStorage ( StorageKeys . UNSENT_EVENTS , JSON . stringify ( this . _unsentEvents ) ) ;
382- this . setInStorage ( StorageKeys . UNSENT_IDENTIFYS , JSON . stringify ( this . _unsentIdentifys ) ) ;
302+ localStorage . setItem ( this . options . unsentKey , JSON . stringify ( this . _unsentEvents ) ) ;
303+ localStorage . setItem ( this . options . unsentIdentifyKey , JSON . stringify ( this . _unsentIdentifys ) ) ;
383304 } catch ( e ) {
384305 //log(e);
385306 }
@@ -391,10 +312,8 @@ Amplitude.prototype.setDomain = function(domain) {
391312 domain : domain
392313 } ) ;
393314 this . options . domain = Cookie . options ( ) . domain ;
394-
395- this . _upgradeStoredData ( ) ;
396- this . _loadStoredData ( ) ;
397-
315+ _loadCookieData ( this ) ;
316+ _saveCookieData ( this ) ;
398317 //log('set domain=' + domain);
399318 } catch ( e ) {
400319 log ( e ) ;
@@ -404,7 +323,7 @@ Amplitude.prototype.setDomain = function(domain) {
404323Amplitude . prototype . setUserId = function ( userId ) {
405324 try {
406325 this . options . userId = ( userId !== undefined && userId !== null && ( '' + userId ) ) || null ;
407- this . setInStorage ( StorageKeys . USER_ID , this . options . userId ) ;
326+ _saveCookieData ( this ) ;
408327 //log('set userId=' + userId);
409328 } catch ( e ) {
410329 log ( e ) ;
@@ -414,7 +333,7 @@ Amplitude.prototype.setUserId = function(userId) {
414333Amplitude . prototype . setOptOut = function ( enable ) {
415334 try {
416335 this . options . optOut = enable ;
417- this . setInStorage ( StorageKeys . OPT_OUT , this . options . optOut ) ;
336+ _saveCookieData ( this ) ;
418337 //log('set optOut=' + enable);
419338 } catch ( e ) {
420339 log ( e ) ;
@@ -425,7 +344,7 @@ Amplitude.prototype.setDeviceId = function(deviceId) {
425344 try {
426345 if ( deviceId ) {
427346 this . options . deviceId = ( '' + deviceId ) ;
428- this . setInStorage ( StorageKeys . DEVICE_ID , this . options . deviceId ) ;
347+ _saveCookieData ( this ) ;
429348 }
430349 } catch ( e ) {
431350 log ( e ) ;
@@ -515,19 +434,19 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
515434 var eventId ;
516435 if ( eventType === IDENTIFY_EVENT ) {
517436 eventId = this . nextIdentifyId ( ) ;
518- this . setInStorage ( StorageKeys . LAST_IDENTIFY_ID , eventId ) ;
437+ localStorage . setItem ( LocalStorageKeys . LAST_IDENTIFY_ID , eventId ) ;
519438 } else {
520439 eventId = this . nextEventId ( ) ;
521- this . setInStorage ( StorageKeys . LAST_EVENT_ID , eventId ) ;
440+ localStorage . setItem ( LocalStorageKeys . LAST_EVENT_ID , eventId ) ;
522441 }
523442 var eventTime = new Date ( ) . getTime ( ) ;
524443 var ua = this . _ua ;
525444 if ( ! this . _sessionId || ! this . _lastEventTime || eventTime - this . _lastEventTime > this . options . sessionTimeout ) {
526445 this . _sessionId = eventTime ;
527- this . setInStorage ( StorageKeys . SESSION_ID , this . _sessionId ) ;
446+ localStorage . setItem ( LocalStorageKeys . SESSION_ID , this . _sessionId ) ;
528447 }
529448 this . _lastEventTime = eventTime ;
530- this . setInStorage ( StorageKeys . LAST_EVENT_TIME , this . _lastEventTime ) ;
449+ localStorage . setItem ( LocalStorageKeys . LAST_EVENT_TIME , this . _lastEventTime ) ;
531450
532451 userProperties = userProperties || { } ;
533452 // Only add utm properties to user properties for events
0 commit comments