Skip to content

Commit 6a35d09

Browse files
authored
Add more context to the no request sent messages in callbacks (#145)
* Add more context to the no request sent messages in callbacks * PR feedback
1 parent feab9d8 commit 6a35d09

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

src/amplitude-client.js

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(i
817817
AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
818818
if (!this._apiKeySet('identify()')) {
819819
if (type(opt_callback) === 'function') {
820-
opt_callback(0, 'No request sent');
820+
opt_callback(0, 'No request sent', {reason: 'API key is not set'});
821821
}
822822
return;
823823
}
@@ -833,13 +833,16 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
833833
return this._logEvent(
834834
Constants.IDENTIFY_EVENT, null, null, identify_obj.userPropertiesOperations, null, null, opt_callback
835835
);
836+
} else {
837+
if (type(opt_callback) === 'function') {
838+
opt_callback(0, 'No request sent', {reason: 'No user property operations'});
839+
}
836840
}
837841
} else {
838842
utils.log.error('Invalid identify input type. Expected Identify object but saw ' + type(identify_obj));
839-
}
840-
841-
if (type(opt_callback) === 'function') {
842-
opt_callback(0, 'No request sent');
843+
if (type(opt_callback) === 'function') {
844+
opt_callback(0, 'No request sent', {reason: 'Invalid identify input type'});
845+
}
843846
}
844847
};
845848

@@ -862,9 +865,15 @@ AmplitudeClient.prototype.setVersionName = function setVersionName(versionName)
862865
*/
863866
AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventProperties, apiProperties, userProperties, groups, timestamp, callback) {
864867
_loadCookieData(this); // reload cookie before each log event to sync event meta-data between windows and tabs
865-
if (!eventType || this.options.optOut) {
868+
if (!eventType) {
869+
if (type(callback) === 'function') {
870+
callback(0, 'No request sent', {reason: 'Missing eventType'});
871+
}
872+
return;
873+
}
874+
if (this.options.optOut) {
866875
if (type(callback) === 'function') {
867-
callback(0, 'No request sent');
876+
callback(0, 'No request sent', {reason: 'optOut is set to true'});
868877
}
869878
return;
870879
}
@@ -928,7 +937,7 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
928937
}
929938

930939
if (!this._sendEventsIfReady(callback) && type(callback) === 'function') {
931-
callback(0, 'No request sent');
940+
callback(0, 'No request sent', {reason: 'No events to send or upload queued'});
932941
}
933942

934943
return eventId;
@@ -979,10 +988,21 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie
979988
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
980989
*/
981990
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) {
982-
if (!this._apiKeySet('logEvent()') || !utils.validateInput(eventType, 'eventType', 'string') ||
983-
utils.isEmptyString(eventType)) {
991+
if (!this._apiKeySet('logEvent()')) {
992+
if (type(opt_callback) === 'function') {
993+
opt_callback(0, 'No request sent', {reason: 'API key not set'});
994+
}
995+
return -1;
996+
}
997+
if (!utils.validateInput(eventType, 'eventType', 'string')) {
998+
if (type(opt_callback) === 'function') {
999+
opt_callback(0, 'No request sent', {reason: 'Invalid type for eventType'});
1000+
}
1001+
return -1;
1002+
}
1003+
if (utils.isEmptyString(eventType)) {
9841004
if (type(opt_callback) === 'function') {
985-
opt_callback(0, 'No request sent');
1005+
opt_callback(0, 'No request sent', {reason: 'Missing eventType'});
9861006
}
9871007
return -1;
9881008
}
@@ -1005,10 +1025,15 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e
10051025
* @example amplitudeClient.logEventWithGroups('Clicked Button', null, {'orgId': 24});
10061026
*/
10071027
AmplitudeClient.prototype.logEventWithGroups = function(eventType, eventProperties, groups, opt_callback) {
1008-
if (!this._apiKeySet('logEventWithGroup()') ||
1009-
!utils.validateInput(eventType, 'eventType', 'string')) {
1028+
if (!this._apiKeySet('logEventWithGroups()')) {
10101029
if (type(opt_callback) === 'function') {
1011-
opt_callback(0, 'No request sent');
1030+
opt_callback(0, 'No request sent', {reason: 'API key not set'});
1031+
}
1032+
return -1;
1033+
}
1034+
if (!utils.validateInput(eventType, 'eventType', 'string')) {
1035+
if (type(opt_callback) === 'function') {
1036+
opt_callback(0, 'No request sent', {reason: 'Invalid type for eventType'});
10121037
}
10131038
return -1;
10141039
}
@@ -1113,9 +1138,27 @@ var _removeEvents = function _removeEvents(scope, eventQueue, maxId) {
11131138
* Note the server response code and response body are passed to the callback as input arguments.
11141139
*/
11151140
AmplitudeClient.prototype.sendEvents = function sendEvents(callback) {
1116-
if (!this._apiKeySet('sendEvents()') || this._sending || this.options.optOut || this._unsentCount() === 0) {
1141+
if (!this._apiKeySet('sendEvents()')) {
1142+
if (type(callback) === 'function') {
1143+
callback(0, 'No request sent', {reason: 'API key not set'});
1144+
}
1145+
return;
1146+
}
1147+
if (this.options.optOut) {
1148+
if (type(callback) === 'function') {
1149+
callback(0, 'No request sent', {reason: 'optOut is set to true'});
1150+
}
1151+
return;
1152+
}
1153+
if (this._unsentCount() === 0) {
1154+
if (type(callback) === 'function') {
1155+
callback(0, 'No request sent', {reason: 'No events to send'});
1156+
}
1157+
return;
1158+
}
1159+
if (this._sending) {
11171160
if (type(callback) === 'function') {
1118-
callback(0, 'No request sent');
1161+
callback(0, 'No request sent', {reason: 'Request already in progress'});
11191162
}
11201163
return;
11211164
}

0 commit comments

Comments
 (0)