Fixed new tracking requirements
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname } from "next/navigation"
|
||||
import { useEffect, useMemo } from "react"
|
||||
import { useEffect, useMemo, useRef } from "react"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { RoomConfiguration } from "@/server/routers/hotels/output"
|
||||
@@ -48,6 +48,11 @@ export default function EnterDetailsTracking(props: Props) {
|
||||
const { getPageLoadTime, hasRun } = useTrackingStore()
|
||||
const pathName = usePathname()
|
||||
|
||||
// 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)
|
||||
|
||||
const getSpecialRoomType = (packages: Packages | null) => {
|
||||
if (!packages) {
|
||||
return ""
|
||||
@@ -123,25 +128,31 @@ export default function EnterDetailsTracking(props: Props) {
|
||||
])
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasRun) {
|
||||
if (!hasRunInitial.current) {
|
||||
hasRunInitial.current = true
|
||||
previousPathname.current = pathName // Set initial path to compare later
|
||||
return
|
||||
}
|
||||
console.log("TRACKING: Tracking RouterTransition pageViewEnd", pageObject)
|
||||
console.log(
|
||||
"TRACKING: Tracking RouterTransition userData",
|
||||
userTrackingData
|
||||
)
|
||||
console.log(
|
||||
"TRACKING: Tracking RouterTransition hotelInfo",
|
||||
hotelDetailsData
|
||||
)
|
||||
window.adobeDataLayer.push({
|
||||
event: "pageViewEnd",
|
||||
pageInfo: pageObject,
|
||||
userInfo: userTrackingData,
|
||||
hotelInfo: hotelDetailsData,
|
||||
})
|
||||
}, [userTrackingData, hasRun, pageObject, hotelDetailsData])
|
||||
|
||||
if (previousPathname.current !== pathName) {
|
||||
console.log("TRACKING: Tracking RouterTransition pageViewEnd", pageObject)
|
||||
console.log(
|
||||
"TRACKING: Tracking RouterTransition userData",
|
||||
userTrackingData
|
||||
)
|
||||
console.log(
|
||||
"TRACKING: Tracking RouterTransition hotelInfo",
|
||||
hotelDetailsData
|
||||
)
|
||||
window.adobeDataLayer.push({
|
||||
event: "pageView",
|
||||
pageInfo: pageObject,
|
||||
userInfo: userTrackingData,
|
||||
hotelInfo: hotelDetailsData,
|
||||
})
|
||||
}
|
||||
previousPathname.current = pathName // Update for next render
|
||||
}, [userTrackingData, pageObject, hotelDetailsData, pathName])
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user