Skip to content

Commit 4698144

Browse files
committed
fix(landing): scope X pixel URL dedupe to the tracker instance so same-URL returns to landing still track
1 parent 39f9b10 commit 4698144

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

apps/sim/app/(landing)/x-page-view-tracker.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect } from 'react'
3+
import { useEffect, useRef } from 'react'
44
import { usePathname, useSearchParams } from 'next/navigation'
55

66
declare 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

Comments
 (0)