Skip to content

Commit f243a92

Browse files
fix: Bug with logEvent callbacks not being called when unsent events are dropped (#342)
* fixes #142 * fix error handling * revert commit * extend callback interface * fix callback call for deleted events * Added tests * single letter variable nit Co-authored-by: Donald Pipowitch <[email protected]>
1 parent 7f8631e commit f243a92

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/amplitude-client.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,14 @@ var _generateApiPropertiesTrackingConfig = function _generateApiPropertiesTracki
13841384
*/
13851385
AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue) {
13861386
if (queue.length > this.options.savedMaxCount) {
1387-
queue.splice(0, queue.length - this.options.savedMaxCount);
1387+
const deletedEvents = queue.splice(0, queue.length - this.options.savedMaxCount);
1388+
deletedEvents.forEach((event) => {
1389+
if (type(event.callback) === 'function') {
1390+
event.callback(0, 'No request sent', {
1391+
reason: 'Event dropped because options.savedMaxCount exceeded. User may be offline or have a content blocker',
1392+
});
1393+
}
1394+
});
13881395
}
13891396
};
13901397

@@ -1394,6 +1401,7 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue
13941401
* @callback Amplitude~eventCallback
13951402
* @param {number} responseCode - Server response code for the event / identify upload request.
13961403
* @param {string} responseBody - Server response body for the event / identify upload request.
1404+
* @param {object} details - (optional) Additional information associated with sending event.
13971405
*/
13981406

13991407
/**
@@ -1684,7 +1692,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
16841692
// here.
16851693
// }
16861694
} catch (e) {
1687-
// utils.log('failed upload');
1695+
// utils.log.error('failed upload');
16881696
}
16891697
});
16901698
};

test/amplitude-client.js

+27
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,33 @@ describe('AmplitudeClient', function () {
19451945
assert.equal(message, 'success');
19461946
});
19471947

1948+
it.only('should run the callback even with a dropped unsent event', function () {
1949+
amplitude.init(apiKey, null, { savedMaxCount: 1 });
1950+
var counter = 0;
1951+
var value = null;
1952+
var message = null;
1953+
var reason = null;
1954+
var callback = function (status, response, details) {
1955+
counter++;
1956+
value = status;
1957+
message = response;
1958+
reason = details.reason;
1959+
};
1960+
amplitude.logEvent('DroppedEvent', {}, callback);
1961+
amplitude.logEvent('SavedEvent', {}, callback);
1962+
server.respondWith([0, {}, '']);
1963+
server.respond();
1964+
1965+
// verify callback fired
1966+
assert.equal(counter, 1);
1967+
assert.equal(value, 0);
1968+
assert.equal(message, 'No request sent');
1969+
assert.equal(
1970+
reason,
1971+
'Event dropped because options.savedMaxCount exceeded. User may be offline or have a content blocker',
1972+
);
1973+
});
1974+
19481975
it('should run callback once and only after 413 resolved', function () {
19491976
var counter = 0;
19501977
var value = -1;

0 commit comments

Comments
 (0)