@@ -435,6 +435,7 @@ export class ReplayContainer implements ReplayContainerInterface {
435435 try {
436436 WINDOW . document . addEventListener ( 'visibilitychange' , this . _handleVisibilityChange ) ;
437437 WINDOW . addEventListener ( 'blur' , this . _handleWindowBlur ) ;
438+ WINDOW . addEventListener ( 'beforeunload' , this . _handleWindowUnload ) ;
438439 WINDOW . addEventListener ( 'focus' , this . _handleWindowFocus ) ;
439440
440441 // We need to filter out dropped events captured by `addGlobalEventProcessor(this.handleGlobalEvent)` below
@@ -467,6 +468,7 @@ export class ReplayContainer implements ReplayContainerInterface {
467468
468469 WINDOW . removeEventListener ( 'blur' , this . _handleWindowBlur ) ;
469470 WINDOW . removeEventListener ( 'focus' , this . _handleWindowFocus ) ;
471+ WINDOW . removeEventListener ( 'beforeunload' , this . _handleWindowUnload ) ;
470472
471473 restoreRecordDroppedEvent ( ) ;
472474
@@ -571,6 +573,19 @@ export class ReplayContainer implements ReplayContainerInterface {
571573 this . _doChangeToBackgroundTasks ( breadcrumb ) ;
572574 } ;
573575
576+ /**
577+ * Handle when page is unloaded (=left).
578+ */
579+ private _handleWindowUnload : ( ) => void = ( ) => {
580+ const breadcrumb = createBreadcrumb ( {
581+ category : 'ui.unload' ,
582+ } ) ;
583+
584+ // Do not count blur as a user action -- it's part of the process of them
585+ // leaving the page
586+ this . _doChangeToBackgroundTasks ( breadcrumb ) ;
587+ } ;
588+
574589 /**
575590 * Handle when page is focused
576591 */
0 commit comments