React and Next.js In development

The nabd-react package, with a dedicated entry for Next.js.

Not available yet. These examples show the planned API; package and function names may change before the first release.

Install

bash
npm install nabd-browser nabd-react

React

main.tsx
import { NabdErrorBoundary, NabdProvider } from 'nabd-react';
import { NabdRouterTracker } from 'nabd-react/router';

const options = {
  orgToken: import.meta.env.VITE_NABD_KEY,
  appName: 'My App',
  consentRequired: true,
};

root.render(
  <NabdProvider options={options}>
    <BrowserRouter>
      <NabdRouterTracker routes={routes} />   {/* screen names from React Router */}
      <NabdErrorBoundary fallback={<ErrorPage />}>
        <App />
      </NabdErrorBoundary>
    </BrowserRouter>
  </NabdProvider>,
);

routes is your own route array (or router.routes with createBrowserRouter), so the screen name is the pattern /customers/:name, not the actual URL. Without it the name comes from the URL, and only numbers and identifiers are replaced.

NabdErrorBoundary records the error type and fingerprint and shows fallback. React Router 6.4 to 8.

Next.js (App Router)

app/layout.tsx
import { NabdNext } from 'nabd-react/next';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="ar" dir="rtl">
      <body>
        <NabdNext
          orgToken={process.env.NEXT_PUBLIC_NABD_KEY!}
          appName="My Site"
          consentRequired
        />
        {children}
      </body>
    </html>
  );
}

The screen name is the path with dynamic segment values replaced by their names (/customers/:name). Send errors caught by error.tsx yourself with nabd.captureError(error).

Consent and events

tsx
const nabd = useNabd();

<button onClick={() => nabd.setConsent(true)}>Accept</button>

nabd.trackEvent('taskCompleted', { screen: 'invoice_create' });