Using the browser SDK in a browser extension (Manifest V3)
Manifest V3 forbids remotely hosted code: an extension may only run scripts packaged with it. Extension pages also have no useful cookies. The SDK has an...
Manifest V3 forbids remotely hosted code: an extension may only run scripts packaged with it. Extension pages also have no useful cookies. The SDK has an extension mode for this.
What extension mode changes
It turns on by itself when the page URL is chrome-extension:, moz-extension:, safari-web-extension: or ms-browser-extension:. Force it with extension: true, for example in a content script.
No cookies.
persistence: "cookie"and"localStorage+cookie"become"localStorage". The same applies toflagPersistenceandconsentPersistence. Writes still wait for consent.extensionPersistencepicks that replacement store (default"localStorage"). In a content script, which shares the host page'slocalStorage, use"memory"so the SDK writes nothing into the page's storage (the identity then lasts for one page load).No remote code. The classic script's
modulesoption is refused, so nothing is loaded fromassetHost. Lazy chunks of the npm build (the replay recorder, the guide kinds) are part of your package, so they still work.Everything else is unchanged: capture, flags, consent and replay.
Setup
Bundle the SDK with your extension (webpack, Vite, esbuild) and import the modules you need:
import { createClient, autocapture, flags } from "@lucida-analytics/browser";
import { replay } from "@lucida-analytics/browser/replay";
const lucida = createClient("lc_pk_...", { apiHost: "https://lc.example.com" }, [autocapture, flags, replay]);In manifest.json, allow the API host for fetch:
{
"host_permissions": ["https://lc.example.com/*"],
"content_security_policy": { "extension_pages": "script-src 'self'; object-src 'self'" }
}The SDK never uses eval or remote scripts, so the default extension CSP works.
Where to run it
Context | Works | Notes |
|---|---|---|
Popup, options page, side panel | yes | Has |
Content script | yes, with care | It runs in the host page, so its |
Background service worker | no | No DOM and no |
Popup pages close often. Lower flushAt or call lucida.flush() before the popup closes, so that queued events are sent. The SDK flushes with sendBeacon on pagehide too.
Consent
Extension stores have their own privacy rules. With consent: { mode: "opt_in" }, nothing is stored or sent until you call setConsent({ analytics: true }), for example from an onboarding page.