Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to use navigator.sendBeacon #45

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions src/lib/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ let xhrMockClass: ReturnType<typeof getXhrMockClass>;

const xmr = jest.spyOn(window, 'XMLHttpRequest');

Object.assign(navigator, {
sendBeacon() {
// just making node aware this function exists
},
});
const sendBeacon = jest.spyOn(navigator, 'sendBeacon');

const defaultData: Required<PlausibleOptions> = {
hashMode: false,
trackLocalhost: false,
Expand Down Expand Up @@ -78,6 +85,25 @@ describe('sendEvent', () => {

expect(xhrMockClass.send).toHaveBeenCalledWith(JSON.stringify(payload));
});
test('sends via Navigator#sendBeacon', () => {
expect(sendBeacon).not.toHaveBeenCalled();
sendEvent('myEvent', defaultData, { useSendBeacon: true });
expect(sendBeacon).toHaveBeenCalledTimes(1);

const payload = {
n: 'myEvent',
u: defaultData.url,
d: defaultData.domain,
r: defaultData.referrer,
w: defaultData.deviceWidth,
h: 0,
};

expect(sendBeacon).toHaveBeenCalledWith(
`${defaultData.apiHost}/api/event`,
JSON.stringify(payload)
);
});
test('hash mode', () => {
expect(xmr).not.toHaveBeenCalled();
sendEvent('myEvent', { ...defaultData, hashMode: true });
Expand Down
32 changes: 20 additions & 12 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ type EventPayload = {
readonly p?: string;
};

// eslint-disable-next-line functional/no-mixed-type
export type EventOptions = {
/**
* Callback called when the event is successfully sent.
* Does not work with `useSendBeacon = true`.
*/
readonly callback?: () => void;
/**
* Properties to be bound to the event.
*/
readonly props?: { readonly [propName: string]: string | number | boolean };
/**
* Whether to use `Navigator#sendBeacon`.
*/
readonly useSendBeacon?: boolean;
};

/**
Expand Down Expand Up @@ -68,15 +72,19 @@ export function sendEvent(
p: options && options.props ? JSON.stringify(options.props) : undefined,
};

const req = new XMLHttpRequest();
req.open('POST', `${data.apiHost}/api/event`, true);
req.setRequestHeader('Content-Type', 'text/plain');
req.send(JSON.stringify(payload));
// eslint-disable-next-line functional/immutable-data
req.onreadystatechange = () => {
if (req.readyState !== 4) return;
if (options && options.callback) {
options.callback();
}
};
if (!options?.useSendBeacon) {
const req = new XMLHttpRequest();
req.open('POST', `${data.apiHost}/api/event`, true);
req.setRequestHeader('Content-Type', 'text/plain');
req.send(JSON.stringify(payload));
// eslint-disable-next-line functional/immutable-data
req.onreadystatechange = () => {
if (req.readyState !== 4) return;
if (options && options.callback) {
options.callback();
}
};
} else {
navigator.sendBeacon(`${data.apiHost}/api/event`, JSON.stringify(payload));
}
}
1 change: 1 addition & 0 deletions src/lib/tracker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('tracker', () => {
variation3: 1,
variation4: true,
},
useSendBeacon: false,
});

test('inits with default config', () => {
Expand Down