@@ -99,20 +99,31 @@ export const createAnalyticsModule = ({
9999 }
100100 } ;
101101
102- if ( typeof window !== "undefined" && enabled ) {
103- window . addEventListener ( "visibilitychange" , ( ) => {
104- if ( document . visibilityState === "hidden" ) {
105- analyticsSharedState . isProcessing = false ;
106- // flush entire queue on visibility change and hope for the best //
107- const eventsData = analyticsSharedState . requestsQueue . splice ( 0 ) ;
108- flush ( eventsData ) ;
109- } else if ( document . visibilityState === "visible" ) {
110- startAnalyticsProcessor ( flush , {
111- throttleTime,
112- batchSize,
113- } ) ;
114- }
102+ const onDocHidden = ( ) => {
103+ analyticsSharedState . isProcessing = false ;
104+ // flush entire queue on visibility change and hope for the best //
105+ const eventsData = analyticsSharedState . requestsQueue . splice ( 0 ) ;
106+ flush ( eventsData ) ;
107+ } ;
108+
109+ const onDocVisible = ( ) => {
110+ startAnalyticsProcessor ( flush , {
111+ throttleTime,
112+ batchSize,
115113 } ) ;
114+ } ;
115+
116+ const onVisibilityChange = ( ) => {
117+ if ( typeof window === "undefined" ) return ;
118+ if ( document . visibilityState === "hidden" ) {
119+ onDocHidden ( ) ;
120+ } else if ( document . visibilityState === "visible" ) {
121+ onDocVisible ( ) ;
122+ }
123+ } ;
124+
125+ if ( typeof window !== "undefined" && enabled ) {
126+ window . addEventListener ( "visibilitychange" , onVisibilityChange ) ;
116127 }
117128
118129 // start analytics processor only if it's the first instance and analytics is enabled //
@@ -174,6 +185,7 @@ function transformEventDataToApiRequestData(sessionContext: SessionContext) {
174185export function getAnalyticsModuleOptionsFromUrlParams ( ) :
175186 | AnalyticsModuleOptions
176187 | undefined {
188+ if ( typeof window === "undefined" ) return undefined ;
177189 const urlParams = new URLSearchParams ( window . location . search ) ;
178190 const jsonString = urlParams . get ( "analytics" ) ;
179191 if ( ! jsonString ) return undefined ;
0 commit comments