# React Router Web Analytics installation - Docs

1.  1

    ## Install the package

    Required

    Install the PostHog JavaScript library and React SDK using your package manager:

    PostHog AI

    ### npm

    ```bash
    npm install --save posthog-js @posthog/react
    ```

    ### yarn

    ```bash
    yarn add posthog-js @posthog/react
    ```

    ### pnpm

    ```bash
    pnpm add posthog-js @posthog/react
    ```

2.  2

    ## Configure Vite

    Required

    Add `posthog-js` and `@posthog/react` to `ssr.noExternal` in your `vite.config.ts` to avoid SSR errors:

    vite.config.ts

    PostHog AI

    ```typescript
    // ... imports
    export default defineConfig({
      plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
      ssr: {
        noExternal: ['posthog-js', '@posthog/react'],
      },
    });
    ```

3.  3

    ## Add the PostHogProvider

    Required

    Initialize PostHog and wrap your app with the `PostHogProvider` in your `app/entry.client.tsx` file:

    app/entry.client.tsx

    PostHog AI

    ```typescript
    import { startTransition, StrictMode } from "react";
    import { hydrateRoot } from "react-dom/client";
    import { HydratedRouter } from "react-router/dom";
    import posthog from "posthog-js";
    import { PostHogProvider } from "@posthog/react";
    posthog.init("<ph_project_token>", {
      api_host: "https://us.i.posthog.com",
      defaults: "2026-01-30",
    });
    startTransition(() => {
      hydrateRoot(
        document,
        <PostHogProvider client={posthog}>
          <StrictMode>
            <HydratedRouter />
          </StrictMode>
        </PostHogProvider>,
      );
    });
    ```

4.  4

    ## Send events

    Click around and view a couple pages to generate some events. PostHog automatically captures pageviews, clicks, and other interactions for you.

    If you'd like, you can also manually capture custom events:

    JavaScript

    PostHog AI

    ```javascript
    posthog.capture('my_custom_event', { property: 'value' })
    ```

5.  5

    ## Next steps

    Recommended

    After installing PostHog and ensuring [autocapture](/docs/data/autocapture.md) is enabled, head to your [web analytics dashboard](/docs/web-analytics/dashboard.md) to see your data. And then check out our [getting started](/docs/web-analytics/getting-started.md) guide.

    > **PostHog tip:** Web analytics works with [anonymous events](/docs/data/anonymous-vs-identified-events.md). This means if you are primarily using PostHog for web analytics, it can be significantly cheaper for you.

### Community questions

Ask a question

### Was this page useful?

HelpfulCould be better