Settings · 2 min read
React and Next.js
Add Lemu to a React or Next.js app with @lemuai/react: a provider, the useLemu hook and page tracking for React Router and the Next.js routers
@lemuai/react adds Lemu to React and Next.js apps: a provider, a hook to send events from any component, and page tracking for React Router and both Next.js routers. It's built on @lemuai/analytics and is safe with server-side rendering: nothing touches window during render, and calls made on the server do nothing.
You need the write key of a Website source. See Get a write key. The source's React / Next.js tab under Set up shows the code below with your key filled in.
Install#
npm install @lemuai/reactSet up#
Wrap your app once with LemuProvider:
import { LemuProvider } from "@lemuai/react";
export function App() {
return (
<LemuProvider writeKey="YOUR_WRITE_KEY">
<Routes />
</LemuProvider>
);
}The provider starts tracking after it mounts in the browser. It reads options once, when it mounts. For the options, see Website and JavaScript.
Page views#
usePageTracking(pathname) records a page view whenever the path changes. Use it once, inside the provider.
Put the provider in a client component:
// app/lemu.tsx
"use client";
import { usePathname } from "next/navigation";
import { LemuProvider, usePageTracking } from "@lemuai/react";
function PageTracker() {
usePageTracking(usePathname());
return null;
}
export function Lemu({ children }: { children: React.ReactNode }) {
return (
<LemuProvider writeKey={process.env.NEXT_PUBLIC_LEMU_WRITE_KEY!}>
<PageTracker />
{children}
</LemuProvider>
);
}Then use it in your root layout:
// app/layout.tsx
import { Lemu } from "./lemu";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<Lemu>{children}</Lemu>
</body>
</html>
);
}Set NEXT_PUBLIC_LEMU_WRITE_KEY to your write key. It's public, so the NEXT_PUBLIC_ prefix is fine.
Send events#
Get the client with useLemu() in any component:
import { LemuEvent, useLemu } from "@lemuai/react";
function AddToCartButton({ product }) {
const lemu = useLemu();
return (
<button
onClick={() =>
lemu.track(LemuEvent.AddToCart, {
value: product.price,
currency: "INR",
content_ids: [product.sku],
})
}
>
Add to cart
</button>
);
}LemuEvent has a constant for each of Meta's standard events. Any other name works as a custom event. See the event reference.
useLemu() also works outside a provider. It returns the shared client, and calls made before the provider starts it are kept and sent once it does.
Purchases#
const lemu = useLemu();
lemu.purchase({
value: 1499.5,
currency: "INR",
order_id: order.id,
contents: [{ id: "SKU1", quantity: 1, item_price: 1499.5 }],
});purchase sends a Purchase event at once. Always pass your real order id: Lemu counts one conversion per order_id, however many sources report the order (this page, a webhook, a reload of the thank-you page). See Conversion events.
Identify users#
const lemu = useLemu();
// after sign-up or login
lemu.identify(user.id, { email: user.email, phone: user.phone, first_name: user.firstName });
// on logout
lemu.reset();The user id is kept and sent with every later event until reset. Send phone numbers in international format, for example +919876543210. Contact details belong in identify only.
Consent and opt-out#
To wait for consent, pass requireConsent to the provider and call setConsent(true) from your consent banner:
<LemuProvider writeKey="YOUR_WRITE_KEY" options={{ requireConsent: true }}>
<App />
</LemuProvider>
// in your consent banner
const lemu = useLemu();
lemu.setConsent(true);Nothing is sent or stored until then. lemu.optOut() and lemu.optIn() turn tracking off and on, remembered in the browser. See Consent and opt-out for the details.
Debug mode#
<LemuProvider writeKey="YOUR_WRITE_KEY" options={{ debug: true }}>Every call, request and response is logged to the browser console with a [lemu] prefix.
API#
| Export | What it is |
|---|---|
LemuProvider | Props: writeKey, options (see options), and an optional client from createLemu(). Starts tracking after mount |
useLemu() | The client: track, page, purchase, identify, reset, flush, setConsent, optOut, optIn, anonymousId |
usePageTracking(pathname, properties?) | Records a page view on every path change |
LemuEvent | Standard event names. See the event reference |
createLemu() | A separate client, for sending to a second source |
Was this page helpful?
Docs