Skip to content

Commit

Permalink
fix: Handle missing pageId in metadata when page is resumed (#388)
Browse files Browse the repository at this point in the history

Co-authored-by: Quinn Hanam <qhanam@gmail.com>

---------

Co-authored-by: Quinn Hanam <qhanam@gmail.com>
  • Loading branch information
ps863 and qhanam committed Mar 16, 2023
1 parent bc1c15f commit f81bcf2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/sessions/PageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ export class PageManager {
this.createLandingPage(pageId);
} else if (this.page.pageId !== pageId) {
this.createNextPage(this.page, pageId);
} else if (this.resumed) {
// Update attributes state in PageManager for event metadata
this.collectAttributes(
this.page as Page,
typeof payload === 'object' ? payload : undefined
);
return;
} else {
// The view has not changed.
return;
Expand Down
48 changes: 48 additions & 0 deletions src/sessions/__tests__/PageManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,54 @@ describe('PageManager tests', () => {
// Assert
expect(pageManager.getPage()?.interaction).toEqual(1);

// Assert
expect(pageManager.getAttributes()).toMatchObject({
pageId: '/console/home',
interaction: 1
});

window.removeEventListener(
'popstate',
(pageManager as any).popstateListener
);
});

test('when session resumes and page is manually recorded with custom page attributes then custom page attributes are restored', async () => {
// Init
const pageManager: PageManager = new PageManager(
{
...DEFAULT_CONFIG,
allowCookies: true
},
record
);

pageManager.resumeSession({
pageId: '/console/home',
interaction: 1,
start: Date.now()
});

pageManager.recordPageView({
pageId: '/console/home',
pageTags: ['pageGroup1'],
pageAttributes: {
customPageAttributeString: 'customPageAttributeValue',
customPageAttributeNumber: 1,
customPageAttributeBoolean: true
}
});

// Assert
expect(record.mock.calls).toHaveLength(0); // No event emitted, but attributes restored in PageManager state
expect(pageManager.getAttributes()).toMatchObject({
pageId: '/console/home',
pageTags: ['pageGroup1'],
customPageAttributeString: 'customPageAttributeValue',
customPageAttributeNumber: 1,
customPageAttributeBoolean: true
});

window.removeEventListener(
'popstate',
(pageManager as any).popstateListener
Expand Down

0 comments on commit f81bcf2

Please sign in to comment.