Merged in feat/SW-1909-update-adobe-on-language-change (pull request #1526)

feat(SW-1909): Fix Adobe Data Layer not updating during language changes

* feat(SW-1909): Update Tracking on language changes


Approved-by: Linus Flood
This commit is contained in:
Chuma Mcphoy (We Ahead)
2025-03-13 08:25:50 +00:00
parent bb599d4679
commit 550474dffe
2 changed files with 54 additions and 16 deletions

View File

@@ -1,41 +1,42 @@
"use client"
import { usePathname, useSearchParams } from "next/navigation"
import { startTransition, useEffect, useRef } from "react"
import { startTransition, useEffect } from "react"
import useRouterTransitionStore from "@/stores/router-transition"
import useTrackingStore from "@/stores/tracking"
import useLang from "@/hooks/useLang"
import { trackPageViewStart } from "@/utils/tracking"
export default function RouterTracking() {
const pathName = usePathname()
const searchParams = useSearchParams()
const { setInitialPageLoadTime } = useTrackingStore()
const currentLang = useLang()
const { setInitialPageLoadTime, updateRouteInfo, hasPathOrLangChanged } =
useTrackingStore()
const { startRouterTransition } = useRouterTransitionStore()
// We need this check to differentiate hard vs soft navigations
// This is not because of StrictMode
const hasRunInitial = useRef<boolean>(false)
const previousPathname = useRef<string | null>(null)
useEffect(() => {
if (!hasRunInitial.current) {
hasRunInitial.current = true
previousPathname.current = pathName // Set initial path to compare later
return
}
if (previousPathname.current !== pathName) {
updateRouteInfo(pathName, currentLang)
if (hasPathOrLangChanged()) {
setInitialPageLoadTime(Date.now())
trackPageViewStart()
startTransition(() => {
startRouterTransition()
})
}
previousPathname.current = pathName // Update for next render
}, [pathName, searchParams, setInitialPageLoadTime, startRouterTransition])
}, [
pathName,
searchParams,
currentLang,
updateRouteInfo,
hasPathOrLangChanged,
setInitialPageLoadTime,
startRouterTransition,
])
return null
}