Javascript Web experiments installation

Last updated:

|
  1. Install PostHog JavaScript web SDK

    Required

    Install PostHog in seconds with our wizard by running this command in your project directory with your terminal (it also works for LLM coding agents like Cursor and Bolt):

    prompt
    npx -y @posthog/wizard@latest --region us

    Wait for it to finish and test the setup once the wizard is complete.

    The wizard supports React, Next.js, Svelte, and React Native. We've got more on the way.

    Check out the wizard's GitHub repo for more details.

    Set up a reverse proxy (recommended)

    We recommend setting up a reverse proxy, so that events are less likely to be intercepted by tracking blockers.

    We have our own managed reverse proxy service included in the platform add-ons, which routes through our infrastructure and makes setting up your proxy easy.

    If you don't want to use our managed service then there are several other options for creating a reverse proxy, including using Cloudflare, AWS Cloudfront, and Vercel.

    Grouping products in one project (recommended)

    If you have multiple customer-facing products (e.g. a marketing website + mobile app + web app), it's best to install PostHog on them all and group them in one project.

    This makes it possible to track users across their entire journey (e.g. from visiting your marketing website to signing up for your product), or how they use your product across multiple platforms.

  2. Capture conversion event

    Required

    Once PostHog is initialized, you should be able to capture events. For your experiment to be meaningful, we need to capture an event that we want to measure, such as a conversion event.

    For this tutorial, let's capture a conversion event on a <button id="cta"> click.

    Web
    <button id="cta">Click me</button>
    <script>
    document.getElementById('cta').addEventListener('click', () => {
    posthog.capture('cta clicked')
    })
    </script>
  3. Validate PostHog events

    Checkpoint
    Confirm events are being sent to PostHog

    Before proceeding, let's make sure events are being captured and sent to PostHog. You should see cta clicked events appear in the Activity feed.

    Check for events in PostHog
  4. Create an experiment

    Required

    Go to the Experiments tab in the PostHog app and click on the New experiment button in the top right.

    Create experiment

    For this tutorial, let's create a new experiment using simplified test values:

    • Name: "Test experiment"
    • Description: "This is a test experiment"
    • Feature flag key: "test-experiment-ff-key"
    • Experiment type: "Feature flag"
    • Variants: "control" and "test"
    • Participant type: "Users"

    Then click Save as draft.

  5. Add primary metric and launch

    Required

    Scroll down to the Primary metrics section and click + Add primary metric.

    Choose Single-use and select Type > Mean.

    Then search for the event cta clicked under Metric and click Save.

    Add primary metric

    By default, experiments are exposed to 100% of users. You can customize release conditions to expose the experiment to a subset of users.

    For this tutorial, we'll ship the experiment to all users and click Launch in the top right.

  6. Call feature flag

    Required

    Use the PostHog SDK to call the experiment flag and change the <button id="cta"> text based on the assigned variant.

    Web
    // Ensure flags are loaded before usage.
    // You only need to call this on the code the first time a user visits.
    // See this doc for more details: /docs/feature-flags/manual#ensuring-flags-are-loaded-before-usage
    posthog.onFeatureFlags(function() {
    const cta = document.getElementById('cta')
    const variant = posthog.getFeatureFlag('test-experiment-ff-key')
    cta.textContent = variant == 'control' ? 'Control CTA' : 'Test CTA'
    })
    // Otherwise, you can just do:
    const cta = document.getElementById('cta')
    const variant = posthog.getFeatureFlag('test-experiment-ff-key')
    cta.textContent = variant == 'control' ? 'Control CTA' : 'Test CTA'
    // You can also test your code by overriding the feature flag:
    // e.g., posthog.featureFlags.overrideFeatureFlags({ flags: {'test-experiment-ff-key': 'test'}})

    Now when a user triggers a cta clicked event, PostHog automatically assigns the user to a variant and records an experiment exposure.

    By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about distribution and release conditions.

  7. Validate feature flag calls

    Checkpoint

    Make sure exposures and feature flag calls are being sent to PostHog. You should see $feature_flag_called events appear in the Activity feed.

    Check for events in PostHog
  8. Evaluate experiment results

    Recommended

    As you capture more cta clicked events, more exposures will populate the primary metrics in your experiment.

    Evaluate experiment metrics

    With enough data, you can analyze the experiment and its variants by:

    • Conversion rates
    • Statistical significance
    • Credible intervals
    • Chance to win %
    • Minimum detectable effect
    • And more

Questions? Ask Max AI.

It's easier than reading through 794 pages of documentation

Community questions

Was this page useful?

Next article

React experiments installation

Once PostHog is initialized, you should be able to capture events. For this tutorial, let's capture a conversion event on a <button id="cta"> click. Use the React hooks to evaluate the experiment flag and render the <button id="cta"> text based on the assigned variant. Now when a user triggers a cta clicked event, PostHog automatically assigns the user to a variant and records an experiment exposure. By default, users are split equally between variants. If you want to assign specific…

Read next article