diff --git a/components/TrackingSDK/RouterTransition.tsx b/components/TrackingSDK/RouterTransition.tsx index 2a4f038b4..6168d7764 100644 --- a/components/TrackingSDK/RouterTransition.tsx +++ b/components/TrackingSDK/RouterTransition.tsx @@ -49,9 +49,8 @@ export default function RouterTransition({ useEffect(() => { if (!hasRun && !hasRunInitial.current) { - if (performance) { + const handleLoad = () => { const entry = performance.getEntriesByType("navigation")[0] - if (entry) { const trackingData = { ...pageData, @@ -69,7 +68,14 @@ export default function RouterTransition({ }) } } - + // If the page is already loaded, execute immediately + if (document.readyState === "complete") { + handleLoad() + } else { + // Otherwise wait for the load event + window.addEventListener("load", handleLoad) + return () => window.removeEventListener("load", handleLoad) + } hasRunInitial.current = true setHasRun() }