# Install Wiki: [Quick Install](https://github.com/infinitnet/cookieless-analytics/wiki/Quick-Install). Cookieless Analytics is a native EmDash plugin, published on npm as `@infinitnet/cookieless-analytics`. A new site takes one click (Deploy to Cloudflare) and the EmDash setup wizard. On an existing EmDash site you install the package, add it to your EmDash config and to your Astro integrations, make sure your layout renders `EmDashHead`, and redeploy (steps 1 to 4). The plugin creates its tables on first use; there is no migration to run. Other sites are measured with one script tag ([client-sites.md](client-sites.md)), and the data can live in the EmDash D1 database, an own D1 database or Postgres through Hyperdrive. The EmDash plugin registry lists sandboxed plugins only, so the plugin is not listed there. The target setup is Cloudflare Workers with D1. Peer ranges: `emdash` `>=1.4.0 <2`, `astro` `^7.0.0`, `react` 18 or 19 (for the admin). Node 20 or newer builds it. ## New site: Deploy to Cloudflare For a new site, the fast path is the Deploy to Cloudflare button: [![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/infinitnet/cookieless-analytics/tree/main/template) It copies [template/](../template/README.md) of this repository into a new repository on your GitHub or GitLab account and deploys it to your Cloudflare account. The template is the EmDash Cloudflare starter with steps 1 to 3 and the Cron Trigger already done: the dependency `@infinitnet/cookieless-analytics` from npm, the plugin and the integration in `astro.config.mjs`, `EmDashHead` in the layout, the D1 database `DB`, the R2 bucket `MEDIA` and the trigger `* * * * *` in `wrangler.jsonc`. Cloudflare creates the database and the bucket and asks for `EMDASH_ENCRYPTION_KEY` ([Secrets](#secrets-emdash_encryption_key); it can stay empty until you connect Search). Then: 1. Open `/_emdash/admin` on the new site and complete the setup wizard. 2. On Workers Free, set `capacityProfile` to Small ([Cloudflare notes](#cloudflare-notes)). 3. Run the checks of step 4 below and add the WAF rate rule. ## 1. Install the package ```sh npm install @infinitnet/cookieless-analytics # or pnpm add @infinitnet/cookieless-analytics yarn add @infinitnet/cookieless-analytics ``` ## 2. Add the plugin and the integration The package has two parts, and both are needed. - The plugin (default export of `@infinitnet/cookieless-analytics`) goes into the `plugins` option of `emdash()`. It owns the settings, the admin pages, the database tables, the routes and the cron tasks. - The Astro integration (default export of `@infinitnet/cookieless-analytics/integration`) goes into `integrations` **after** `emdash()`. It registers one middleware with `order: "pre"` that serves the page script, forwards the event path, serves shared dashboards and logs human error pages. EmDash's own middleware must run first, because it fills `locals.emdash`, which the middleware uses. ```js // astro.config.mjs import { defineConfig } from "astro/config"; import emdash from "emdash/astro"; import cookielessAnalytics from "@infinitnet/cookieless-analytics"; import cookielessAnalyticsIntegration from "@infinitnet/cookieless-analytics/integration"; export default defineConfig({ integrations: [ emdash({ // database, storage and the rest of your EmDash config plugins: [cookielessAnalytics()], }), cookielessAnalyticsIntegration(), ], }); ``` Options: - `cookielessAnalytics(options)` takes three options, all optional. `sites` is a list of connected EmDash installs ([multi-site.md](multi-site.md)). `database` puts the analytics tables in a D1 database of their own ([Own database for analytics](#own-database-for-analytics)). `databases` gives chosen sites a D1 database each ([One database per site](#one-database-per-site)); it replaces `database`, the two cannot be combined. A bad entry stops the build with a message that names it. - `cookielessAnalyticsIntegration()` takes no options. - Everything else (paths, session window, retention, privacy signals) is a setting in the admin ([settings.md](settings.md)). The package also exports `@infinitnet/cookieless-analytics/contract` (constants and types for other plugins) and `@infinitnet/cookieless-analytics/server` (`recordEvent`). See [integration.md](integration.md). ## 3. Render `EmDashHead` in your layout The page script is added to your pages by the plugin's `page:fragments` hook. EmDash runs that hook only inside its `EmDashHead` component, because plugin hooks cannot see or change responses by themselves. If your layout does not render `EmDashHead`, no script reaches the page and nothing is counted. ```astro --- import { EmDashHead } from "emdash/ui"; import { createPublicPageContext } from "emdash/page"; const page = createPublicPageContext({ Astro, kind: "custom", title: Astro.props.title }); --- ``` The hook adds exactly one tag to ``, the same for every request and every user agent: ```html ``` If your theme must add the tag itself, turn off the setting `injectScript` and add the tag by hand with the path from `scriptPath`. With `scriptDelivery` set to `inline` the plugin emits one inline script with the same code instead of the tag. Without the integration (step 2) the plugin falls back to an inline script that posts to `/_emdash/api/plugins/cookieless-analytics/in`. Counting still works in that degraded mode, but the neutral paths, error page logging and shared dashboards need the integration. ## 4. Deploy and check Redeploy the site. The plugin creates its tables when it activates and runs forward-only migrations on first use. Then check: 1. Open the EmDash admin. The sidebar has a folder "Analytics" with nine pages. 2. View the source of a public page. It holds one `