Fix error from visitor id not set (#2678)

This commit is contained in:
Greg Bergé
2025-01-07 15:18:05 +01:00
committed by GitHub
parent cbe61397a3
commit 12f25d8bfc
@@ -86,15 +86,6 @@ export function InsightsProvider(props: InsightsProviderProps) {
| undefined;
}>({});
/**
* Get the visitor ID and store it in a ref.
*/
React.useEffect(() => {
getVisitorId().then((visitorId) => {
visitorIdRef.current = visitorId;
});
}, []);
/**
* Synchronously flush all the pending events.
*/
@@ -194,13 +185,19 @@ export function InsightsProvider(props: InsightsProviderProps) {
},
);
// When the page is unloaded, flush all events
/**
* Get the visitor ID and store it in a ref.
*/
React.useEffect(() => {
window.addEventListener('beforeunload', flushEventsSync);
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 (
<InsightsContext.Provider value={trackEvent}>