Skip to content

Commit

Permalink
fix: jahilldev#43 allow param override
Browse files Browse the repository at this point in the history
  • Loading branch information
Tombarr committed Apr 16, 2023
1 parent b184afb commit 0bdb2f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/ga4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ const eventKeys = {
fileDownload: 'file_download',
};

/* -----------------------------------
*
* Merge
*
* -------------------------------- */

function merge<T>(eventParamsA: [T, T][], eventParamsB: [T, T][]) {
const paramMapA = new Map(eventParamsA);
const paramMapB = new Map(eventParamsB);
const mergedEventParams = new Map([...paramMapA, ...paramMapB]);

return Array.from(mergedEventParams.entries());
}

/* -----------------------------------
*
* Arguments
Expand All @@ -100,8 +114,8 @@ function getArguments(args: any[]): [string | undefined, IProps] {
*
* -------------------------------- */

function getEventMeta({ type = '', event }: Pick<IProps, 'type' | 'event'>) {
const searchString = (isBrowser) ? document.location.search : self.location.search;
function getEventMeta({ type = '', event }: Pick<IProps, 'type' | 'event'>): [string, string][] {
const searchString = root?.document?.location?.search || root?.location?.search || '';
const searchParams = new URLSearchParams(searchString);

const searchResults = searchTerms.some((term) =>
Expand All @@ -117,10 +131,10 @@ function getEventMeta({ type = '', event }: Pick<IProps, 'type' | 'event'>) {
];

if (event) {
eventParams = eventParams.concat(getEventParams(event));
eventParams = merge(eventParams as [string, string][], getEventParams(event) as [string, string][]);
}

return eventParams;
return eventParams as [string, string][];
}

/* -----------------------------------
Expand Down Expand Up @@ -153,7 +167,7 @@ function getQueryParams(trackingId: string, { type, event, debug }: IProps) {
[param.screenResolution, `${screen.width}x${screen.height}`],
];

params = params.concat(getEventMeta({ type, event }));
params = merge(params as [string, string][], getEventMeta({ type, event }));
params = params.filter(([, value]) => value);

return new URLSearchParams(params);
Expand Down Expand Up @@ -262,7 +276,7 @@ function onFocusEvent() {
function onVisibilityChange() {
const timeIndex = engagementTimes.length - 1;
const [, isHidden] = engagementTimes[timeIndex];
const stateIndex = ['hidden', 'visible'].indexOf(document.visibilityState);
const stateIndex = ['hidden', 'visible'].indexOf(root?.document?.visibilityState || '');
const isVisible = Boolean(stateIndex);

if (stateIndex === -1) {
Expand Down Expand Up @@ -298,7 +312,7 @@ const onScrollEvent = debounce((trackingId: string) => {
event: eventParams,
});

document.removeEventListener('scroll', scrollHandler);
root?.document?.removeEventListener('scroll', scrollHandler);
});

/* -----------------------------------
Expand Down
20 changes: 20 additions & 0 deletions packages/ga4/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ describe('ga4 -> track()', () => {
);
});

it('overrides existing params when set by user', () => {
const testClientId = '123456789';
const params = [
`${param.eventParam}.${param.clientId}=${testClientId}`,
];

track(trackingId, {
type: testEvent,
event: {
[`${param.eventParam}.${param.clientId}`]: testClientId,
},
});

expect(navigator.sendBeacon).toBeCalledTimes(1);

params.forEach((param) =>
expect(navigator.sendBeacon).toBeCalledWith(expect.stringContaining(param))
);
});

it('uses the supplied analytics endpoint if defined on the window', () => {
const testEndpoint = `${testUrl}/collect/${Math.random()}`;

Expand Down

0 comments on commit 0bdb2f9

Please sign in to comment.