Developer Guides
How to run an AB Test in Node.js
To run a simple A/B experiment in a webpage with Node.js using the Statsig SDK, you’ll go through a few essential steps. This will involve setting up a simple Node.js server, integrating with the Statsig SDK for A/B testing, and configuring your experiment in the Statsig console.
Step 1: Set Up a Basic Node.js Server
- Initialize Your Node.js Project:
- Create a new directory for your project, navigate into it, and run
npm init -y
to create apackage.json
file.
- Create a new directory for your project, navigate into it, and run
- Install Necessary Packages:
- Install Express for your web server and the Statsig SDK for Node.js by running:
- Create Your Server:
- Create an
index.js
file and set up a basic Express server. Here’s a simple example:
- Verify the app runs by issuing
node index.js
command
- Create an
Step 2: Integrate Statsig for A/B Testing
- Initialize Statsig:
- At the top of your
index.js
, require the Statsig SDK and initialize it with your server-side SDK key. - Modify your route to serve different content based on the experiment’s variant.
- Ensure you replace
'your-server-sdk-key'
with your actual Statsig server-side SDK key and'your_experiment_key'
with the name of your experiment.
- Run the app by issuing
node index.js
command
- At the top of your
Step 3: Create Your A/B Test in Statsig Console
- Log into Statsig Console:
- Go to the Statsig Console and log in.
- Create a New Experiment:
- Navigate to the “Experiments” section and click on “Create New”.
- Fill in the details for your experiment, including giving it a unique key (e.g.,
'your_experiment_key'
).
- Configure Experiment Variants:
- Add variants for your experiment (e.g.,
control
andtest
). - Within each variant’s configuration, add a parameter named
textToShow
and assign the text you wish to display for each variant.
- Add variants for your experiment (e.g.,
- Launch the Experiment:
- After setting up your variants and parameters, save and start the experiment.
By following these steps, you’ve created a simple Node.js application that runs an A/B test to serve different text content based on the variant assigned to a user in the Statsig experiment. This setup enables you to conduct A/B testing efficiently, allowing you to make data-driven decisions based on user interaction with your application.