Setup the SDK
1
Install the SDK
To use the SDK in your project, you must add Statsig as a dependency.
- Swift Package Manager
- Cocoapods
In your Xcode, select File > Swift Packages > Add Package Dependency
and enter the URL https://github.com/statsig-io/statsig-kit.git.You can also include it directly in your project’s Package.swift. Find out the latest release version on our GitHub page.
2
Initialize the SDK
Next, initialize the SDK with a client SDK key from the “API Keys” tab on the Statsig console. These keys are safe to embed in a client application.Along with the key, pass in a User Object with the attributes you’d like to target later on in a gate or experiment.The completion block is called after the network request to fetch the latest feature gate and experiment values for your user.
If you try to get any value before the completion block is called, you could get either the cached value from the previous session,
or the default value. If you need the latest value, please wait for the completion block to be called first.
Avoid iOS 18.4 on Simulator: Apple introduced a networking bug in iOS 18.4 that causes requests to fail when running in the Simulator. For more details, see this thread on Apple’s forums.
Use the SDK
Checking a Feature Flag/Gate
Now that your SDK is initialized, let’s check 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.
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 able 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 we recommend the use of layers to enable quicker iterations with parameter reuse.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 for the event, and you can additionally provide some value and/or an object of metadata to be logged together with the event:Parameter Stores
Parameter Stores hold a set of parameters for your mobile app. These parameters can be remapped on-the-fly from a static value to a Statsig entity (Feature Gates, Experiments, and Layers), so you can decouple your code from the configuration in Statsig. Read more about Param Stores here.Statsig User
You need to provide a StatsigUser object to check/get your configurations. You should pass as much information as possible in order to take advantage of advanced gate and config conditions. Most of the time, theuserID field is needed in order to provide a consistent experience for a given
user (see logged-out experiments to understand how to correctly run experiments for logged-out
users).
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.
Once the user logs in or has an update/changed, make sure to call updateUser
with the updated userID and/or any other updated user attributes:
Statsig Options
Use
overrideStableID to set a deterministic device identifier when needed.StableID
Each client SDK has the notion of stableID, a devive-level identifier that is generated the first time the SDK is initialized and is stored locally for all future sessions. Unless storage is wiped (or app deleted), the stableID will not change. This allows us to run device level experiments and experiments when other user identifiable information is unavailable (Logged out users).Manual Exposures
Manual logging is error-prone and can often introduce issues like uneven exposures, which compromise experiment results.
- Feature Gates
- Dynamic Configs
- Experiments
- Layers
Local Overrides
If you want to locally override gates/configs/experiments/layers for testing, Statsig offers convenient methods for a quick local override. Unless you call the remove method, these will be persisted session-to-session on the client’s device. Note that these overrides only apply locally - they don’t impact definitions in the console or elsewhere.Shutting Statsig Down
In order to save users’ data and battery usage, as well as prevent logged events from being dropped, we keep event logs in client cache and flush periodically. Because of this, some events may not have been sent when your app shuts down. To make sure all logged events are properly flushed or saved locally, you should tell Statsig to shutdown when your app is closing:Using multiple instances of the SDK
Up to this point, we’ve used the SDK’s singleton. We also support creating multiples instances of the SDK - theStatsig singleton wraps a single instance of the SDK (typically called a StatsigClient) that you can instantiate.
You must use a different SDK key for each sdk instance you create for this to work. Various functionality of the Statsig client is keyed on the SDK key being used. Using the same key will lead to collisions.
Use a unique SDK key per instance to avoid collisions.
Initialize Response
The SDK provides a method to access the raw values that are used internally for gate, config, and layer value. This can be useful for debugging or for advanced use cases where you need to access the underlying data. For example, you can use these values to bootstrap another SDK, like the javascript SDK when you open an in-app browser. ThegetInitializeResponseJson method returns an ExternalInitializeResponse object that contains:
- A JSON string representation of the initialize response values
- Evaluation details that provide metadata about how the values were obtained (network, cache, etc.)
Listening for changes
In v1.14.0+, you can listen for SDK changes usingStatsigListening.