# Svelte session replay installation - Docs

1.  1

    ## Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    PostHog AI

    ### npm

    ```bash
    npm install posthog-js
    ```

    ### yarn

    ```bash
    yarn add posthog-js
    ```

    ### pnpm

    ```bash
    pnpm add posthog-js
    ```

2.  2

    ## Initialize PostHog

    Required

    If you haven't created a root layout already, create a new file called `+layout.js` in your `src/routes` folder. Check the environment is the browser, and initialize PostHog if so:

    src/routes/+layout.js

    PostHog AI

    ```javascript
    import posthog from 'posthog-js'
    import { browser } from '$app/environment';
    import { onMount } from 'svelte';
    export const load = async () => {
      if (browser) {
        posthog.init(
          '<ph_project_token>',
          {
            api_host: 'https://us.i.posthog.com',
            defaults: '2026-01-30'
          }
        )
      }
      return
    };
    ```

    **SvelteKit layout**

    Learn more about [SvelteKit layouts](https://kit.svelte.dev/docs/routing#layout) in the official documentation.

3.  3

    ## Watch session recordings

    Recommended

    Visit your site or app and interact with it for at least 10 seconds to generate a recording. Navigate between pages, click buttons, and fill out forms to capture meaningful interactions.

    [Watch your first recording →](/replay/home.md)

4.  4

    ## Next steps

    Recommended

    Now that you're recording sessions, continue with the resources below to learn what else Session Replay enables within the PostHog platform.

    | Resource | Description |
    | --- | --- |
    | [Watching recordings](/docs/session-replay/how-to-watch-recordings.md) | How to find and watch session recordings |
    | [Privacy controls](/docs/session-replay/privacy.md) | How to mask sensitive data in recordings |
    | [Network recording](/docs/session-replay/network-recording.md) | How to capture network requests in recordings |
    | [Console log recording](/docs/session-replay/console-log-recording.md) | How to capture console logs in recordings |
    | [More tutorials](/docs/session-replay/tutorials.md) | Other real-world examples and use cases |

### Community questions

Ask a question

### Was this page useful?

HelpfulCould be better