Merged in fix/tracking-multiple-times (pull request #1186)

fix: avoid tracking multiple times on the same page

* fix: avoid tracking multiple times on the same page


Approved-by: Erik Tiekstra
This commit is contained in:
Linus Flood
2025-01-17 10:08:28 +00:00
parent 69b69af03c
commit d229b5c463

View File

@@ -41,6 +41,8 @@ export default function RouterTransition({
const pathName = usePathname() const pathName = usePathname()
const { isTransitioning, stopRouterTransition } = useRouterTransitionStore() const { isTransitioning, stopRouterTransition } = useRouterTransitionStore()
const previousPathname = useRef<string | null>(null)
// We need this check to differentiate hard vs soft navigations // We need this check to differentiate hard vs soft navigations
// This is not because of StrictMode // This is not because of StrictMode
const hasRunInitial = useRef<boolean>(false) const hasRunInitial = useRef<boolean>(false)
@@ -112,6 +114,7 @@ export default function RouterTransition({
pageLoadTime: getPageLoadTime(), pageLoadTime: getPageLoadTime(),
} }
const pageObject = createSDKPageObject(trackingData) const pageObject = createSDKPageObject(trackingData)
if (previousPathname.current !== pathName) {
pushToDataLayer({ pushToDataLayer({
event: "pageView", event: "pageView",
pageInfo: pageObject, pageInfo: pageObject,
@@ -120,6 +123,8 @@ export default function RouterTransition({
paymentInfo, paymentInfo,
}) })
} }
previousPathname.current = pathName // Update for next render
}
} }
}, [ }, [
isTransitioning, isTransitioning,