diff --git a/packages/gitbook/src/components/Insights/InsightsProvider.tsx b/packages/gitbook/src/components/Insights/InsightsProvider.tsx index 2e65ab8c5..66e956a17 100644 --- a/packages/gitbook/src/components/Insights/InsightsProvider.tsx +++ b/packages/gitbook/src/components/Insights/InsightsProvider.tsx @@ -192,14 +192,31 @@ export function InsightsProvider(props: InsightsProviderProps) { React.useEffect(() => { getVisitorId().then((visitorId) => { visitorIdRef.current = visitorId; - // When the page is unloaded, flush all events, but only if the visitor ID is set - window.addEventListener('beforeunload', flushEventsSync); }); + }, []); + + // When the page is unloaded, flush all events + React.useEffect(() => { + window.addEventListener('beforeunload', flushEventsSync); return () => { window.removeEventListener('beforeunload', flushEventsSync); }; }, [flushEventsSync]); + /** + * Get the visitor ID and store it in a ref. + */ + // React.useEffect(() => { + // getVisitorId().then((visitorId) => { + // visitorIdRef.current = visitorId; + // // When the page is unloaded, flush all events, but only if the visitor ID is set + // window.addEventListener('beforeunload', flushEventsSync); + // }); + // return () => { + // window.removeEventListener('beforeunload', flushEventsSync); + // }; + // }, [flushEventsSync]); + return (