@@ -127,9 +127,8 @@ var DEFAULT_OPTIONS = require('./options');
127127 * @example var amplitude = new Amplitude();
128128 */
129129var Amplitude = function Amplitude ( ) {
130+ this . cookieStorage = new cookieStorage ( ) . getStorage ( ) ;
130131 this . options = object . merge ( { } , DEFAULT_OPTIONS ) ;
131-
132- this . _cookieStorage = new cookieStorage ( ) . getStorage ( ) ;
133132 this . _q = [ ] ; // queue for proxied functions before script load
134133 this . _sending = false ;
135134 this . _ua = new UAParser ( navigator . userAgent ) . getResult ( ) ;
@@ -152,7 +151,7 @@ Amplitude.prototype.Identify = Identify;
152151 * Run functions queued up by proxy loading snippet
153152 * @private
154153 */
155- Amplitude . prototype . _runQueuedFunctions = function ( ) {
154+ Amplitude . prototype . runQueuedFunctions = function ( ) {
156155 for ( var i = 0 ; i < this . _q . length ; i ++ ) {
157156 var fn = this [ this . _q [ i ] [ 0 ] ] ;
158157 if ( type ( fn ) === 'function' ) {
@@ -177,11 +176,11 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
177176 this . options . apiKey = ( type ( apiKey ) === 'string' && ! utils . isEmptyString ( apiKey ) && apiKey ) || null ;
178177
179178 _parseConfig ( this . options , opt_config ) ;
180- this . _cookieStorage . options ( {
179+ this . cookieStorage . options ( {
181180 expirationDays : this . options . cookieExpiration ,
182181 domain : this . options . domain
183182 } ) ;
184- this . options . domain = this . _cookieStorage . options ( ) . domain ;
183+ this . options . domain = this . cookieStorage . options ( ) . domain ;
185184
186185 _upgradeCookeData ( this ) ;
187186 _loadCookieData ( this ) ;
@@ -215,7 +214,7 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
215214 }
216215
217216 if ( this . options . includeUtm ) {
218- this . _saveUtmData ( ) ;
217+ this . _initUtmData ( ) ;
219218 }
220219 if ( this . options . includeReferrer ) {
221220 this . _saveReferrer ( this . _getReferrer ( ) ) ;
@@ -319,7 +318,7 @@ Amplitude.prototype.getSessionId = function getSessionId() {
319318 * Increments the eventId and returns it.
320319 * @private
321320 */
322- Amplitude . prototype . _nextEventId = function _nextEventId ( ) {
321+ Amplitude . prototype . nextEventId = function nextEventId ( ) {
323322 this . _eventId ++ ;
324323 return this . _eventId ;
325324} ;
@@ -328,7 +327,7 @@ Amplitude.prototype._nextEventId = function _nextEventId() {
328327 * Increments the identifyId and returns it.
329328 * @private
330329 */
331- Amplitude . prototype . _nextIdentifyId = function _nextIdentifyId ( ) {
330+ Amplitude . prototype . nextIdentifyId = function nextIdentifyId ( ) {
332331 this . _identifyId ++ ;
333332 return this . _identifyId ;
334333} ;
@@ -337,7 +336,7 @@ Amplitude.prototype._nextIdentifyId = function _nextIdentifyId() {
337336 * Increments the sequenceNumber and returns it.
338337 * @private
339338 */
340- Amplitude . prototype . _nextSequenceNumber = function _nextSequenceNumber ( ) {
339+ Amplitude . prototype . nextSequenceNumber = function nextSequenceNumber ( ) {
341340 this . _sequenceNumber ++ ;
342341 return this . _sequenceNumber ;
343342} ;
@@ -385,6 +384,26 @@ Amplitude.prototype._sendEventsIfReady = function _sendEventsIfReady(callback) {
385384 return false ; // an upload was scheduled, no events were uploaded
386385} ;
387386
387+ /**
388+ * Helper function to fetch values from storage
389+ * Storage argument allows for localStoraoge and sessionStoraoge
390+ * @private
391+ * @deprecated
392+ */
393+ Amplitude . prototype . _getFromStorage = function _getFromStorage ( storage , key ) {
394+ return storage . getItem ( key ) ;
395+ } ;
396+
397+ /**
398+ * Helper function to set values in storage
399+ * Storage argument allows for localStoraoge and sessionStoraoge
400+ * @private
401+ * @deprecated
402+ */
403+ Amplitude . prototype . _setInStorage = function _setInStorage ( storage , key ) {
404+ storage . setItem ( key ) ;
405+ } ;
406+
388407/**
389408 * cookieData (deviceId, userId, optOut, sessionId, lastEventTime, eventId, identifyId, sequenceNumber)
390409 * can be stored in many different places (localStorage, cookie, etc).
@@ -393,7 +412,7 @@ Amplitude.prototype._sendEventsIfReady = function _sendEventsIfReady(callback) {
393412 */
394413var _upgradeCookeData = function _upgradeCookeData ( scope ) {
395414 // skip if migration already happened
396- var cookieData = scope . _cookieStorage . get ( scope . options . cookieName ) ;
415+ var cookieData = scope . cookieStorage . get ( scope . options . cookieName ) ;
397416 if ( type ( cookieData ) === 'object' && cookieData . deviceId && cookieData . sessionId && cookieData . lastEventTime ) {
398417 return ;
399418 }
@@ -445,7 +464,7 @@ var _upgradeCookeData = function _upgradeCookeData(scope) {
445464 * @private
446465 */
447466var _loadCookieData = function _loadCookieData ( scope ) {
448- var cookieData = scope . _cookieStorage . get ( scope . options . cookieName ) ;
467+ var cookieData = scope . cookieStorage . get ( scope . options . cookieName ) ;
449468 if ( type ( cookieData ) === 'object' ) {
450469 if ( cookieData . deviceId ) {
451470 scope . options . deviceId = cookieData . deviceId ;
@@ -479,7 +498,7 @@ var _loadCookieData = function _loadCookieData(scope) {
479498 * @private
480499 */
481500var _saveCookieData = function _saveCookieData ( scope ) {
482- scope . _cookieStorage . set ( scope . options . cookieName , {
501+ scope . cookieStorage . set ( scope . options . cookieName , {
483502 deviceId : scope . options . deviceId ,
484503 userId : scope . options . userId ,
485504 optOut : scope . options . optOut ,
@@ -496,9 +515,9 @@ var _saveCookieData = function _saveCookieData(scope) {
496515 * Since user properties are propagated on server, only send once per session, don't need to send with every event
497516 * @private
498517 */
499- Amplitude . prototype . _saveUtmData = function _saveUtmData ( queryParams , cookieParams ) {
518+ Amplitude . prototype . _initUtmData = function _initUtmData ( queryParams , cookieParams ) {
500519 queryParams = queryParams || location . search ;
501- cookieParams = cookieParams || this . _cookieStorage . get ( '__utmz' ) ;
520+ cookieParams = cookieParams || this . cookieStorage . get ( '__utmz' ) ;
502521 var utmProperties = getUtmData ( cookieParams , queryParams ) ;
503522
504523 // always setOnce initial utm params
@@ -537,7 +556,7 @@ Amplitude.prototype._getReferrer = function _getReferrer() {
537556 * Parse the domain from referrer info
538557 * @private
539558 */
540- var _getReferringDomain = function _getReferringDomain ( referrer ) {
559+ Amplitude . prototype . _getReferringDomain = function _getReferringDomain ( referrer ) {
541560 if ( referrer === null || referrer === undefined || referrer === '' ) {
542561 return null ;
543562 }
@@ -559,7 +578,7 @@ Amplitude.prototype._saveReferrer = function _saveReferrer(referrer) {
559578 }
560579
561580 // always setOnce initial referrer
562- var referring_domain = _getReferringDomain ( referrer ) ;
581+ var referring_domain = this . _getReferringDomain ( referrer ) ;
563582 var identify = new Identify ( ) . setOnce ( 'initial_referrer' , referrer ) ;
564583 identify . setOnce ( 'initial_referring_domain' , referring_domain ) ;
565584
@@ -607,10 +626,10 @@ Amplitude.prototype.setDomain = function setDomain(domain) {
607626 }
608627
609628 try {
610- this . _cookieStorage . options ( {
629+ this . cookieStorage . options ( {
611630 domain : domain
612631 } ) ;
613- this . options . domain = this . _cookieStorage . options ( ) . domain ;
632+ this . options . domain = this . cookieStorage . options ( ) . domain ;
614633 _loadCookieData ( this ) ;
615634 _saveCookieData ( this ) ;
616635 } catch ( e ) {
@@ -715,14 +734,14 @@ Amplitude.prototype.clearUserProperties = function clearUserProperties(){
715734
716735/**
717736 * Send an identify call containing user property operations to Amplitude servers.
718- * See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#user-properties-and-user-property-operations} for more information on user property operations.
719- * @param {object } identify - the Identify object containing the user property operations to send.
737+ * See [Readme]{@link https://github.com/amplitude/Amplitude-Javascript#user-properties-and-user-property-operations} for more information on the Identify API and user property operations.
738+ * @param {Identify_object } identify_obj - the Identify object containing the user property operations to send.
720739 * @param {function } opt_callback - (optional) callback function to run when the identify event has been sent. Note: the server response code and response body from the identify event upload are passed to the callback function.
721740 * @example
722741 * var identify = new amplitude.Identify().set('colors', ['rose', 'gold']).add('karma', 1).setOnce('sign_up_date', '2016-03-31');
723742 * amplitude.identify(identify);
724743 */
725- Amplitude . prototype . identify = function ( identify , opt_callback ) {
744+ Amplitude . prototype . identify = function ( identify_obj , opt_callback ) {
726745 if ( ! this . _apiKeySet ( 'identify()' ) ) {
727746 if ( type ( opt_callback ) === 'function' ) {
728747 opt_callback ( 0 , 'No request sent' ) ;
@@ -731,24 +750,24 @@ Amplitude.prototype.identify = function(identify, opt_callback) {
731750 }
732751
733752 // if identify input is a proxied object created by the async loading snippet, convert it into an identify object
734- if ( type ( identify ) === 'object' && identify . hasOwnProperty ( '_q' ) ) {
753+ if ( type ( identify_obj ) === 'object' && identify_obj . hasOwnProperty ( '_q' ) ) {
735754 var instance = new Identify ( ) ;
736- for ( var i = 0 ; i < identify . _q . length ; i ++ ) {
737- var fn = instance [ identify . _q [ i ] [ 0 ] ] ;
755+ for ( var i = 0 ; i < identify_obj . _q . length ; i ++ ) {
756+ var fn = instance [ identify_obj . _q [ i ] [ 0 ] ] ;
738757 if ( type ( fn ) === 'function' ) {
739- fn . apply ( instance , identify . _q [ i ] . slice ( 1 ) ) ;
758+ fn . apply ( instance , identify_obj . _q [ i ] . slice ( 1 ) ) ;
740759 }
741760 }
742- identify = instance ;
761+ identify_obj = instance ;
743762 }
744763
745- if ( identify instanceof Identify ) {
764+ if ( identify_obj instanceof Identify ) {
746765 // only send if there are operations
747- if ( Object . keys ( identify . userPropertiesOperations ) . length > 0 ) {
748- return this . _logEvent ( Constants . IDENTIFY_EVENT , null , null , identify . userPropertiesOperations , opt_callback ) ;
766+ if ( Object . keys ( identify_obj . userPropertiesOperations ) . length > 0 ) {
767+ return this . _logEvent ( Constants . IDENTIFY_EVENT , null , null , identify_obj . userPropertiesOperations , opt_callback ) ;
749768 }
750769 } else {
751- utils . log ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify ) ) ;
770+ utils . log ( 'Invalid identify input type. Expected Identify object but saw ' + type ( identify_obj ) ) ;
752771 }
753772
754773 if ( type ( opt_callback ) === 'function' ) {
@@ -789,11 +808,11 @@ Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, a
789808 try {
790809 var eventId ;
791810 if ( eventType === Constants . IDENTIFY_EVENT ) {
792- eventId = this . _nextIdentifyId ( ) ;
811+ eventId = this . nextIdentifyId ( ) ;
793812 } else {
794- eventId = this . _nextEventId ( ) ;
813+ eventId = this . nextEventId ( ) ;
795814 }
796- var sequenceNumber = this . _nextSequenceNumber ( ) ;
815+ var sequenceNumber = this . nextSequenceNumber ( ) ;
797816 var eventTime = new Date ( ) . getTime ( ) ;
798817 if ( ! this . _sessionId || ! this . _lastEventTime || eventTime - this . _lastEventTime > this . options . sessionTimeout ) {
799818 this . _sessionId = eventTime ;
@@ -912,22 +931,31 @@ Amplitude.prototype.logRevenue = function logRevenue(price, quantity, product) {
912931} ;
913932
914933/**
915- * Remove events in storage with event ids up to and including maxEventId. Does
916- * a true filter in case events get out of order or old events are removed.
934+ * Remove events in storage with event ids up to and including maxEventId.
935+ * @private
936+ */
937+ Amplitude . prototype . removeEvents = function removeEvents ( maxEventId , maxIdentifyId ) {
938+ _removeEvents ( this , '_unsentEvents' , maxEventId ) ;
939+ _removeEvents ( this , '_unsentIdentifys' , maxIdentifyId ) ;
940+ } ;
941+
942+ /**
943+ * Helper function to remove events up to maxId from a single queue.
944+ * Does a true filter in case events get out of order or old events are removed.
917945 * @private
918946 */
919- Amplitude . prototype . _removeEvents = function _removeEvents ( eventQueue , maxId ) {
947+ var _removeEvents = function _removeEvents ( scope , eventQueue , maxId ) {
920948 if ( maxId < 0 ) {
921949 return ;
922950 }
923951
924952 var filteredEvents = [ ] ;
925- for ( var i = 0 ; i < this [ eventQueue ] . length || 0 ; i ++ ) {
926- if ( this [ eventQueue ] [ i ] . event_id > maxId ) {
927- filteredEvents . push ( this [ eventQueue ] [ i ] ) ;
953+ for ( var i = 0 ; i < scope [ eventQueue ] . length || 0 ; i ++ ) {
954+ if ( scope [ eventQueue ] [ i ] . event_id > maxId ) {
955+ filteredEvents . push ( scope [ eventQueue ] [ i ] ) ;
928956 }
929957 }
930- this [ eventQueue ] = filteredEvents ;
958+ scope [ eventQueue ] = filteredEvents ;
931959} ;
932960
933961/**
@@ -969,8 +997,7 @@ Amplitude.prototype.sendEvents = function sendEvents(callback) {
969997 scope . _sending = false ;
970998 try {
971999 if ( status === 200 && response === 'success' ) {
972- scope . _removeEvents ( '_unsentEvents' , maxEventId ) ;
973- scope . _removeEvents ( '_unsentIdentifys' , maxIdentifyId ) ;
1000+ scope . removeEvents ( maxEventId , maxIdentifyId ) ;
9741001
9751002 // Update the event cache after the removal of sent events.
9761003 if ( scope . options . saveEvents ) {
@@ -987,8 +1014,7 @@ Amplitude.prototype.sendEvents = function sendEvents(callback) {
9871014 // utils.log('request too large');
9881015 // Can't even get this one massive event through. Drop it, even if it is an identify.
9891016 if ( scope . options . uploadBatchSize === 1 ) {
990- scope . _removeEvents ( '_unsentEvents' , maxEventId ) ;
991- scope . _removeEvents ( '_unsentIdentifys' , maxIdentifyId ) ;
1017+ scope . removeEvents ( maxEventId , maxIdentifyId ) ;
9921018 }
9931019
9941020 // The server complained about the length of the request. Backoff and try again.
0 commit comments