--- title: How to set up A/B tests in Node.js (Express) date: 2024-02-02 author: - lior-neu-ner tags: - experimentation --- import { ProductScreenshot } from 'components/ProductScreenshot' export const EventsInPostHogLight = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/node-express-ab-tests/events-light.png" export const EventsInPostHogDark = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/node-express-ab-tests/events-dark.png" export const ResultsLight = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/node-express-ab-tests/results-light.png" export const ResultsDark = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/node-express-ab-tests/results-dark.png" A/B tests help you improve your Node app by enabling you to compare the impact of changes on key metrics. To show you how to set one up, we create a basic Node app with [Express](https://expressjs.com/), add PostHog, create an A/B test, and implement the code for it. ## 1. Create a basic Express app First, ensure [Node.js is installed](https://nodejs.dev/en/learn/how-to-install-nodejs/). Then, open your command line, create a new folder for our tutorial, install Express, and create a new file `server.js`: ```bash mkdir express-ab-test cd express-ab-test npm i express touch server.js ``` Next, add the following code to `server.js`: ```js file=server.js const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { const paragraphText = 'Placeholder text' const htmlContent = `
${paragraphText}
`; res.send(htmlContent); }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) }) ``` Run `node server.js` and navigate to `http://localhost:3000` to see your app in action.  ## 2. Add PostHog to your app With our app set up, it’s time to install and set up PostHog. If you don't have a PostHog instance, you can [sign up for free](https://us.posthog.com/signup). To start, install [PostHog’s Node library](/docs/libraries/node): ```bash npm install posthog-node ``` Next, we initialize our PostHog client in `server.js` using our project API key and instance address. You can find these in [your project settings](https://us.posthog.com/settings/project). ```js file=server.js const express = require('express'); const app = express(); const port = 3000; const { PostHog } = require('posthog-node'); const posthogClient = new PostHog('${paragraphText}
`; res.send(htmlContent); }) // rest of your code ``` When you restart your app and refresh the page, you should see the text updated to either `Control variant!` or `Test variant!`. > **💡 Setting the correct `distinctId`:** > > You may notice that we set `distinctId = 'placeholder-user-id'` in our flag call above. In production apps, to ensure you fetch the correct flag value for your user, `distinctId` should be set to their unique ID. > > For logged-in users, you typically use their email or user ID as their `distinctId`. For logged-out users, assuming they made their request from a browser, you can use values from their request cookies. See an example of this in the [server side section of our Remix tutorial](/tutorials/remix-ab-tests#setting-the-correct-distinctid) ## 6. Include the feature flag when capturing your event To ensure our goal metric is correctly calculated for each experiment variant, we need to include our feature flag information when capturing our `endpoint_called` event. To do this, we add the [`$feature/my-cool-experiment`](/docs/libraries/node#step-2-include-feature-flag-information-when-capturing-events) key to our event properties: ```js file=server.js app.get('/', async (req, res) => { // rest of your code // update your posthogClient.capture call to include the feature flag information posthogClient.capture({ distinctId, event: 'endpoint_called', properties: { '$feature/my-cool-experiment': enabledVariant } }); // rest of your code }); ``` Now PostHog is able to calculate our goal metric for our experiment results.