How to set up Feature Flags in Javascript
To use feature flags in a web page with the Statsig JavaScript SDK, you'll start by creating a basic "Hello World" HTML page, then integrate Statsig to manage feature flags.
Step 1: Creating a Statsig Project
- Sign Up/Login to Statsig:
- Visit Statsig's website and sign up for an account or log in.
- Create a New Project (if needed):
- In the Statsig console, create a new project for your application.
Step 2: Creating a Basic "Hello World" Web Page
-
Create a New HTML File:
- Name it
index.html
. - Open it in your favorite text editor or IDE.
- Name it
-
Add Basic HTML Content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World Page</title>
</head>
<body>
<h1>Hello, World!</h1>
<div id="featureFlagContent"></div>
<script src="app.js"></script>
</body>
</html>This code creates a simple web page with a heading that says "Hello, World!" and includes a script file
app.js
that we will use to integrate Statsig.
Step 3: Integrating Statsig for Feature Flags
-
Create the JavaScript File (
app.js
):- Create a new file named
app.js
in the same directory as yourindex.html
.
- Create a new file named
-
Add Statsig JavaScript SDK:
- First, you need to include the Statsig SDK. You can add it directly to your
index.html
inside the<head>
section, or you can import it in yourapp.js
if you are using a module bundler. - For simplicity, we'll add it directly to the
index.html
above the<script src="app.js"></script>
:<script src="https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js"></script>
- First, you need to include the Statsig SDK. You can add it directly to your
-
Initialize Statsig and Check a Feature Flag in
app.js
:// Initialize Statsig with your client SDK key
const client = new StatsigClient(
'your_client_sdk_key',
{ userID: "unique_user_id" },
{ environment: { tier: "production" } } // optional
);
client.initializeAsync().then(() => {
// Check if a feature flag is enabled
const isFeatureEnabled = statsig.checkGate('example_feature_flag');
if (isFeatureEnabled) {
document.getElementById('featureFlagContent').innerText = 'Feature Flag is enabled!';
} else {
document.getElementById('featureFlagContent').innerText = 'Feature Flag is disabled.';
}
});- Replace
'your_client_sdk_key'
with your actual Statsig Client SDK Key. - Replace
'example_feature_flag'
with the name of your feature flag. - This script initializes Statsig and checks if a feature flag is enabled. It then updates the content of a div based on the flag's status.
- Replace
-
Open Your HTML File in a Browser:
- Open the
index.html
file in a web browser to see the result.
- Open the
Step 4: Creating Feature Flags in the Statsig Console
-
Navigate to Feature Gates:
- Go to the Feature Gates page in the Statsig console dashboard.
-
Create a New Feature Gate:
- Click on "Create".
- Enter a name for your feature flag (e.g.,
example_feature_flag
). - Optionally, configure targeting rules based on user properties or environments.
- Save your feature flag.
-
Toggle and Configure Your Feature Gate:
- You can now toggle the feature gate on or off and configure detailed targeting rules directly from the Statsig console.
This process allows you to dynamically control features within your web page, facilitating testing and feature rollout without the need to modify and redeploy your code constantly.