# React

The Analytics component and track() for React apps.

The React package works in any React app, with or without a framework. [Other frameworks](/docs/install/frameworks#react-frameworks) shows where the component goes in TanStack Start and React Router.

## Install the package

```bash
npm install @ultanimus/react
```

## Add the component

Render `Analytics` once, near the root of your app.

`App.tsx`

```tsx
import { Analytics } from '@ultanimus/react'

export function App({ children }) {
  return (
    <>
      {children}
      <Analytics site="YOUR_SITE_ID" />
    </>
  )
}
```

> **Tip:** Use the component or the script tag, not both. A second copy on the same page counts nothing, and your site's settings show a note until you remove it.

## Without the package

The package is under 1 KB with no dependencies. It loads the same script as the snippet and adds a typed `track()`. To skip it, paste the [script tag](/docs/install/script-tag) into the `<head>` of your `index.html`, or of your root layout in a framework, and send events with `window.ultanimus?.track()`.

## Page weight

Add `load` to measure what one page load in ten downloads, for the [Load page](/docs/dashboard/load). A number from 1 to 100 sets another share.

```tsx
<Analytics site="YOUR_SITE_ID" load />
```

## Web vitals

Add `vitals` to measure LCP, INP and CLS on real page loads, for the [Web vitals page](/docs/dashboard/vitals). It measures every page load; a number from 1 to 100 sets a share.

```tsx
<Analytics site="YOUR_SITE_ID" vitals />
```

## Errors

Add `errors` to report the JavaScript errors visitors run into, for the [Errors page](/docs/dashboard/errors). The component adds the script after the page mounts, so errors thrown before that are missed. Put the [script tag](/docs/install/script-tag) in your HTML instead to catch those too.

```tsx
<Analytics site="YOUR_SITE_ID" errors />
```

## Content Security Policy

If your site sends a Content Security Policy, add the tracker's domain to `script-src` and `connect-src`, or the browser blocks the script:

```text
script-src 'self' https://cdn.ultanimus.com
connect-src 'self' https://cdn.ultanimus.com
```

[Script tag](/docs/install/script-tag#content-security-policy) explains where the policy usually lives.

## Send an event

Call `track` with a name and, if you want, properties.

```tsx
import { track } from '@ultanimus/react'

track('signup', { plan: 'pro' })
```

[Custom events](/docs/tracking/custom-events) covers naming and properties.

## Signed-in users

Pass your own ID for the signed-in person, or `null` when nobody is, and they count as one visitor across days and devices.

```tsx
<Analytics site="YOUR_SITE_ID" user={session?.userId ?? null} />
```

[Signed-in users](/docs/tracking/identify) explains what changes and what is stored.

## IDs in page addresses

Pass `url` to replace the IDs and names in your page addresses before they leave the browser. [Page addresses](/docs/tracking/page-addresses) covers what the function gets and returns.

```tsx
<Analytics site="YOUR_SITE_ID" url={(url) => url.replace(/\/orders\/\d+/, '/orders/[id]')} />
```

## Outbound links and downloads

Add `outbound` to record clicks on links to other sites, and `downloads` for clicks on files. [Links and downloads](/docs/tracking/links-and-downloads) lists what counts.

```tsx
<Analytics site="YOUR_SITE_ID" outbound downloads />
```
