# Install With npm

> Add the tracking snippet from your app's own code with the @behavly/browser package, in React, Next.js, Vue, Nuxt, SvelteKit or any JavaScript app.

Source: https://behavly.io/help-center/user-activity/install-with-npm · Behavly Help Center · Heatmaps & Tracking

If your site is a JavaScript app with a build step, you can add the tracking snippet from your own code instead of pasting the script tag. The `@behavly/browser` package adds the same tag for you and gives you typed `track` and `identify` calls. Both ways record exactly the same things.

```bash
npm install @behavly/browser
```

#### Start It

Call `init` once, with your site's write key from **Tracking → Setup**, in code that runs on every page:

```js
import { init } from "@behavly/browser";

init({ pid: "YOUR_WRITE_KEY" });
```

On the server it does nothing, so it is safe to import in Next.js, Nuxt and SvelteKit. Calling it again, or on a page that already has the script tag, adds nothing.

##### React And Next.js

Put the component in your root layout, once. In the Next.js App Router the layout can stay a server component:

```tsx
import { BehavlyScript } from "@behavly/browser/react";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <BehavlyScript pid="YOUR_WRITE_KEY" />
      </body>
    </html>
  );
}
```

Vue, Nuxt and SvelteKit examples are in the [package's README](https://www.npmjs.com/package/@behavly/browser).

> Note: There is no page view code to add. The snippet records every page on its own, including navigation inside a single-page app.

#### Send Events

```js
import { track, identify } from "@behavly/browser";

track("signup", { plan: "pro" });
identify("person@example.com", { name: "Pat Example" });
```

A call made before the snippet has loaded waits and is sent as soon as it starts. You rarely need `identify`: sign-ins are recognised on their own (see [Identity](https://behavly.io/help-center/user-activity/install-the-snippet#identity)). `consent`, `reset`, `screen`, `flush`, `optOut` and `optIn` work as they do on `window.behavly`.

#### Options

Each option sets one of the script tag's attributes, described in [Install The Tracking Snippet](https://behavly.io/help-center/user-activity/install-the-snippet):

| Option | Attribute |
| --- | --- |
| `allowLocalhost: true` | `data-allow-localhost="true"` |
| `allowFileProtocol: true` | `data-allow-file-protocol="true"` |
| `debug: true` | `data-debug="true"` |
| `disableConsole: true` | `data-disable-console="true"` |
| `allowedHostnames: ["example.io"]` | `data-allowed-hostnames="example.io"` |
| `src: "https://a.example.com/t.js"` | The address the snippet loads from, for [your own subdomain](https://behavly.io/help-center/user-activity/proxy-through-your-subdomain). |

See also: https://behavly.io/help-center/user-activity/install-the-snippet, https://behavly.io/help-center/user-activity/proxy-through-your-subdomain
