@@ -172,7 +172,6 @@ Amplitude.prototype.init = function init(apiKey, opt_userId, opt_config, opt_cal
172172 domain : this . options . domain
173173 } ) ;
174174 this . options . domain = this . cookieStorage . options ( ) . domain ;
175-
176175 _upgradeCookeData ( this ) ;
177176 _loadCookieData ( this ) ;
178177
@@ -384,8 +383,7 @@ Amplitude.prototype._sendEventsIfReady = function _sendEventsIfReady(callback) {
384383 // otherwise schedule an upload after 30s
385384 if ( ! this . _updateScheduled ) { // make sure we only schedule 1 upload
386385 this . _updateScheduled = true ;
387- setTimeout (
388- function ( ) {
386+ setTimeout ( function ( ) {
389387 this . _updateScheduled = false ;
390388 this . sendEvents ( ) ;
391389 } . bind ( this ) , this . options . eventUploadPeriodMillis
@@ -807,13 +805,9 @@ Amplitude.prototype.setVersionName = function setVersionName(versionName) {
807805 * @private
808806 */
809807Amplitude . prototype . _logEvent = function _logEvent ( eventType , eventProperties , apiProperties , userProperties , callback ) {
810- if ( type ( callback ) !== 'function' ) {
811- callback = null ;
812- }
813-
814808 _loadCookieData ( this ) ; // reload cookie before each log event to sync event meta-data between windows and tabs
815809 if ( ! eventType || this . options . optOut ) {
816- if ( callback ) {
810+ if ( type ( callback ) === 'function' ) {
817811 callback ( 0 , 'No request sent' ) ;
818812 }
819813 return ;
@@ -834,9 +828,9 @@ Amplitude.prototype._logEvent = function _logEvent(eventType, eventProperties, a
834828 this . _lastEventTime = eventTime ;
835829 _saveCookieData ( this ) ;
836830
837- userProperties = ( type ( userProperties ) === 'object' && userProperties ) || { } ;
831+ userProperties = userProperties || { } ;
838832 apiProperties = apiProperties || { } ;
839- eventProperties = ( type ( eventProperties ) === 'object' && eventProperties ) || { } ;
833+ eventProperties = eventProperties || { } ;
840834 var event = {
841835 device_id : this . options . deviceId ,
842836 user_id : this . options . userId ,
@@ -932,7 +926,7 @@ var _isNumber = function _isNumber(n) {
932926 */
933927Amplitude . prototype . logRevenue = function logRevenue ( price , quantity , product ) {
934928 // Test that the parameters are of the right type.
935- if ( ! this . _apiKeySet ( 'logRevenue()' ) || ! _isNumber ( price ) || quantity !== undefined && ! _isNumber ( quantity ) ) {
929+ if ( ! this . _apiKeySet ( 'logRevenue()' ) || ! _isNumber ( price ) || ( quantity !== undefined && ! _isNumber ( quantity ) ) ) {
936930 // utils.log('Price and quantity arguments to logRevenue must be numbers');
937931 return - 1 ;
938932 }
@@ -981,73 +975,68 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) {
981975 * Note the server response code and response body are passed to the callback as input arguments.
982976 */
983977Amplitude . prototype . sendEvents = function sendEvents ( callback ) {
984- if ( ! this . _apiKeySet ( 'sendEvents()' ) ) {
978+ if ( ! this . _apiKeySet ( 'sendEvents()' ) || this . _sending || this . options . optOut || this . _unsentCount ( ) === 0 ) {
985979 if ( type ( callback ) === 'function' ) {
986980 callback ( 0 , 'No request sent' ) ;
987981 }
988982 return ;
989983 }
990984
991- if ( ! this . _sending && ! this . options . optOut && this . _unsentCount ( ) > 0 ) {
992- this . _sending = true ;
993- var url = ( 'https:' === window . location . protocol ? 'https' : 'http' ) + '://' +
994- this . options . apiEndpoint + '/' ;
995-
996- // fetch events to send
997- var numEvents = Math . min ( this . _unsentCount ( ) , this . options . uploadBatchSize ) ;
998- var mergedEvents = this . _mergeEventsAndIdentifys ( numEvents ) ;
999- var maxEventId = mergedEvents . maxEventId ;
1000- var maxIdentifyId = mergedEvents . maxIdentifyId ;
1001- var events = JSON . stringify ( mergedEvents . eventsToSend ) ;
1002- var uploadTime = new Date ( ) . getTime ( ) ;
1003-
1004- var data = {
1005- client : this . options . apiKey ,
1006- e : events ,
1007- v : Constants . API_VERSION ,
1008- upload_time : uploadTime ,
1009- checksum : md5 ( Constants . API_VERSION + this . options . apiKey + events + uploadTime )
1010- } ;
985+ this . _sending = true ;
986+ var url = ( 'https:' === window . location . protocol ? 'https' : 'http' ) + '://' + this . options . apiEndpoint + '/' ;
987+
988+ // fetch events to send
989+ var numEvents = Math . min ( this . _unsentCount ( ) , this . options . uploadBatchSize ) ;
990+ var mergedEvents = this . _mergeEventsAndIdentifys ( numEvents ) ;
991+ var maxEventId = mergedEvents . maxEventId ;
992+ var maxIdentifyId = mergedEvents . maxIdentifyId ;
993+ var events = JSON . stringify ( mergedEvents . eventsToSend ) ;
994+ var uploadTime = new Date ( ) . getTime ( ) ;
995+
996+ var data = {
997+ client : this . options . apiKey ,
998+ e : events ,
999+ v : Constants . API_VERSION ,
1000+ upload_time : uploadTime ,
1001+ checksum : md5 ( Constants . API_VERSION + this . options . apiKey + events + uploadTime )
1002+ } ;
10111003
1012- var scope = this ;
1013- new Request ( url , data ) . send ( function ( status , response ) {
1014- scope . _sending = false ;
1015- try {
1016- if ( status === 200 && response === 'success' ) {
1017- scope . removeEvents ( maxEventId , maxIdentifyId ) ;
1004+ var scope = this ;
1005+ new Request ( url , data ) . send ( function ( status , response ) {
1006+ scope . _sending = false ;
1007+ try {
1008+ if ( status === 200 && response === 'success' ) {
1009+ scope . removeEvents ( maxEventId , maxIdentifyId ) ;
10181010
1019- // Update the event cache after the removal of sent events.
1020- if ( scope . options . saveEvents ) {
1021- scope . saveEvents ( ) ;
1022- }
1011+ // Update the event cache after the removal of sent events.
1012+ if ( scope . options . saveEvents ) {
1013+ scope . saveEvents ( ) ;
1014+ }
10231015
1024- // Send more events if any queued during previous send.
1025- if ( ! scope . _sendEventsIfReady ( callback ) && type ( callback ) === 'function' ) {
1026- callback ( status , response ) ;
1027- }
1016+ // Send more events if any queued during previous send.
1017+ if ( ! scope . _sendEventsIfReady ( callback ) && type ( callback ) === 'function' ) {
1018+ callback ( status , response ) ;
1019+ }
10281020
1029- // handle payload too large
1030- } else if ( status === 413 ) {
1031- // utils.log('request too large');
1032- // Can't even get this one massive event through. Drop it, even if it is an identify.
1033- if ( scope . options . uploadBatchSize === 1 ) {
1034- scope . removeEvents ( maxEventId , maxIdentifyId ) ;
1035- }
1021+ // handle payload too large
1022+ } else if ( status === 413 ) {
1023+ // utils.log('request too large');
1024+ // Can't even get this one massive event through. Drop it, even if it is an identify.
1025+ if ( scope . options . uploadBatchSize === 1 ) {
1026+ scope . removeEvents ( maxEventId , maxIdentifyId ) ;
1027+ }
10361028
1037- // The server complained about the length of the request. Backoff and try again.
1038- scope . options . uploadBatchSize = Math . ceil ( numEvents / 2 ) ;
1039- scope . sendEvents ( callback ) ;
1029+ // The server complained about the length of the request. Backoff and try again.
1030+ scope . options . uploadBatchSize = Math . ceil ( numEvents / 2 ) ;
1031+ scope . sendEvents ( callback ) ;
10401032
1041- } else if ( type ( callback ) === 'function' ) { // If server turns something like a 400
1042- callback ( status , response ) ;
1043- }
1044- } catch ( e ) {
1045- // utils.log('failed upload');
1033+ } else if ( type ( callback ) === 'function' ) { // If server turns something like a 400
1034+ callback ( status , response ) ;
10461035 }
1047- } ) ;
1048- } else if ( type ( callback ) === 'function' ) {
1049- callback ( 0 , 'No request sent' ) ;
1050- }
1036+ } catch ( e ) {
1037+ // utils.log('failed upload');
1038+ }
1039+ } ) ;
10511040} ;
10521041
10531042/**
0 commit comments