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

fix: add return type of proxied function #546

Merged
merged 2 commits into from
Aug 18, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface Options {
measurementIds?: string | string[];
}

type SendBeaconFn = typeof navigator.sendBeacon;

/**
* Returns an instance of `gaEventsForwarderPlugin`. Add this plugin to listen for events sent to Google Analytics,
* transform the events and send the events to Amplitude.
Expand Down Expand Up @@ -41,7 +43,7 @@ export const gaEventsForwarderPlugin = ({ measurementIds = [] }: Options = {}):
let amplitude: BrowserClient | undefined = undefined;
let logger: Logger | undefined = undefined;
let preSetupEventQueue: BaseEvent[] = [];
let sendBeacon: undefined | ((url: string | URL, data?: BodyInit | null | undefined) => boolean);
let sendBeacon: undefined | SendBeaconFn;

/**
* Creates proxy for `navigator.sendBeacon` immediately to start listening for events.
Expand All @@ -53,11 +55,11 @@ export const gaEventsForwarderPlugin = ({ measurementIds = [] }: Options = {}):

// eslint-disable-next-line @typescript-eslint/unbound-method
globalScope.navigator.sendBeacon = new Proxy(globalScope.navigator.sendBeacon, {
apply: (target, thisArg, [url, body]: [string, BodyInit | undefined]) => {
apply: (target: SendBeaconFn, thisArg: any, argArray: Parameters<SendBeaconFn>) => {
// Intercepts request and attempt to send to Amplitude
interceptRequest(url, body);
intercept.apply(thisArg, argArray);
// Execute sendBeacon
target.apply(thisArg, [url, body]);
return target.apply(thisArg, argArray);
},
});
}
Expand All @@ -68,7 +70,7 @@ export const gaEventsForwarderPlugin = ({ measurementIds = [] }: Options = {}):
* 3a: Pushes to preSetupEventQueue while waiting for Amplitude SDK to initialize
* 3b. Sends events to Amplitude after Amplitude SDK is initialized
*/
const interceptRequest = (requestUrl: string, data?: BodyInit) => {
const intercept: SendBeaconFn = (requestUrl, data) => {
try {
const url = new URL(requestUrl);
if (
Expand All @@ -87,9 +89,11 @@ export const gaEventsForwarderPlugin = ({ measurementIds = [] }: Options = {}):
}
}
}
return true;
} catch (error) {
/* istanbul ignore next */
logger?.error(String(error));
return false;
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ga-events-forwarder-browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type GA4Event = Record<string, string | number>;
* @param data The request payload. This exists when multiple events are sent in a single request.
* @returns A list of deserialized Google Analytics events.
*/
export const parseGA4Events = (url: URL, data?: BodyInit): GA4Event[] => {
export const parseGA4Events = (url: URL, data?: BodyInit | null): GA4Event[] => {
const sharedProperties: GA4Event = {};

for (const entry of url.searchParams.entries()) {
Expand Down