Install Lucida on Astro

Add the Lucida browser SDK to an Astro site: package, a client script with only the key, CSP, optional proxy, consent and a first-event check.

Embed
Preview of the card
<a href="https://lucidace.com/help/install-astro"><img src="https://lucidace.com/help/_api/public/orgs/af34862a-c837-4478-8335-0505efe40459/spaces/2f4b888b-c4e6-488b-81bd-6e838fb12d2e/pages/9ea39d97-894c-4e4d-80fe-5eb4ff33c327/social-card.svg?v=3e7ae33eec8b" alt="Install Lucida on Astro" width="600" height="315"></a>

For an Astro site. Page views and events come from the browser SDK in a client script. The @lucida-analytics/astro middleware is optional: it resolves the visitor and flags on the server.

1. Install

npm install @lucida-analytics/astro @lucida-analytics/browser

Put the public key in .env:

PUBLIC_LUCIDA_KEY=lc_pk_YOUR_PUBLIC_KEY

lucida init --framework astro --host https://capture.lucidace.com writes src/lucida.ts and the env var.

2. Init with the key

// src/lucida.ts
import { init } from "@lucida-analytics/browser";

export const lucida = init(import.meta.env.PUBLIC_LUCIDA_KEY);

Import it once in your base layout:

---
// src/layouts/Base.astro
---
<slot />
<script>
  import "../lucida";
</script>

No host option is needed on the cloud (default https://capture.lucidace.com). Pass { apiHost: "..." } as the second argument only when you self-host or use a reverse proxy.

Optional server step, for server-resolved flags (no flicker) and the anonymous id:

// src/middleware.ts
import { lucidaMiddleware } from "@lucida-analytics/astro";
export const onRequest = lucidaMiddleware({ apiKey: import.meta.env.PUBLIC_LUCIDA_KEY, host: "https://capture.lucidace.com" });

Astro.locals.lucida then has distinctId and flags; bootstrapScript(lc) writes them to window.__LUCIDA_BOOTSTRAP__ and you pass that as init(key, { bootstrap: window.__LUCIDA_BOOTSTRAP__ }). The cookie is stored only when consent allows. Here host is the edge client's option and is required, not the browser SDK's apiHost.

3. Content Security Policy

script-src  'self';
connect-src 'self' https://capture.lucidace.com;

Astro bundles the script, so 'self' covers it. The SDK never uses eval. Through a proxy on your own domain, connect-src 'self' is enough. More for guides, experiments and the toolbar: Content Security Policy.

4. Reverse proxy (optional)

Set apiHost to a path on your domain and forward /v1/* to https://capture.lucidace.com:

init(import.meta.env.PUBLIC_LUCIDA_KEY, { apiHost: "https://example.com/lc", uiHost: "https://lucidace.com" });

Astro has no built-in rewrite for a static site: use your host (nginx, Vercel vercel.json, Cloudflare Worker). The rules are in Serve Lucida from your own domain. uiHost is needed because apiHost then no longer points at Lucida.

The SDK default is opt_out: it stores and sends before the visitor decides. For prior consent, start in opt_in:

export const lucida = init(import.meta.env.PUBLIC_LUCIDA_KEY, { consent: { mode: "opt_in" } });
// from your banner:
lucida.setConsent({ analytics: true, replay: true, marketing: false });

For Cookie Munch pass cookieMunchConsent from @lucida-analytics/browser/consent as a module: Cookie Munch consent. Defaults and regional rules: Consent by location.

6. Check the first event

  • Terminal: lucida init --framework astro --host https://capture.lucidace.com --project $LUCIDA_PROJECT_ID --verify waits for a new event (needs an account key). Load a page of your site.

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

Updated