# Vite + React Router

> Step-by-step guide to integrate Ours Privacy with Vite + React Router for tracking page views and custom events in HIPAA-regulated workflows.

Source: https://docs.oursprivacy.com/docs/vite-react-router-integration

Install the Ours Privacy Web SDK in your Vite + React Router application, track page views, and log custom events, with analytics built for **HIPAA-regulated workflows**.

## Integration Options

The integration guide below covers using the [Ours Privacy Web SDK](https://docs.oursprivacy.com/docs/web-sdk-javascript).

For a no-code approach, we recommend the [Ours Privacy Tag Manager](https://docs.oursprivacy.com/docs/tag-manager), which provides visual tag management built for HIPAA-regulated sites.

Choose the approach that best suits your workflow.

***

[View example repo](https://github.com/with-ours/ours-vite-react-router)

***

## Installation Methods

There are **two ways** to install and initialize Ours Privacy in your Vite + React Router application:

### Method 1: NPM Package Installation (Recommended)

This method provides better TypeScript support, more control over the initialization process, and is the recommended approach for most Vite + React Router applications.

**Key steps:**

1. Install the Ours Privacy SDK: `npm install @oursprivacy/cdp-sdk`
2. Create an Analytics Provider component
3. Add the provider to your app
4. Add custom event tracking

**Example implementation:**

```tsx
// src/providers/analytics-provider.tsx
import { useEffect } from 'react';
import ours from '@oursprivacy/cdp-sdk';

export function AnalyticsProvider() {
  useEffect(() => {
    // Initialize Ours Privacy on app load
    ours.init('YOUR_SITE_ID', { track_web_events: true });
  }, []);

  return null;
}
```

```tsx
// src/App.tsx
import { AnalyticsProvider } from './providers/analytics-provider';

export const App = () => {
  return (
    <>
      <AnalyticsProvider />
      <RouterProvider router={router} />
    </>
  );
};
```

### Method 2: Script Tag Installation (Alternative for simple setups)

This method is suitable for straightforward integrations where you want minimal setup.

**Key steps:**

1. Get your snippet from the Ours Privacy dashboard
2. Add the script tag to your `index.html`
3. Add custom event tracking

**Example implementation:**

```html
<!-- index.html -->
<head>
  <!-- Ours Privacy Web SDK -->
  <script>
    // Paste your Ours Privacy initialization snippet here
    // Example:
    // window.ours = window.ours || function(){(window.ours.q=window.ours.q||[]).push(arguments)};
    // ours('init', 'YOUR_SITE_ID', {track_web_events: true});
  </script>
  <!-- End Ours Privacy Web SDK -->
</head>
```

## Tracking Page Views

Route changes are tracked for you. The Web SDK watches `history.pushState` and the browser's `popstate` event, so it records a `page_view` on the initial load and on every client-side route change. You do not need a router listener or a `useEffect`.

> **Note:** Do not add your own `PageView` call on route change. It creates a second, redundant event alongside the automatic `page_view`, which splits your page view reporting across two event names.

Initialize the SDK once when your app starts, then let React Router navigate. For the full picture, including the one routing style that does need extra handling, see [Tracking page views in single-page apps](https://docs.oursprivacy.com/docs/spa-pageview-tracking).

## Tracking Events

To track custom events such as button clicks or form submissions, use the `ours('track', ...)` function within your React components.

### Using NPM Package Method

```tsx
// src/components/track-button.tsx
import ours from '@oursprivacy/cdp-sdk';

export function TrackButton() {
  const handleClick = () => {
    ours.track('ButtonClicked', {
      button_name: 'sign_up',
      page: 'homepage',
    });
  };

  return <button onClick={handleClick}>Sign Up</button>;
}
```

### Using Script Tag Method

```tsx
// src/components/track-button.tsx
export function TrackButton() {
  const handleClick = () => {
    if (typeof window !== 'undefined' && window.ours) {
      window.ours('track', 'ButtonClicked', {
        button_name: 'sign_up',
        page: 'homepage',
      });
    }
  };

  return <button onClick={handleClick}>Sign Up</button>;
}
```

### Example: Tracking Form Submissions

```tsx
// src/components/contact-form.tsx
import ours from '@oursprivacy/cdp-sdk';

export function ContactForm() {
  const handleFormSubmit = (e: React.FormEvent) => {
    e.preventDefault();

    ours.track('FormSubmit', {
      form_name: 'contact_form',
      form_type: 'contact',
    });

    // Your form submission logic here
  };

  return (
    <form onSubmit={handleFormSubmit}>
      <input type="text" name="name" placeholder="Your Name" required />
      <input type="email" name="email" placeholder="Your Email" required />
      <button type="submit">Submit</button>
    </form>
  );
}
```

***

If you need further assistance, contact us at [support@oursprivacy.com](mailto:support@oursprivacy.com).
