# Install on SvelteKit

> Add MetricRaven to a SvelteKit app with npx sv add @metricraven, or by pasting the tag into src/app.html.

Source: https://metricraven.com/docs/install-sveltekit  
Markdown: https://metricraven.com/docs/install-sveltekit.md  
Section: Installation guides

Every SvelteKit page, whether server-rendered, prerendered or a client-side navigation, is built from `src/app.html`. One tag in its `<head>` covers the whole app, and navigations between routes are counted as pageviews without extra code.

## With the Svelte CLI

In your project folder, run the MetricRaven add-on and paste your site ID when asked. It adds the tag to `src/app.html` (or to `index.html` in a Svelte app without Kit) and installs nothing else. Running it again updates the site ID on the existing tag.

```bash title="Terminal"
npx sv add @metricraven

# or without prompts (write the proxy host without https://)
npx sv add @metricraven="siteId:YOUR_SITE_ID"
npx sv add @metricraven="siteId:YOUR_SITE_ID+apiHost:a.example.com"
```

`apiHost` is optional: set it to your [managed proxy](https://metricraven.com/docs/managed-proxy) subdomain to load the script and send events through your own domain.

## By hand

1. **Copy your snippet**

   In MetricRaven, open your site's **Settings → Tracking & install → Install** card and copy the script tag. It already contains your site ID.

2. **Paste it into src/app.html**

   Replace `YOUR_SITE_ID` with the public site ID from your site's **Settings → Tracking & install → Install** card. The site ID is public and safe in browser code; never put your secret key in a page.

   ```html title="src/app.html"
   <!-- src/app.html -->
   <!doctype html>
   <html lang="en">
   	<head>
   		<meta charset="utf-8" />
   		<meta name="viewport" content="width=device-width, initial-scale=1" />
   		<script defer src="https://metricraven.com/js/script.js" data-site-id="YOUR_SITE_ID"></script>
   		%sveltekit.head%
   	</head>
   	<body data-sveltekit-preload-data="hover">
   		<div style="display: contents">%sveltekit.body%</div>
   	</body>
   </html>
   ```

   Keep your existing file and add only the `<script>` line, above `%sveltekit.head%`.

3. **Build, deploy and open your app**

   `vite dev` runs on `localhost`, which is not tracked by default; check the deployed app.

> **Verify it works**
>
> Open your live website in a normal browser window (not an automated or embedded one). In the browser's Network panel, look for `script.js` and then a request to `/api/v1/collect` whose response contains `ok: true`. Open your site's **Traffic** tab and choose **Refresh**; the selected dates must include your visit.
>
> Nothing showing? See [Troubleshooting](https://metricraven.com/docs/troubleshooting).

## Notes

- Click between a few pages and check the Network panel shows one `/api/v1/collect` pageview per navigation. Two per click means the tag is loaded twice.
- For typed `track`, `identify` and consent calls from components, use the [npm package](https://metricraven.com/docs/npm-package) alongside the tag, or instead of it.

## Questions

### Can the site ID come from an environment variable?

Yes. SvelteKit replaces `%sveltekit.env.PUBLIC_METRICRAVEN_SITE_ID%` in `src/app.html` with that public environment variable, so write `data-site-id="%sveltekit.env.PUBLIC_METRICRAVEN_SITE_ID%"`.

### My app sets kit.csp. What do I add?

Add `nonce="%sveltekit.nonce%"` to the tag so it gets the per-request nonce, and allow `https://metricraven.com` in `connect-src`. See [Script blocked by Content Security Policy (CSP)](https://metricraven.com/docs/content-security-policy).

Optional script attributes are listed in [Install & verify](https://metricraven.com/docs/install). To exclude your own visits, see [Exclude your visits](https://metricraven.com/docs/exclude-visits).
