Install Lucida in a browser extension

Add Lucida to a Manifest V3 extension for Chrome, Edge or Firefox: installs, updates and uninstalls, popup and content-script events, feature flags.

Embed
Preview of the card
<a href="https://lucidace.com/help/install-chrome-extension"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/0e39af85-72ac-42ac-88dc-739f09ed7d2d/social-card.svg?v=3e7ae33eec8b" alt="Install Lucida in a browser extension" width="600" height="315"></a>

@lucida-analytics/chrome-extension is for Manifest V3 extensions in Chrome, Edge, Brave, Opera and Firefox. The background worker owns the client. Popups, options pages, side panels and content scripts send to it with messages, so the whole extension shares one person, one queue and one set of flags.

1. Install

npm i @lucida-analytics/chrome-extension

Bundle it with your extension (Vite, esbuild, webpack). The Chrome Web Store doesn't allow code loaded from a server, and nothing in the SDK does that.

Or let the CLI do steps 2 and 3: lucida init --host https://capture.lucidace.com in the extension's folder. It detects the setup and:

Setup

Key

Client

Wired into

Storage permission

WXT (--framework wxt)

WXT_LUCIDA_KEY in .env

utils/lucida.ts (startLucida())

first line of defineBackground's main in entrypoints/background.ts

manifest.permissions in wxt.config.ts

Plasmo (--framework plasmo)

PLASMO_PUBLIC_LUCIDA_KEY in .env

lucida.ts

background.ts (created when there is none)

manifest.permissions in package.json

manifest.json with a bundler (--framework chrome-extension)

VITE_LUCIDA_KEY in .env.local with Vite, else LUCIDA_KEY

lucida.ts next to the worker

the background.service_worker file

permissions in manifest.json

JSON files keep their key order. Running it twice changes nothing. Popups and content scripts still use connect() (step 4).

2. Manifest

{
  "manifest_version": 3,
  "permissions": ["storage"],
  "background": { "service_worker": "background.js", "type": "module" }
}

storage keeps the person's id and unsent events when Chrome stops the worker. Without it the SDK still works, but it warns in the console and every restart looks like a new person.

3. Background worker

import { createClient } from "@lucida-analytics/chrome-extension";

const lucida = createClient("lc_pk_YOUR_PUBLIC_KEY");

Call it at the top level of the worker file. Chrome only delivers the install event to listeners added when the worker starts.

4. Popup, options page, side panel and content scripts

import { connect } from "@lucida-analytics/chrome-extension/page";

const lucida = connect();
lucida.capture("highlight_saved");
if (await lucida.isFeatureEnabled("new-toolbar")) showToolbar();

On an extension page, connect() sends a $screen named after the page (popup.html). Every event says where it came from in $extension_context: popup, options, side_panel or content_script.

What you get

Event

When

$extension_installed

First install

$extension_updated

A new version is installed ($previous_version)

$extension_started

The browser starts with the extension enabled

$extension_uninstalled

The extension is removed (see below)

$screen

An extension page opens

Every event carries $extension_id, $extension_version (also $app_version), $os, $browser and $browser_version. Install and uninstall counts per version give you retention and churn per release.

Uninstalls

By default, removing the extension opens a short Lucida page in a new tab. It records $extension_uninstalled and tells the user the extension was removed. To show your own page (an exit survey), pass createClient(key, { uninstall: "https://example.com/bye" }). It opens with lucida_distinct_id and lucida_extension_version appended, so put the Lucida web snippet on it and call identify with that id. uninstall: false turns it off.

Privacy

  • Content-script events say nothing about the page the user is on. tabUrl: "host" adds $host, and tabUrl: "url" adds $current_url too. Say so in your privacy policy and Web Store data disclosure before you turn it on.

  • consent: { mode: "opt_in" } captures nothing until optIn(). After optOut() the uninstall URL is cleared too.

Updated