Svelte experiments installation
- 1
Install the package
RequiredInstall the PostHog JavaScript library using your package manager:
- 2
Initialize PostHog
RequiredIf you haven't created a root layout already, create a new file called
+layout.jsin yoursrc/routesfolder. Check the environment is the browser, and initialize PostHog if so:src/routes/+layout.jsSvelteKit layoutLearn more about SvelteKit layouts in the official documentation.
- 3
Server-side setup
OptionalInstall
posthog-nodeusing your package manager: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.jsNoteMake 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
RequiredExperiments 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:
- 5
Run your experiment
RequiredOnce 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
RecommendedNow 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 How to create an experiment in PostHog Adding experiment code How to implement experiments for all platforms Statistical significance Understanding when results are meaningful Experiment insights How to analyze your experiment data More tutorials Other real-world examples and use cases