From 683646a490d99d154db861f367e6bdb0b862e50e Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Tue, 4 Feb 2025 10:06:44 +0000 Subject: [PATCH] Merged in fix/pageloadtime-fix (pull request #1248) tracking: wait on document.readystate * tracking: wait on document.readystate --- components/TrackingSDK/RouterTransition.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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() }