diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 6ac9402e..10089659 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -817,7 +817,7 @@ var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(i AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) { if (!this._apiKeySet('identify()')) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent'); + opt_callback(0, 'No request sent', {reason: 'API key is not set'}); } return; } @@ -833,13 +833,16 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) { return this._logEvent( Constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, opt_callback ); + } else { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', {reason: 'No user property operations'}); + } } } else { utils.log.error('Invalid identify input type. Expected Identify object but saw ' + type(identify_obj)); - } - - if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent'); + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', {reason: 'Invalid identify input type'}); + } } }; @@ -862,9 +865,15 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName) */ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, timestamp, callback) { _loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs - if (!eventType || this.options.optOut) { + if (!eventType) { + if (type(callback) === 'function') { + callback(0, 'No request sent', {reason: 'Missing eventType'}); + } + return; + } + if (this.options.optOut) { if (type(callback) === 'function') { - callback(0, 'No request sent'); + callback(0, 'No request sent', {reason: 'optOut is set to true'}); } return; } @@ -928,7 +937,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert } if (!this._sendEventsIfReady(callback) && type(callback) === 'function') { - callback(0, 'No request sent'); + callback(0, 'No request sent', {reason: 'No events to send or upload queued'}); } return eventId; @@ -979,10 +988,21 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie * @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15}); */ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) { - if (!this._apiKeySet('logEvent()') || !utils.validateInput(eventType, 'eventType', 'string') || - utils.isEmptyString(eventType)) { + if (!this._apiKeySet('logEvent()')) { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', {reason: 'API key not set'}); + } + return -1; + } + if (!utils.validateInput(eventType, 'eventType', 'string')) { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', {reason: 'Invalid type for eventType'}); + } + return -1; + } + if (utils.isEmptyString(eventType)) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent'); + opt_callback(0, 'No request sent', {reason: 'Missing eventType'}); } return -1; } @@ -1005,10 +1025,15 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e * @example amplitudeClient.logEventWithGroups('Clicked Button', null, {'orgId': 24}); */ AmplitudeClient.prototype.logEventWithGroups = function(eventType, eventProperties, groups, opt_callback) { - if (!this._apiKeySet('logEventWithGroup()') || - !utils.validateInput(eventType, 'eventType', 'string')) { + if (!this._apiKeySet('logEventWithGroups()')) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent'); + opt_callback(0, 'No request sent', {reason: 'API key not set'}); + } + return -1; + } + if (!utils.validateInput(eventType, 'eventType', 'string')) { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', {reason: 'Invalid type for eventType'}); } return -1; } @@ -1113,9 +1138,27 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) { * Note the server response code and response body are passed to the callback as input arguments. */ AmplitudeClient.prototype.sendEvents = function sendEvents(callback) { - if (!this._apiKeySet('sendEvents()') || this._sending || this.options.optOut || this._unsentCount() === 0) { + if (!this._apiKeySet('sendEvents()')) { + if (type(callback) === 'function') { + callback(0, 'No request sent', {reason: 'API key not set'}); + } + return; + } + if (this.options.optOut) { + if (type(callback) === 'function') { + callback(0, 'No request sent', {reason: 'optOut is set to true'}); + } + return; + } + if (this._unsentCount() === 0) { + if (type(callback) === 'function') { + callback(0, 'No request sent', {reason: 'No events to send'}); + } + return; + } + if (this._sending) { if (type(callback) === 'function') { - callback(0, 'No request sent'); + callback(0, 'No request sent', {reason: 'Request already in progress'}); } return; }