Svelte session replay installation

  1. Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    npm install posthog-js
  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
    import posthog from 'posthog-js'
    import { browser } from '$app/environment';
    import { onMount } from 'svelte';
    export const load = async () => {
    if (browser) {
    posthog.init(
    '<ph_project_api_key>',
    {
    api_host: 'https://us.i.posthog.com',
    defaults: '2025-11-30'
    }
    )
    }
    return
    };
    SvelteKit layout

    Learn more about SvelteKit layouts in the official documentation.

  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 →

  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.

    ResourceDescription
    Watching recordingsHow to find and watch session recordings
    Privacy controlsHow to mask sensitive data in recordings
    Network recordingHow to capture network requests in recordings
    Console log recordingHow to capture console logs in recordings
    More tutorialsOther real-world examples and use cases

Community questions

Was this page useful?

Questions about this page? or post a community question.