A client-side analytics hook that tracks user navigation paths, time spent on pages, and interaction events with support for session management and traffic source attribution.
import { FlowTrace } from '@/hooks/flow-trace'export default function Layout() {return (<><Suspense><FlowTrace /></Suspense>{/* Your content */}</>)}
.ft-tracking classsendBeacon or fetch with keepalive for reliable data transmissionft_path: Navigation path history (JSON array)ft_first: First seen timestampft_time_spent_ms: Total time spent across all sessionsft_session_source: Source attribution for current sessionft_session_start: Current session start timestampft_last_interaction: Last interaction timestamp.ft-tracking elements with 5-second debounce/api/flow-trace endpoint{firstSeen: string, // ISO timestamp of first visitsessionStart: string, // ISO timestamp of session starttimeSpentTotalMs: number, // Total time spent (all sessions)page: {url: string, // Current page URLtitle: string // Page title},path: [{path: string, // Full path with query & hashts: string, // ISO timestampspentMs: number, // Time spent on this pagesource?: string // Traffic source (if new arrival)}]}
Add the ft-tracking class to elements you want to track:
<ahref="/contact"className="ft-tracking">Contact Us</a><buttonclassName="ft-tracking"id="newsletter-signup">Subscribe</button>
Click payloads include:
element.href: Link destination (if anchor)element.innerText: Element text contentelement.id: Element IDcontext: Complete analytics payload/api/flow-tracezone constant)Traffic sources are determined in priority order:
utm_source)External sources are validated to exclude:
NEXT_PUBLIC_PRODUCTION_URL)The component is used in src/app/layout.tsx to enable flow tracking throughout the application.
next/navigation for route change detectionluxon for timezone-aware datetime handling