Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ga4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ function track(...args: any[]) {

if (!trackingId) {
console.error('GA4: Tracking ID is missing or undefined');
return;
}

if (!navigator.sendBeacon) {
console.warn('GA4: navigator.sendBeacon is not supported in this browser');
return;
}

Expand Down
18 changes: 18 additions & 0 deletions packages/ga4/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ describe('ga4 -> track()', () => {
expect(navigator.sendBeacon).not.toBeCalled();
});

it.skip('logs an error message if sendBeacon is undefined', () => {
// It would be nice if we could test this, but sadly jest does not allow us to mock this
// property.
//
// Additionally, replaceProperty is only available in jest 29 and above, but since it doesn't
// work anyways we don't need this to force an upgrade.
//
// > Property `sendBeacon` is not declared configurable

// @ts-ignore
jest.replaceProperty(navigator, 'sendBeacon', undefined);

track(trackingId);

expect(errorSpy).toHaveBeenCalledWith(errorTrackingId);
expect(navigator.sendBeacon).not.toBeCalled();
});

it('can be called directly with a tracking ID', () => {
track(trackingId);

Expand Down
Loading