-
-
Save Auz/5e34519a71464c36cd0da5f42a1243a1 to your computer and use it in GitHub Desktop.
Example of GrowthBook in JS routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GrowthBook } from "@growthbook/growthbook"; | |
var express = require('express'); | |
var router = express.Router(); | |
... | |
// grab the Mixpanel factory | |
var Mixpanel = require('mixpanel'); | |
// create an instance of the mixpanel client | |
var mixpanel = Mixpanel.init('<YOUR_TOKEN>'); | |
... | |
... | |
// Home page route. | |
router.get('/', function (req, res) { | |
// Define the experimental context | |
const growthbook = new GrowthBook({ | |
// The attributes used to assign variations | |
user: { id: req.userId }, // could use sessionId here if they're no userid. | |
// Called when a user is put into an experiment | |
trackingCallback: (experiment, result) => { | |
mixpanel.track("Experiment Viewed", { | |
distinct_id: req.sessionId, | |
userId: req?.userId || null, | |
sessionId: req.sessionId, | |
experimentId: experiment.key, | |
variationId: result.variationId, | |
}); | |
}, | |
}); | |
// define the experiment: | |
const { value } = growthbook.run({ | |
key: "my-experiment", | |
variations: ["Control", "New Page"], | |
}); | |
if(value === 1) { | |
res.send('New Page'); | |
} else { | |
res.send('Control'); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment