Install Lucida on React (Vite)
Add the Lucida browser SDK to a React app built with Vite: package, provider with only the key, CSP, optional proxy, consent and a first-event check.
For React 18 or later with Vite (or any bundler that does not need server rendering). The package is SSR-safe: the browser SDK is never touched on the server.
1. Install
npm install @lucida-analytics/react @lucida-analytics/browserPut the public key in .env.local:
VITE_LUCIDA_KEY=lc_pk_YOUR_PUBLIC_KEYOr run lucida init --framework react --host https://capture.lucidace.com, which writes the env var and an init module and prints the install command.
2. Init with the key
// src/main.tsx
import { LucidaProvider } from "@lucida-analytics/react";
root.render(
<LucidaProvider apiKey={import.meta.env.VITE_LUCIDA_KEY}>
<App />
</LucidaProvider>,
);That is all that is needed on the cloud: the default host is https://capture.lucidace.com. Add options={{ apiHost: "..." }} only when you self-host or send through a reverse proxy.
Use the hooks anywhere below the provider:
import { useLucida, useCapture } from "@lucida-analytics/react";
const lucida = useLucida(); // the client, or null on the server
const capture = useCapture();
capture("buy_clicked");
// after sign-in: lucida?.identify(user.id, { email: user.email }); on sign-out: lucida?.reset();Page views follow route changes by themselves (capturePageview: "history" is the default for history routers; use "hash" for hash routers).
<LucidaTag publicKey=... modules={["replay"]} /> is the provider-free loader: modules takes names ("replay", "heatmaps", "forms", "guides") as well as functions, and useFlag(key) reads flags under it.
3. Content Security Policy
script-src 'self';
connect-src 'self' https://capture.lucidace.com;Bundle the SDK with your app so 'self' covers it; the SDK never uses eval. If you proxy through your own domain, connect-src 'self' is enough. Guides, web experiments and the toolbar need more: see Content Security Policy.
4. Reverse proxy (optional)
Ad blockers often block third-party analytics hosts. To send through your own path, point apiHost at it and add one rule in front of https://capture.lucidace.com:
<LucidaProvider apiKey={import.meta.env.VITE_LUCIDA_KEY} options={{ apiHost: "https://example.com/lc", uiHost: "https://lucidace.com" }}>For Vite's dev server a server.proxy entry works; for production use nginx, Cloudflare or Vercel as shown in Serve Lucida from your own domain. uiHost is needed because apiHost then no longer points at Lucida.
5. Consent
The SDK default is opt_out: it stores and sends before the visitor decides. For prior consent, start in opt_in:
<LucidaProvider apiKey={import.meta.env.VITE_LUCIDA_KEY} options={{ consent: { mode: "opt_in" } }}>Then call setConsent from your banner. useConsent() gives status, optIn, optOut and setConsent and re-renders on every change:
const { setConsent } = useConsent();
setConsent({ analytics: true, replay: true, marketing: false });For Cookie Munch pass cookieMunchConsent from @lucida-analytics/browser/consent in modules (@lucida-analytics/react 0.1.3 with @lucida-analytics/browser 0.1.8): Cookie Munch consent. Defaults and regional rules: Consent by location.
6. Check the first event
Terminal:
lucida init --framework react --host https://capture.lucidace.com --project $LUCIDA_PROJECT_ID --verifywaits for a new event (needs an account key). Load a page of your app.App: the Install wizard (onboarding page) waits for the first event and tells you when it arrives.
If nothing arrives, look for Refused to connect in the console (CSP).
More
Quickstart (replay, groups, URL privacy), SDK pinning, all frameworks.