From 956e5aa3a47c322ac6401f3efb14f9e0721caa71 Mon Sep 17 00:00:00 2001 From: Krishna Rajendran Date: Mon, 16 Jul 2018 10:42:31 -0700 Subject: [PATCH 1/2] Add more context to the no request sent messages in callbacks --- src/amplitude-client.js | 75 ++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 6ac9402e..24480dbd 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', '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', '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', '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', 'Missing eventType'); + } + return; + } + if (this.options.optOut) { if (type(callback) === 'function') { - callback(0, 'No request sent'); + callback(0, 'No request sent', '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', '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', 'API key not set'); + } + return -1; + } + if (!utils.validateInput(eventType, 'eventType', 'string')) { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', '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', '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('logEventWithGroup()')) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent'); + opt_callback(0, 'No request sent', 'API key not set'); + } + return -1; + } + if (!utils.validateInput(eventType, 'eventType', 'string')) { + if (type(opt_callback) === 'function') { + opt_callback(0, 'No request sent', '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', 'API key not set'); + } + return; + } + if (this.options.optOut) { + if (type(callback) === 'function') { + callback(0, 'No request sent', 'optOut is set to true'); + } + return; + } + if (this._unsentCount() === 0) { + if (type(callback) === 'function') { + callback(0, 'No request sent', 'No events to send'); + } + return; + } + if (this._sending) { if (type(callback) === 'function') { - callback(0, 'No request sent'); + callback(0, 'No request sent', 'Request already in progress'); } return; } From 5f0c71df10f5ea2621a5d413a164fe756389f2cb Mon Sep 17 00:00:00 2001 From: Krishna Rajendran Date: Mon, 16 Jul 2018 12:53:30 -0700 Subject: [PATCH 2/2] PR feedback --- src/amplitude-client.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 24480dbd..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', 'API key is not set'); + opt_callback(0, 'No request sent', {reason: 'API key is not set'}); } return; } @@ -835,13 +835,13 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) { ); } else { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent', 'No user property operations'); + 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', 'Invalid identify input type'); + opt_callback(0, 'No request sent', {reason: 'Invalid identify input type'}); } } }; @@ -867,13 +867,13 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert _loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs if (!eventType) { if (type(callback) === 'function') { - callback(0, 'No request sent', 'Missing eventType'); + callback(0, 'No request sent', {reason: 'Missing eventType'}); } return; } if (this.options.optOut) { if (type(callback) === 'function') { - callback(0, 'No request sent', 'optOut is set to true'); + callback(0, 'No request sent', {reason: 'optOut is set to true'}); } return; } @@ -937,7 +937,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert } if (!this._sendEventsIfReady(callback) && type(callback) === 'function') { - callback(0, 'No request sent', 'No events to send or upload queued'); + callback(0, 'No request sent', {reason: 'No events to send or upload queued'}); } return eventId; @@ -990,19 +990,19 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) { if (!this._apiKeySet('logEvent()')) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent', 'API key not set'); + 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', 'Invalid type for eventType'); + 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', 'Missing eventType'); + opt_callback(0, 'No request sent', {reason: 'Missing eventType'}); } return -1; } @@ -1025,15 +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()')) { + if (!this._apiKeySet('logEventWithGroups()')) { if (type(opt_callback) === 'function') { - opt_callback(0, 'No request sent', 'API key not set'); + 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', 'Invalid type for eventType'); + opt_callback(0, 'No request sent', {reason: 'Invalid type for eventType'}); } return -1; } @@ -1140,25 +1140,25 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) { AmplitudeClient.prototype.sendEvents = function sendEvents(callback) { if (!this._apiKeySet('sendEvents()')) { if (type(callback) === 'function') { - callback(0, 'No request sent', 'API key not set'); + callback(0, 'No request sent', {reason: 'API key not set'}); } return; } if (this.options.optOut) { if (type(callback) === 'function') { - callback(0, 'No request sent', 'optOut is set to true'); + 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', 'No events to send'); + callback(0, 'No request sent', {reason: 'No events to send'}); } return; } if (this._sending) { if (type(callback) === 'function') { - callback(0, 'No request sent', 'Request already in progress'); + callback(0, 'No request sent', {reason: 'Request already in progress'}); } return; }