diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 14a91a8f..e5b49b24 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -1384,7 +1384,14 @@ var _generateApiPropertiesTrackingConfig = function _generateApiPropertiesTracki */ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue) { if (queue.length > this.options.savedMaxCount) { - queue.splice(0, queue.length - this.options.savedMaxCount); + const deletedEvents = queue.splice(0, queue.length - this.options.savedMaxCount); + deletedEvents.forEach((event) => { + if (type(event.callback) === 'function') { + event.callback(0, 'No request sent', { + reason: 'Event dropped because options.savedMaxCount exceeded. User may be offline or have a content blocker', + }); + } + }); } }; @@ -1394,6 +1401,7 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue * @callback Amplitude~eventCallback * @param {number} responseCode - Server response code for the event / identify upload request. * @param {string} responseBody - Server response body for the event / identify upload request. + * @param {object} details - (optional) Additional information associated with sending event. */ /** @@ -1684,7 +1692,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() { // here. // } } catch (e) { - // utils.log('failed upload'); + // utils.log.error('failed upload'); } }); }; diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 9788feb5..fdc8df1d 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -1945,6 +1945,33 @@ describe('AmplitudeClient', function () { assert.equal(message, 'success'); }); + it.only('should run the callback even with a dropped unsent event', function () { + amplitude.init(apiKey, null, { savedMaxCount: 1 }); + var counter = 0; + var value = null; + var message = null; + var reason = null; + var callback = function (status, response, details) { + counter++; + value = status; + message = response; + reason = details.reason; + }; + amplitude.logEvent('DroppedEvent', {}, callback); + amplitude.logEvent('SavedEvent', {}, callback); + server.respondWith([0, {}, '']); + server.respond(); + + // verify callback fired + assert.equal(counter, 1); + assert.equal(value, 0); + assert.equal(message, 'No request sent'); + assert.equal( + reason, + 'Event dropped because options.savedMaxCount exceeded. User may be offline or have a content blocker', + ); + }); + it('should run callback once and only after 413 resolved', function () { var counter = 0; var value = -1;