# Astro experiments installation - Docs

1.  1

    ## Create the PostHog component

    Required

    In your `src/components` folder, create a `posthog.astro` file:

    Terminal

    PostHog AI

    ```bash
    cd ./src/components
    # or 'cd ./src && mkdir components && cd ./components' if your components folder doesn't exist
    touch posthog.astro
    ```

    In this file, add your PostHog web snippet. Be sure to include the `is:inline` directive to prevent Astro from processing it:

    src/components/posthog.astro

    PostHog AI

    ```html
    ---
    // src/components/posthog.astro
    ---
    <script is:inline>
        !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture register register_once register_for_session unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group identify setPersonProperties setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags resetGroups onFeatureFlags addFeatureFlagsHandler onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
        posthog.init('<ph_project_token>', {
            api_host: 'https://us.i.posthog.com',
            defaults: '2026-01-30'
        })
    </script>
    ```

2.  2

    ## Create a layout

    Required

    Create a layout where we will use `posthog.astro`. Create a new file `PostHogLayout.astro` in your `src/layouts` folder:

    Terminal

    PostHog AI

    ```bash
    cd ./src/layouts
    # or 'cd ./src && mkdir layouts && cd ./layouts' if your layouts folder doesn't exist
    touch PostHogLayout.astro
    ```

    Add the following code to `PostHogLayout.astro`:

    src/layouts/PostHogLayout.astro

    PostHog AI

    ```html
    ---
    import PostHog from '../components/posthog.astro'
    ---
    <head>
        <PostHog />
    </head>
    ```

3.  3

    ## Use the layout in your pages

    Required

    Update your pages (like `index.astro`) to wrap your app with the new layout:

    src/pages/index.astro

    PostHog AI

    ```html
    ---
    import PostHogLayout from '../layouts/PostHogLayout.astro';
    ---
    <PostHogLayout>
      <!-- your existing app components -->
    </PostHogLayout>
    ```

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:

    ```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