Skip to content

Commit 826d8c5

Browse files
ps863qhanam
andcommitted
fix: Handle missing pageId in metadata when page is resumed (#388)
Co-authored-by: Quinn Hanam <[email protected]> --------- Co-authored-by: Quinn Hanam <[email protected]>
1 parent f0897b8 commit 826d8c5

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/sessions/PageManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ export class PageManager {
107107
this.createLandingPage(pageId);
108108
} else if (this.page.pageId !== pageId) {
109109
this.createNextPage(this.page, pageId);
110+
} else if (this.resumed) {
111+
// Update attributes state in PageManager for event metadata
112+
this.collectAttributes(
113+
this.page as Page,
114+
typeof payload === 'object' ? payload : undefined
115+
);
116+
return;
110117
} else {
111118
// The view has not changed.
112119
return;

src/sessions/__tests__/PageManager.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,54 @@ describe('PageManager tests', () => {
233233
// Assert
234234
expect(pageManager.getPage()?.interaction).toEqual(1);
235235

236+
// Assert
237+
expect(pageManager.getAttributes()).toMatchObject({
238+
pageId: '/console/home',
239+
interaction: 1
240+
});
241+
242+
window.removeEventListener(
243+
'popstate',
244+
(pageManager as any).popstateListener
245+
);
246+
});
247+
248+
test('when session resumes and page is manually recorded with custom page attributes then custom page attributes are restored', async () => {
249+
// Init
250+
const pageManager: PageManager = new PageManager(
251+
{
252+
...DEFAULT_CONFIG,
253+
allowCookies: true
254+
},
255+
record
256+
);
257+
258+
pageManager.resumeSession({
259+
pageId: '/console/home',
260+
interaction: 1,
261+
start: Date.now()
262+
});
263+
264+
pageManager.recordPageView({
265+
pageId: '/console/home',
266+
pageTags: ['pageGroup1'],
267+
pageAttributes: {
268+
customPageAttributeString: 'customPageAttributeValue',
269+
customPageAttributeNumber: 1,
270+
customPageAttributeBoolean: true
271+
}
272+
});
273+
274+
// Assert
275+
expect(record.mock.calls).toHaveLength(0); // No event emitted, but attributes restored in PageManager state
276+
expect(pageManager.getAttributes()).toMatchObject({
277+
pageId: '/console/home',
278+
pageTags: ['pageGroup1'],
279+
customPageAttributeString: 'customPageAttributeValue',
280+
customPageAttributeNumber: 1,
281+
customPageAttributeBoolean: true
282+
});
283+
236284
window.removeEventListener(
237285
'popstate',
238286
(pageManager as any).popstateListener

0 commit comments

Comments
 (0)