# Svelte experiments 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

    ## Server-side setup

    Optional

    Install `posthog-node` using your package manager:

    PostHog AI

    ### npm

    ```bash
    npm install posthog-node --save
    ```

    ### yarn

    ```bash
    yarn add posthog-node
    ```

    ### pnpm

    ```bash
    pnpm add posthog-node
    ```

    ### Bun

    ```bash
    bun add posthog-node
    ```

    Then, initialize the PostHog Node client where you'd like to use it on the server side. For example, in a load function:

    routes/+page.server.js

    PostHog AI

    ```javascript
    import { PostHog } from 'posthog-node';
    export async function load() {
      const posthog = new PostHog('<ph_project_token>', { host: 'https://us.i.posthog.com' });
      posthog.capture({
        distinctId: 'distinct_id_of_the_user',
        event: 'event_name',
      })
      await posthog.shutdown()
    }
    ```

    **Note**

    Make sure to always call `posthog.shutdown()` after capturing events from the server-side. PostHog queues events into larger batches, and this call forces all batched events to be flushed immediately.

4.  4

    ## Implement your experiment

    Required

    Experiments run on top of our feature flags. You can define which version of your code runs based on the return value of the feature flag.

    For client-side experiments, use the JavaScript snippet. For server-side experiments, use the Node.js snippet:

    ```javascript
    if (posthog.getFeatureFlag('your-experiment-feature-flag') === 'test') {
        // Do something differently for this user
    } else {
        // It's a good idea to let control variant always be the default behaviour,
        // so if something goes wrong with flag evaluation, you don't break your app.
    }
    // Test that it works
    posthog.featureFlags.overrideFeatureFlags({ flags: {'your-experiment-feature-flag': 'test'} })
    ```

5.  5

    ## Run your experiment

    Required

    Once you've implemented the feature flag in your code, you'll enable it for a target audience by creating a new experiment in the PostHog dashboard.

6.  6

    ## Next steps

    Recommended

    Now that you're running experiments, continue with the resources below to learn what else Experiments enables within the PostHog platform.

    | Resource | Description |
    | --- | --- |
    | [Creating an experiment](/docs/experiments/creating-an-experiment.md) | How to create an experiment in PostHog |
    | [Adding experiment code](/docs/experiments/adding-experiment-code.md) | How to implement experiments for all platforms |
    | [Statistical significance](/docs/experiments/statistics-bayesian.md) | Understanding when results are meaningful |
    | [Experiment insights](/docs/experiments/analyzing-results.md) | How to analyze your experiment data |
    | [More tutorials](/docs/experiments/tutorials.md) | Other real-world examples and use cases |

### Community questions

Ask a question

### Was this page useful?

HelpfulCould be better