Skip to content

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.

tracker.trackPageView(input?: BrowserPageViewInput): Promise<void>;
type BrowserPageViewInput = {
/** Default: `window.location.pathname + window.location.search`. */
path?: string;
/** Default: `document.title`. */
title?: string;
};
FieldRequiredNotes
pathnoThe page_path param. Defaults to the current URL path + search.
titlenoThe page_title param. Defaults to document.title.

page_location is auto-filled from window.location.href and is not configurable.

Promise<void>. SSR-safe — defaults resolve to '' when window / document are absent.

  1. If ga4MeasurementId was not configured at init, no-op (debug-warns under debug: true).
  2. Computes the effective page_path from input.path or window.location.
  3. If the previous trackPageView call resolved to the same page_path, no-op (debug-warns under debug: true). This protects against React 18 strict-mode double-mount and other patterns that fire navigation effects twice.
  4. Fires gtag('event', 'page_view', { page_path, page_title, page_location }).
'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.