# Install on React Router

> Add Reveata to a React Router app, in framework mode (root.tsx) or as a single-page app.

Where the snippet goes depends on how you use React Router.

Copy the snippet from your Reveata dashboard (it contains your website’s tracking id):

```html
<script
  defer
  data-website-id="ws_your_tracking_id"
  src="https://reveata.com/js/script.js"
></script>
```

## Framework mode

In framework mode (React Router 7 with a `react-router.config.ts`, including Remix apps moved to it), `app/root.tsx` renders the `<head>` of every page. Add the tag to its `Layout`:

```tsx
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router';

export function Layout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
        <script defer data-website-id="ws_your_tracking_id" src="https://reveata.com/js/script.js" />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  );
}

export default function App() {
  return <Outlet />;
}
```

This works with server rendering and with `ssr: false`, because both render `root.tsx` into the HTML.

## Declarative and data mode

When you render `<BrowserRouter>` or `createBrowserRouter` yourself in a Vite app, the page comes from `index.html` at the root of the project. Paste the snippet before `</head>` there, as in [Install on React](https://reveata.com/docs/react).

## Navigation

React Router changes pages through the History API, which the script follows, so every route is counted with no code in your components. Changes to the query string alone (`?tab=2`) are not new pages. With `createHashRouter` (URLs like `/#/settings`), add `data-hash="true"` to the tag.

## Check it works

Open your deployed app in a private window, then check that the visit appears in your Reveata dashboard. If it doesn’t, follow [Verify your installation](https://reveata.com/docs/verify-your-installation). Visits to the development server on `localhost` are ignored unless you add `data-allow-localhost="true"`.

## Goals

A React Router `<Form>` renders a regular `<form>`, so the attribute works on it. The goal counts when the form is submitted, even though React Router handles the submission:

```tsx
<Form method="post" action="/newsletter" data-reveata-goal="newsletter_signup">
  <input type="email" name="email" required />
  <button type="submit">Subscribe</button>
</Form>
```

From code, call `window.reveata?.('goal', 'signup')`. See [Custom goals](https://reveata.com/docs/custom-goals).

## Serve it from your own domain

- **The managed proxy.** One DNS record (for example `stats.example.com`), no code: see [Managed proxy](https://reveata.com/docs/managed-proxy). This works for every way of deploying React Router.
- **Your own Express server.** If you serve the app with `@react-router/express`, add the two Reveata routes to the Express app before the React Router request handler: see [Proxy through Express](https://reveata.com/docs/express#proxy-through-express).
- **A single-page app on a static host** can proxy with its host’s rewrites: see [Vercel](https://reveata.com/docs/self-hosted-proxy#vercel), [Netlify](https://reveata.com/docs/self-hosted-proxy#netlify) or [Cloudflare Workers](https://reveata.com/docs/self-hosted-proxy#cloudflare-workers).

## Revenue

First connect your payment provider in Reveata: see [Connect a payment provider](https://reveata.com/docs/connect-a-payment-provider). Then pass the Reveata visitor id with each checkout, so the payment is credited to the visit that brought the buyer.

In framework mode a checkout usually starts in an `action`. Send the ids with the form from the browser, then add them to the checkout metadata on the server:

```tsx
// app/routes/pricing.tsx
import { Form, redirect } from 'react-router';
import type { Route } from './+types/pricing';
import { stripe } from '~/stripe.server';

export async function action({ request }: Route.ActionArgs) {
  const form = await request.formData();
  const metadata: Record<string, string> = {};
  for (const key of ['reveata_visitor_id', 'reveata_session_id']) {
    const value = form.get(key);
    if (typeof value === 'string' && value) metadata[key] = value;
  }
  const session = await stripe.checkout.sessions.create({
    mode: 'payment',
    line_items: [{ price: 'price_123', quantity: 1 }],
    success_url: new URL('/thanks?session_id={CHECKOUT_SESSION_ID}', request.url).toString(),
    metadata,
  });
  return redirect(session.url!, 303);
}

export default function Pricing() {
  return (
    <Form
      method="post"
      onSubmit={(e) => {
        const ids = window.reveata?.metadata?.() ?? {};
        for (const [name, value] of Object.entries(ids)) {
          const input = e.currentTarget.elements.namedItem(name);
          if (input instanceof HTMLInputElement) input.value = value;
        }
      }}
    >
      <input type="hidden" name="reveata_visitor_id" />
      <input type="hidden" name="reveata_session_id" />
      <button type="submit">Buy</button>
    </Form>
  );
}
```

`window.reveata` has no TypeScript type of its own; declare it once (for example `declare global { interface Window { reveata?: { metadata?: () => Record<string, string> } } }`). If Reveata doesn’t connect to your provider, send each payment from an `action` or your backend with the [Payments API](https://reveata.com/docs/payments-api).

## Common pitfalls

- **The script in a component.** Rendering the tag inside a route component adds it on every navigation. Keep it in `root.tsx` (framework mode) or `index.html` (single-page app).
- **Hash routing.** With `createHashRouter`, add `data-hash="true"` to the snippet.
- **Testing on localhost.** The script ignores `localhost` unless you add `data-allow-localhost="true"`.
- **Counting payments twice.** With a provider connected in Reveata, don’t also send its payments with the Payments API.
