Server-side feature gating
Step 1: Get the Statsig server secret key
To get the Statsig server secret key,
-
Log into the Statsig console at https://console.statsig.com
-
Click on the Project Settings gear icon next to your account settings at the top right corner of the page as shown below
-
Click on the API Keys tab
-
Copy the active Server Secret Key
Step 2: Install the SDK
- Node
- Java
- Ruby
- Go
- .NET
If you are using Bundler, add the Statsig gem to your Gemfile from command line:
bundle add statsig --version ">= 1.4.0"
Or directly include it in your Gemfile and do a bundle install:
gem "statsig", ">= 1.4.0"
bundle install
You can install the statsig SDK via npm or yarn:
npm install statsig-node //using npm
yarn add statsig-node //using yarn
Install the Statsig Server SDK using jitpack.
In your go.mod, add a dependency on the most recent version of the SDK. See the Releases tab in github for all versions.
require (
statsig "github.com/statsig-io/go-sdk"
github.com/statsig-io/go-sdk v1.10.0
)
The Statsig SDK package is hosted on Nuget. You can either install it from your Visual Studio's Nuget package manager, or through .NET CLI as shown below:
dotnet add package Statsig --version 1.1.0
Step 3: Initialize the SDK
To initialize the SDK, copy the following in your server side application code:
- Node
- Java
- Ruby
- Go
- .NET
require 'statsig'
Statsig.initialize('SERVER_SECRET_KEY')
// Or, if you want to initialize with certain options
options = StatsigOptions.new({'tier' => 'staging'})
Statsig.initialize('SERVER_SECRET_KEY', options)
const statsig = require('statsig-node');
await statsig.initialize(
'SERVER_SECRET_KEY',
{ environment: { tier: 'staging' } }, // optional, pass options here if needed
});
Future initFuture = StatsigServer.initializeAsync("SERVER_SECRET_KEY");
initFuture.get();
import (
statsig "github.com/statsig-io/go-sdk"
)
statsig.Initialize("SERVER_SECRET_KEY")
// Or if you want to initialize with certain options
statsig.InitializeWithOptions("SERVER_SECRET_KEY", &statsig.Options{Environment: statsig.Environment{Tier: "staging"}})
using Statsig;
using Statsig.Server;
await StatsigServer.Initialize(
"SERVER_SECRET_KEY",
new StatsigOptions(new StatsigEnvironment(EnvironmentTier.Development)) // optional, use when needed to customize certain behaviors
);
Step 4: Check the feature gate
To implement a feature gate, you must include a feature gate check in your application code.
A feature gate check returns a Boolean value. A feature gate is closed/off and a gate check returns false by default. After you have created a rule to target a set of users, the feature gate check returns true when you perform a check for an eligible user. You can use this return value to expose the eligible user to the feature as shown below.
- Node
- Java
- Ruby
- Go
- .NET
user = StatsigUser.new({'userID' => 'LOGGED_IN_USER_ID'})
if Statsig.check_gate(user, 'FEATURE_GATE_NAME')
// Gate is on, enable new feature
else
// Gate is off
end
const user = {
userID: 'LOGGED_IN_USER_ID',
email: 'LOGGED_IN_USER_EMAIL',
...
};
const showNewDesign = Statsig.checkGateSync(user, 'FEATURE_GATE_NAME');
if (showNewDesign) {
// show new design here
} else {
// show old design here
}
StatsigUser user = new StatsigUser();
user.email = "LOGGED_IN_USER_EMAIL"
Future<Boolean> featureOn = StatsigServer.checkGateAsync(user, "FEATURE_GATE_NAME");
Boolean isFeatureOn = featureOn.get()
user := types.StatsigUser{UserID: "LOGGED_IN_USER_ID"}
feature := statsig.CheckGate(user, "FEATURE_GATE_NAME")
if feature {
// Gate is on, enable new feature
} else {
// Gate is off
}
var user = new StatsigUser { UserID = "LOGGED_IN_USER_ID", Email = "LOGGED_IN_USER_EMAIL" };
var useNewFeature = await StatsigServer.CheckGate(user, "FEATURE_GATE_NAME");
if (useNewFeature)
{
// Gate is on, enable new feature
}
else
{
// Gate is off
}
Step 5 (Optional): Log an event
You can optionally log an event to capture any metrics that show the impact of your feature.
- Node
- Java
- Ruby
- Go
- .NET
Statsig.log_event(user, 'EVENT_NAME', 'EVENT_VALUE', { 'price' => '9.99', 'item_name' => 'diet_coke_48_pack' })
statsig.logEvent(user, "EVENT_NAME", "EVENT_VALUE", {
price: "9.99",
item_name: "diet_coke_48_pack",
});
StatsigServer.logEvent(null, "EVENT_NAME", "EVENT_VALUE", mapOf("test" to "test2"))
statsig.LogEvent(types.StatsigEvent{
User: user,
EventName: "EVENT_NAME",
Value: "EVENT_VALUE",
Metadata: map[string]string{"price": "9.99","item_name": "diet_coke_48_pack"},
})
StatsigServer.LogEvent(user, "EVENT_NAME", "EVENT_VALUE", new Dictionary<string, string>() { { "price", "9.99" }, { "item_name", "diet_coke_48_pack" } });