tracker.trackPageView()
Fires a GA4 page_view event for SPA / App Router navigations where gtag.js’s automatic page-view firing isn’t appropriate (because the page never reloaded).
@trackbridge/sdk/next ships a <TrackbridgePageViews /> component that calls this for you on every Next.js route change. For other frameworks, wire it manually wherever you handle navigation.
Signature
Section titled “Signature”tracker.trackPageView(input?: BrowserPageViewInput): Promise<void>;BrowserPageViewInput
Section titled “BrowserPageViewInput”type BrowserPageViewInput = { /** Default: `window.location.pathname + window.location.search`. */ path?: string; /** Default: `document.title`. */ title?: string;};| Field | Required | Notes |
|---|---|---|
path | no | The page_path param. Defaults to the current URL path + search. |
title | no | The page_title param. Defaults to document.title. |
page_location is auto-filled from window.location.href and is not configurable.
Returns
Section titled “Returns”Promise<void>. SSR-safe — defaults resolve to '' when window / document are absent.
Behavior
Section titled “Behavior”- If
ga4MeasurementIdwas not configured at init, no-op (debug-warns underdebug: true). - Computes the effective
page_pathfrominput.pathorwindow.location. - If the previous
trackPageViewcall resolved to the samepage_path, no-op (debug-warns underdebug: true). This protects against React 18 strict-mode double-mount and other patterns that fire navigation effects twice. - Fires
gtag('event', 'page_view', { page_path, page_title, page_location }).
Example — App Router (manual)
Section titled “Example — App Router (manual)”'use client';import { useEffect } from 'react';import { usePathname } from 'next/navigation';import { tracker } from '@/lib/tracker.client';
export function PageViews() { const pathname = usePathname(); useEffect(() => { tracker.trackPageView({ path: pathname }); }, [pathname]); return null;}The Next.js adapter’s <TrackbridgePageViews /> is exactly this snippet.
See also
Section titled “See also”<TrackbridgePageViews />— Next.js adapter.tracker.trackEvent()— for non-page-view custom events.