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...

Embed
Preview of the card
<a href="https://lucidace.com/help/browser-extension"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/15a50026-00f3-48a6-b50d-4035f7f7016c/social-card.svg?v=3e7ae33eec8b" alt="Using the browser SDK in a browser extension (Manifest V3)" width="600" height="315"></a>

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 to flagPersistence and consentPersistence. Writes still wait for consent.

  • extensionPersistence picks that replacement store (default "localStorage"). In a content script, which shares the host page's localStorage, 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 modules option is refused, so nothing is loaded from assetHost. 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 window, DOM and localStorage. Use the defaults.

Content script

yes, with care

It runs in the host page, so its localStorage belongs to the host site. Use persistence: "memory" (or "none") and set extension: true. Autocapture would record the host page, so turn it off unless that is what you want.

Background service worker

no

No DOM and no localStorage. Send events from the worker with the Node or server SDK, or message a page.

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.

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.

Updated