Use React and Next.js
The React entry gives the client to your components. The client sends page.viewed on each route change.
Procedure
Make the client in one module:
tsimport { createClient } from '@quickpipeaiab/lead-scoring'; export const lg = createClient({ key: 'lg-pub-live-…' });Put
Provideraround your application. In the Next.js App Router, put'use client'at the top of the file:tsx'use client'; import { Provider } from '@quickpipeaiab/lead-scoring/react'; import { lg } from './lead-scoring'; export function App({ children }: { children: React.ReactNode }) { return <Provider client={lg}>{children}</Provider>; }Use
useClientin a component. In the Next.js App Router, each file that callsuseClientneeds'use client'at the top:tsx'use client'; import { useClient } from '@quickpipeaiab/lead-scoring/react'; export function TrialButton() { const { track } = useClient(); return <button onClick={() => track('trial.started')}>Start a trial</button>; }
Result
Provider gives the client to the components. The client sends page.viewed on each route change. useClient fails when no Provider is above the component.
The React entry has react as an optional peer dependency. The core package and the script tag do not need React.
Next.js: join server events to the visitor
A Next.js server can read the visitor cookie lg_aid. Server events then belong to the same visitor as the browser events:
import { cookies } from 'next/headers';
import { serverClient } from './lead-scoring.server';
const anonymousId = (await cookies()).get('lg_aid')?.value;
if (anonymousId) serverClient.as({ anonymousId }).track('quote.created');Before consent, the visitor has no cookie. Then the server sends no event. The server client uses a secret key. Refer to Send server events.