Setup the SDK
1
Install the SDK
Installation
All core logic are written in rust, and we don’t require rust environment from you, so we pre-built all binaries (attached as assets with each release). Our CMakeLists.txt file handle all the complexity for you.If you encounter any installation or build issue, please reach back to us!2
Initialize the SDK
After installation, you will need to initialize the SDK using a Server Secret Key from the Statsig console.There is also an optional parameter named
options that allows you to pass in a StatsigOptions to customize the SDK.initialize will perform a network request. After initializeBlocking completes, virtually all SDK operations will be synchronous. The SDK will fetch updates from Statsig in the background, independently of your API calls.Working with the SDK
Checking a Feature Flag/Gate
Now that your SDK is initialized, let’s fetch a Feature Gate. Feature Gates can be used to create logic branches in code that can be rolled out to different users from the Statsig Console. Gates are always CLOSED or OFF (thinkreturn false;) by default.
From this point on, all APIs will require you to specify the user (see Statsig user) associated with the request. For example, check a gate for a certain user like this:
Reading a Dynamic Config
Feature Gates can be very useful for simple on/off switches, with optional but advanced user targeting. However, if you want to be send a different set of values (strings, numbers, and etc.) to your clients based on specific user attributes, e.g. country, Dynamic Configs can help you with that. The API is very similar to Feature Gates, but you get an entire json object you can configure on the server and you can fetch typed parameters from it. For example:Getting a Layer/Experiment
Then we have Layers/Experiments, which you can use to run A/B/n experiments. We offer two APIs, but often recommend the use of layers, which make parameters reusable and let you run mutually exclusive experiments.Retrieving Feature Gate Metadata
In certain scenarios, you may need more information about a gate evaluation than just a boolean value. For additional metadata about the evaluation, use the Get Feature Gate API, which returns a FeatureGate object:get_feature_gate() method returns a FeatureGate object that provides:
value: The boolean gate valuerule_id: The ID of the rule that served this gateid_type: The type of the evaluationevaluation_details: Additional metadata about the evaluation
Logging an Event
Now that you have a Feature Gate or an Experiment set up, you may want to track some custom events and see how your new features or different experiment groups affect these events. This is super easy with Statsig—simply call the Log Event API and specify the user and event name to log; you additionally provide some value and/or an object of metadata to be logged together with the event:log_event method supports multiple overloads:
log_event(user, event_name)log_event(user, event_name, string_value)log_event(user, event_name, string_value, metadata)
Statsig User
TheStatsigUser object represents a user in Statsig. You must provide a userID or at least one of the customIDs to identify the user.
When calling APIs that require a user, you should pass as much information as possible in order to take advantage of advanced gate and config conditions (like country or OS/browser level checks), and correctly measure impact of your experiments on your metrics/events. At least one ID (userID or customID) is required because it’s needed to provide a consistent experience for a given user (click here)
Besides userID, we also have email, ip, userAgent, country, locale and appVersion as top-level fields on StatsigUser. In addition, you can pass any key-value pairs in an object/dictionary to the custom field and be able to create targeting based on them.
Private Attributes
Private attributes are user attributes that are used for evaluation but are not forwarded to any integrations. They are useful for PII or sensitive data that you don’t want to send to third-party services.Statsig Options
You can pass in an optional parameteroptions in addition to sdkKey during initialization to customize the Statsig client. Here are the available options that you can configure.
Shutting Statsig Down
Because we batch and periodically flush events, some events may not have been sent when your app/server shuts down. To make sure all logged events are properly flushed, you should callshutdown() before your app/server shuts down:
shutdown() method will:
- Flush any pending events to Statsig servers
- Gracefully shutdown the SDK, cleaning up resources
- Wait for all operations to complete
shutdown() before your application exits to ensure all events are sent.
Reference
API Methods
check_gate(user: StatsigUser, gate_name: str, options: Optional[FeatureGateEvaluationOptions] = None) -> boolget_dynamic_config(user: StatsigUser, config_name: str, options: Optional[DynamicConfigEvaluationOptions] = None) -> DynamicConfigget_experiment(user: StatsigUser, experiment_name: str, options: Optional[ExperimentEvaluationOptions] = None) -> DynamicConfigget_layer(user: StatsigUser, layer_name: str, options: Optional[LayerEvaluationOptions] = None) -> Layerget_feature_gate(user: StatsigUser, gate_name: str, options: Optional[FeatureGateEvaluationOptions] = None) -> FeatureGatelog_event(user: StatsigUser, event_name: str, value: Optional[Union[str, float]] = None, metadata: Optional[Dict[str, str]] = None) -> Noneshutdown() -> AsyncResult[None]
Fields Needed Methods
The following methods return information about which user fields are needed for evaluation:get_gate_fields_needed(gate_name: str) -> List[str]get_dynamic_config_fields_needed(config_name: str) -> List[str]get_experiment_fields_needed(experiment_name: str) -> List[str]get_layer_fields_needed(layer_name: str) -> List[str]