Svelte experiments 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. Server-side setup

    Optional

    Install posthog-node using your package manager:

    npm install posthog-node --save

    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
    import { PostHog } from 'posthog-node';
    export async function load() {
    const posthog = new PostHog('<ph_project_api_key>', { 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. 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:

    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. 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. Next steps

    Recommended
    ResourceDescription
    Creating an experimentHow to create an experiment in PostHog
    Adding experiment codeHow to implement experiments for all platforms
    Statistical significanceUnderstanding when results are meaningful
    Experiment insightsHow to analyze your experiment data
    More tutorialsOther real-world examples and use cases

Community questions

Was this page useful?

Questions about this page? or post a community question.