import { useLocation } from '@tanstack/react-router' import { useEffect, useRef } from 'react' import { IS_STAGING_OR_PREVIEW } from '@/lib/constants' import { sendFrontendMetric } from '@/lib/frontend-metrics' /** * Sends one pageview metric on hydration and on every pathname change. * Search-param and hash changes are not pageviews. */ export function usePageviewMetric() { const pathname = useLocation({ select: (location) => location.pathname }) const lastSentPathname = useRef(null) useEffect(() => { if (!IS_STAGING_OR_PREVIEW) return // Guards against the StrictMode effect double-invoke if (lastSentPathname.current === pathname) return lastSentPathname.current = pathname sendFrontendMetric('pageview') }, [pathname]) }