36
loading...
This website collects cookies to deliver better user experience
yarn create next-app
cd my-app
yarn dev -p 4000
git clone https://github.com/growthbook/growthbook.git
cd growthbook
docker-compose up -d
Features
in the left nav instead.Save Attributes
.yarn add @growthbook/growthbook-react
pages/_app.js
with the following contents:import '../styles/globals.css'
import {
GrowthBook,
GrowthBookProvider
} from "@growthbook/growthbook-react";
import { useEffect } from "react";
const FEATURES_ENDPOINT =
"http://localhost:3100/api/features/key_abc123";
// Create a GrowthBook instance
const growthbook = new GrowthBook({
trackingCallback: (experiment, result) => {
console.log("Viewed Experiment", experiment, result);
}
})
export default function MyApp({
Component,
pageProps,
router
}) {
// Refresh features and targeting attributes on navigation
useEffect(() => {
fetch(FEATURES_ENDPOINT)
.then((res) => res.json())
.then((json) => {
growthbook.setFeatures(json.features);
});
growthbook.setAttributes({
"id": "123",
"loggedIn": true,
"deviceId": "abcdef123456",
"employee": true,
"company": "acme",
"country": "US",
"browser": navigator.userAgent,
"url": router.pathname
})
}, [router.pathname])
return (
<GrowthBookProvider growthbook={growthbook}>
<Component {...pageProps} />
</GrowthBookProvider>
)
}
FEATURES_ENDPOINT
constant with the one you see in your instructions modal in GrowthBook.welcome-message
) is what we will reference when using the GrowthBook SDK.pages/index.js
and conditionally render a welcome message based on the state of the feature:import { IfFeatureEnabled } from "@growthbook/growthbook-react";
<IfFeatureEnabled feature="welcome-message">
<p>I hope you enjoy this site and have a great day!</p>
</IfFeatureEnabled>
pages/_app.js
, we designated ourselves as an internal employee. Let's use this attribute to turn on the feature for the whole internal team:employee
to false in pages/_app.js
, you should see the welcome message disappear.pages/_app.js
, make sure employee
is set to false
. That will ensure you skip the first rule we made and fall into the second rollout rule.id
) and the feature key (welcome-message
) and coverts it to a float between 0 and 1. If that float is less than or equal to the rollout percent, the user is included. This ensures a consistent UX and prevents one user from constantly switching between ON and OFF as they navigate your app.pages/_app.js
to a few different random strings and see what happens. You should notice about half of the time the welcome message shows up and half of the time it doesn't. The rollout is working!<IfFeatureEnabled>
component