mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 01:25:16 +00:00
Fix error from visitor id not set (#2678)
This commit is contained in:
@@ -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}>
|
||||
|
||||
Reference in New Issue
Block a user