React and Next.js
Start tracking once, in a client component near the top of your app. Moves between pages are counted by themselves, and track() works from any component.
A tracking component#
app/optiq.tsx
"use client";
import { useEffect } from "react";
import { init } from "@optiqstudio/track";
export default function Optiq() {
useEffect(() => {
init({ key: process.env.NEXT_PUBLIC_OPTIQ_KEY! });
}, []);
return null;
}Put the publishable key in NEXT_PUBLIC_OPTIQ_KEY (or your bundler’s public variable). It is public by design, so it is safe in the browser.
Add it to your layout#
// app/layout.tsx
import Optiq from "./optiq";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<Optiq />
{children}
</body>
</html>
);
}Track from a component#
TypeScript
"use client";
import { track } from "@optiqstudio/track";
export function SignUpForm() {
async function send(form: FormData) {
const ok = await createAccount(form);
if (ok) track("sign_up", { email: String(form.get("email")), plan: "gold" });
}
return <form action={send}>…</form>;
}Note: Count a lead or a sign-up when it succeeds, not when the button is pressed, so failed attempts are not counted.
Content security policy#
If your app sets one, allow the collector for connections:
HTTP header
Content-Security-Policy: connect-src 'self' https://optiq-collect-951694748196.europe-west2.run.appLast updated 2026-11-04 UTC. The SDK is open source under the MIT licence. Tell us what is missing.