Skip to content

Commit 7902a98

Browse files
committed
add error callback logic to sendBeacon'
1 parent 0222136 commit 7902a98

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/amplitude-client.js

+2
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,8 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
17001700
if (this.options.saveEvents) {
17011701
this.saveEvents();
17021702
}
1703+
} else {
1704+
this._logErrorsOnEvents(maxEventId, maxIdentifyId, 0, '');
17031705
}
17041706
return;
17051707
}

test/amplitude-client.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -4051,22 +4051,30 @@ describe('AmplitudeClient', function () {
40514051

40524052
it('should use sendBeacon when beacon transport is set', function () {
40534053
sandbox.stub(navigator, 'sendBeacon').returns(true);
4054+
const callback = sandbox.spy();
4055+
const errCallback = sandbox.spy();
40544056

40554057
amplitude.init(apiKey, null, { transport: constants.TRANSPORT_BEACON });
4056-
amplitude.logEvent('test event');
4058+
amplitude.logEvent('test event', {}, callback, errCallback);
40574059

40584060
assert.equal(navigator.sendBeacon.callCount, 1);
40594061
assert.equal(amplitude._unsentEvents.length, 0);
4062+
assert.isTrue(callback.calledOnce);
4063+
assert.isFalse(errCallback.calledOnce);
40604064
});
40614065

40624066
it('should not remove event from unsentEvents if beacon returns false', function () {
40634067
sandbox.stub(navigator, 'sendBeacon').returns(false);
4068+
const callback = sandbox.spy();
4069+
const errCallback = sandbox.spy();
40644070

40654071
amplitude.init(apiKey, null, { transport: constants.TRANSPORT_BEACON });
4066-
amplitude.logEvent('test event');
4072+
amplitude.logEvent('test event', {}, callback, errCallback);
40674073

40684074
assert.equal(navigator.sendBeacon.callCount, 1);
40694075
assert.equal(amplitude._unsentEvents.length, 1);
4076+
assert.isFalse(callback.calledOnce);
4077+
assert.isTrue(errCallback.calledOnce);
40704078
});
40714079
});
40724080
});

0 commit comments

Comments
 (0)