Next.js integration: provider, page tracking, and error boundary. Uses @telemetry-tracker/core under the hood; all events include anonymous device id and SDK version (see telemetry-core).
In a monorepo workspace:
pnpm add @telemetry-tracker/nextPeer dependencies: next (≥14), react (≥18).
- Wrap your app (or layout) with
TelemetryProviderand pass your config. - Call
useTrackPage(pathname)where you have access to the current pathname (e.g. in a layout or page) so route changes are sent as screen events. - Optionally wrap error-prone trees with
TelemetryErrorBoundaryso React errors are reported.
app/layout.tsx (or a client layout):
"use client";
import { TelemetryProvider, useTrackPage } from "@telemetry-tracker/next";
import { usePathname } from "next/navigation";
const config = {
ingestUrl: process.env.NEXT_PUBLIC_INGEST_URL || "http://localhost:3001",
app: "my-next-app",
environment: process.env.NODE_ENV,
// webVitals: true by default — emits $web_vital events for LCP, INP, CLS, TTFB
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<TelemetryProvider config={config}>
<TrackPage />
{children}
</TelemetryProvider>
);
}
function TrackPage() {
const pathname = usePathname();
useTrackPage(pathname ?? "/");
return null;
}Wrap a section of the tree to report React errors and optionally show a fallback:
import { TelemetryErrorBoundary } from "@telemetry-tracker/next";
<TelemetryErrorBoundary fallback={<div>Something went wrong.</div>}>
<MyComponent />
</TelemetryErrorBoundary>If you don’t pass fallback, the boundary renders nothing when it catches an error.
| Export | Description |
|---|---|
init(config) |
Call @telemetry-tracker/core’s init (usually done via TelemetryProvider). |
identify(userId) |
Set current user id. |
trackEvent(name, properties?) |
Send a named event (re-exported from core). |
trackError(error, context?) |
Report an error with optional context. |
screen(name) |
Record a screen/view (re-exported from core). |
TelemetryProvider({ config, children }) |
Client component that calls init(config) on mount. |
useTrackPage(pathname) |
Sends screen(pathname) when pathname changes (client-only). |
TelemetryErrorBoundary |
Class component that reports errors in componentDidCatch and optionally renders a fallback. |
Config shape is the same as telemetry-core (TelemetryNextConfig = TelemetryConfig).
If you prefer not to use the provider:
"use client";
import { init, useTrackPage } from "@telemetry-tracker/next";
import { usePathname } from "next/navigation";
init({ ingestUrl: "http://localhost:3001", app: "my-app", apiKey: process.env.NEXT_PUBLIC_TELEMETRY_API_KEY });
export function Navigation() {
const pathname = usePathname();
useTrackPage(pathname ?? "/");
return <nav>...</nav>;
}Ensure init runs only on the client (e.g. inside a "use client" component or useEffect).
Import trackEvent, screen, and identify from @telemetry-tracker/next:
import { trackEvent, identify } from "@telemetry-tracker/next";
trackEvent("signup_clicked", { source: "hero" });
identify(user.id); // after login
identify(null); // on logout