diff --git a/packages/plugin-page-view-tracking-browser/src/page-view-tracking.ts b/packages/plugin-page-view-tracking-browser/src/page-view-tracking.ts index 96bf9a771..a985c69a6 100644 --- a/packages/plugin-page-view-tracking-browser/src/page-view-tracking.ts +++ b/packages/plugin-page-view-tracking-browser/src/page-view-tracking.ts @@ -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; } }; diff --git a/packages/plugin-page-view-tracking-browser/test/page-view-tracking.test.ts b/packages/plugin-page-view-tracking-browser/test/page-view-tracking.test.ts index 7c1e0b8d1..75c429791 100644 --- a/packages/plugin-page-view-tracking-browser/test/page-view-tracking.test.ts +++ b/packages/plugin-page-view-tracking-browser/test/page-view-tracking.test.ts @@ -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); }); });