11'use client'
22
3- import { useEffect } from 'react'
3+ import { useEffect , useRef } from 'react'
44import { usePathname , useSearchParams } from 'next/navigation'
55
66declare global {
@@ -11,9 +11,7 @@ declare global {
1111
1212// next/script dedupes by id and never reloads on remount, so this must be
1313// module-scope (not a ref) to survive LandingLayout unmounting/remounting.
14- // Keyed by URL (not a boolean) so Strict Mode effect replays and same-URL
15- // remounts never re-fire a PageView already counted for that URL.
16- let lastTrackedUrl : string | null = null
14+ let hasTrackedInitialPageView = false
1715
1816/**
1917 * The X pixel base code only auto-tracks the first page load; LandingLayout
@@ -26,16 +24,21 @@ export function XPageViewTracker() {
2624 const searchParams = useSearchParams ( )
2725 const query = searchParams . toString ( )
2826
27+ // Instance-scoped (not module-scoped) so a Strict Mode replay of this
28+ // mount's effect is skipped, while a fresh mount — returning to the landing
29+ // layout from the app — starts empty and tracks the view again.
30+ const lastTrackedUrlRef = useRef < string | null > ( null )
31+
2932 useEffect ( ( ) => {
3033 const url = query ? `${ pathname } ?${ query } ` : pathname
34+ if ( lastTrackedUrlRef . current === url ) return
35+ lastTrackedUrlRef . current = url
3136
32- if ( lastTrackedUrl === null ) {
33- lastTrackedUrl = url
37+ if ( ! hasTrackedInitialPageView ) {
38+ hasTrackedInitialPageView = true
3439 return
3540 }
36- if ( url === lastTrackedUrl ) return
3741
38- lastTrackedUrl = url
3942 window . twq ?.( 'config' , 'q5xbl' )
4043 } , [ pathname , query ] )
4144
0 commit comments