Skip to content

useTracker()

Returns the BrowserTracker instance held by the nearest <TrackbridgeProvider> ancestor. The standard way to call any tracker method from a client component.

import { useTracker } from '@trackbridge/sdk/next';
function useTracker(): BrowserTracker;

Throws synchronously if called outside a <TrackbridgeProvider>:

Error: useTracker must be called inside <TrackbridgeProvider>

This is a programming error — no fallback, no warning.

'use client';
import { useTracker } from '@trackbridge/sdk/next';
export function CheckoutButton({ cart }: { cart: Cart }) {
const tracker = useTracker();
return (
<button
onClick={async () => {
await tracker.trackBeginCheckout({
transactionId: cart.id,
value: cart.total,
currency: cart.currency,
items: cart.items,
});
router.push('/checkout');
}}
>
Checkout
</button>
);
}

For a long-lived module reference (e.g., calling the tracker from outside React), prefer the explicit createBrowserTracker pattern in a lib/tracker.client.ts module rather than reaching for the hook from non-component code.