> ## Documentation Index
> Fetch the complete documentation index at: https://agentref.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Svelte and SvelteKit Integration

> Install AgentRef in SvelteKit or plain Svelte and keep attribution stable across navigation.

Svelte and SvelteKit both work well with AgentRef as long as the script is installed in the global shell instead of a page-specific component.

## Before you start

* Set the program website in AgentRef to the same root domain your app serves.
* Copy the exact script snippet from `Settings -> Integration`.
* If your app and checkout start on different subdomains, install AgentRef on both.

## Recommended installation

<Tabs>
  <Tab title="SvelteKit">
    Install the script in `src/app.html` so it is available across all routes.

    ```html theme={null}
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script
          defer
          src="https://www.agentref.co/api/tracking/script.js?pid=YOUR_PROGRAM_ID"
        ></script>
        %sveltekit.head%
      </head>
      <body>
        <div style="display: contents">%sveltekit.body%</div>
      </body>
    </html>
    ```
  </Tab>

  <Tab title="Plain Svelte">
    For plain Svelte apps, install the script in the shared HTML shell.

    ```html theme={null}
    <!-- index.html -->
    <script
      defer
      src="https://www.agentref.co/api/tracking/script.js?pid=YOUR_PROGRAM_ID"
    ></script>
    ```
  </Tab>
</Tabs>

## Checkout compatibility in Svelte

| Checkout pattern                                | Recommendation                                               |
| ----------------------------------------------- | ------------------------------------------------------------ |
| Hosted Stripe surfaces on Svelte pages          | Supported directly.                                          |
| Custom Checkout Sessions created by your server | Supported with the [Stripe guide](/docs/integration/stripe). |
| Checkout on a different root domain             | Not automatic because AgentRef uses first-party cookies.     |

## Svelte-specific notes

* Do not reinstall AgentRef in `onMount()` for every route. One shell-level install is enough.
* Hosted Stripe elements rendered after hydration are still observed automatically.
* If you gate cookies behind consent, call `window.AgentRef.setConsent('granted')` after opt-in.

## Verify the installation

1. Open the live app through a real affiliate link.
2. Confirm `agentref_cid` and `agentref_pid` in browser cookies.
3. Add `?agentref_debug=1` and inspect the console.
4. Complete one supported checkout flow and verify the conversion in AgentRef.

## Troubleshooting

* `window.AgentRef` is undefined: the script was not installed in the global shell.
* Clicks appear but conversions do not: your custom checkout path still needs the [Stripe metadata bridge](/docs/integration/stripe).
* Cookies fail across domains: move the journey onto one root or handle the handoff server-side.

## Related docs

<CardGroup cols={3}>
  <Card title="Stripe Checkout" icon="credit-card" href="/docs/integration/stripe">
    Metadata bridge for custom Stripe sessions.
  </Card>

  <Card title="Consent and GDPR" icon="shield" href="/docs/tracking/consent-gdpr">
    Coordinate AgentRef with your consent banner.
  </Card>

  <Card title="Debug Mode" icon="bug" href="/docs/tracking/debug-mode">
    Runtime inspection and troubleshooting.
  </Card>
</CardGroup>
