Skip to content

Commit

Permalink
Handle multiple trackers with shared state such that they share the p…
Browse files Browse the repository at this point in the history
…age view IDs for events tracked after each other
  • Loading branch information
matus-tomlein committed May 3, 2024
1 parent 1a33223 commit f0620b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
11 changes: 5 additions & 6 deletions libraries/browser-tracker-core/src/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export function Tracker(
preservePageViewId = false,
// Whether pageViewId should be kept the same until the page URL changes. Affects web_page context
preservePageViewIdForUrl = trackerConfiguration.preservePageViewIdForUrl ?? false,
// Whether first trackPageView was fired and pageViewId should not be changed anymore until reload
pageViewSent = false,
// The pageViewId of the last page view event or undefined if no page view tracked yet. Used to determine if pageViewId should be regenerated for a new page view.
lastSentPageViewId: string | undefined = undefined,
// Activity tracking config for callback and page ping variants
activityTrackingConfig: ActivityTrackingConfig = {
enabled: false,
Expand Down Expand Up @@ -734,7 +734,6 @@ export function Tracker(
if (shouldGenerateNewPageViewId()) {
state.pageViewId = uuid();
state.pageViewUrl = configCustomUrl || locationHrefAlias;
pageViewSent = false;
}
return state.pageViewId!;
}
Expand Down Expand Up @@ -980,11 +979,11 @@ export function Tracker(

function logPageView({ title, context, timestamp, contextCallback }: PageViewEvent & CommonEventProperties) {
refreshUrl();
if (pageViewSent) {
// Do not reset pageViewId if previous events were not page_view
if (lastSentPageViewId && lastSentPageViewId == getPageViewId()) {
// Do not reset pageViewId if a page view was not tracked yet or a different page view ID was used (in order to support multiple trackers with shared state)
resetPageView();
}
pageViewSent = true;
lastSentPageViewId = getPageViewId();

// So we know what document.title was at the time of trackPageView
lastDocumentTitle = document.title;
Expand Down
4 changes: 2 additions & 2 deletions libraries/browser-tracker-core/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function createTestSessionIdCookie(params?: CreateTestSessionIdCookie) {
return `_sp_ses.${domainHash}=*; Expires=; Path=/; SameSite=None; Secure;`;
}

export function createTracker(configuration?: TrackerConfiguration) {
export function createTracker(configuration?: TrackerConfiguration, sharedState?: SharedState) {
let id = 'sp-' + Math.random();
return addTracker(id, id, '', '', new SharedState(), configuration);
return addTracker(id, id, '', '', sharedState ?? new SharedState(), configuration);
}
51 changes: 51 additions & 0 deletions libraries/browser-tracker-core/test/tracker/page_view_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,55 @@ describe('Tracker API: page view IDs', () => {
expect(pageView1).not.toEqual(pageView2);
});
});

describe('Multiple trackers with a shared state', () => {
it('Keeps the page view ID for each page view pairs', () => {
const tracker1 = createTracker();
const tracker2 = createTracker(undefined, tracker1?.sharedState);

tracker1?.setCustomUrl('http://example.com/1');
tracker1?.trackPageView();
const pageView1 = tracker1?.getPageViewId();

tracker2?.trackPageView();
const pageView2 = tracker2?.getPageViewId();

expect(pageView1).toEqual(pageView2);

tracker1?.trackPageView();
const pageView3 = tracker1?.getPageViewId();

tracker2?.trackPageView();
const pageView4 = tracker2?.getPageViewId();

expect(pageView1).not.toEqual(pageView3);
expect(pageView3).toEqual(pageView4);
});

it("Doesn't keep the page view ID for multiple calls to one tracker", () => {
const tracker1 = createTracker();
const tracker2 = createTracker(undefined, tracker1?.sharedState);

tracker1?.setCustomUrl('http://example.com/1');
tracker1?.trackPageView();
const pageView1 = tracker1?.getPageViewId();
tracker1?.trackPageView();
const pageView2 = tracker1?.getPageViewId();

tracker2?.trackPageView();
const pageView3 = tracker2?.getPageViewId();

expect(pageView1).not.toEqual(pageView2);
expect(pageView2).toEqual(pageView3);

tracker1?.trackPageView();
const pageView4 = tracker1?.getPageViewId();

tracker2?.trackPageView();
const pageView5 = tracker2?.getPageViewId();

expect(pageView3).not.toEqual(pageView4);
expect(pageView4).toEqual(pageView5);
});
});
});

0 comments on commit f0620b9

Please sign in to comment.