Skip to content

Commit

Permalink
fix: fix the pathOnly option (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhao900914 authored Sep 12, 2024
1 parent aa1cc9d commit 2ac566e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,19 @@ const isCampaignEvent = (event: Event) => {

export const shouldTrackHistoryPageView = (
trackingOption: Options['trackHistoryChanges'],
newURL: string,
oldURL: string,
newURLStr: string,
oldURLStr: string,
): boolean => {
switch (trackingOption) {
case 'pathOnly':
return newURL.split('?')[0] !== oldURL.split('?')[0];
case 'pathOnly': {
if (oldURLStr == '') return true;
const newURL = new URL(newURLStr);
const oldURL = new URL(oldURLStr);
const newBaseStr = newURL.origin + newURL.pathname;
const oldBaseStr = oldURL.origin + oldURL.pathname;
return newBaseStr !== oldBaseStr;
}
default:
return newURL !== oldURL;
return newURLStr !== oldURLStr;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,21 @@ describe('pageViewTrackingPlugin', () => {
const url1 = 'https://www.example.com/path/to/page';
const url2 = 'https://www.example.com/path/to/page?query=1';
expect(shouldTrackHistoryPageView('all', url1, url2)).toBe(true);
expect(shouldTrackHistoryPageView('pathOnly', url1, url1)).toBe(false);
expect(shouldTrackHistoryPageView('pathOnly', url1, url2)).toBe(false);
});

test('shouldTrackHistoryPageView pathOnly option with hash route', () => {
const url1 = 'https://www.example.com/path/to/page';
const url2 = 'https://www.example.com/path/to/page#home';
expect(shouldTrackHistoryPageView('all', url1, url2)).toBe(true);
expect(shouldTrackHistoryPageView('pathOnly', url1, url2)).toBe(false);
});

test('shouldTrackHistoryPageView pathOnly option with null previous url', () => {
const url1 = 'https://www.example.com/path/to/page';
const url2 = '';
expect(shouldTrackHistoryPageView('all', url1, url2)).toBe(true);
expect(shouldTrackHistoryPageView('pathOnly', url1, url2)).toBe(true);
});
});

Expand Down

0 comments on commit 2ac566e

Please sign in to comment.