Source code: statsig-io/js-client-monorepo
Set Up the SDK
1
Install the SDK
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 Statsig Angular bindings package provides a
StatsigService
that can be injected into your components. The way you provide and inject this service can vary depending on how you structure your app.using app config
using app module
Use the SDK
Once you have provided the statsig config token, you can now inject the service into a component or another service and use it.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:Updating user properties (e.g., Login)
Sometimes you’ll need to update user properties, say when the user logs in and a userID is assigned, or a set of new properties have been identified. This would require Statsig to go fetch new values for all the gates, experiments and config evaluations. This is achieved by the callingupdateUserAsync
from the service:
Loading State
Dependent on your setup, you may want to wait for the latest values before checking a gate or experiment. You can use theisLoading
observable to determine if the SDK is still loading and display a loading state.
Angular Directives
Statsig Module
To use the directives, you need to import theStatsigModule
in your Angular module.
Check gate directive
The stgCheckGate directive allows you to conditionally display content in your Angular templates based on the evaluation of a Feature Gate from Statsig. It listens for updates to the feature gate’s value and dynamically adds or removes content based on whether the gate is enabled or not.Session Replay
By including the@statsig/session-replay
package in your project, you can automatically capture and log user sessions as videos.
This feature is useful for debugging and understanding user behavior. Read more about Session Replay.
Web Analytics / Auto Capture
By including the@statsig/web-analytics
package in your project, you can automatically capture common web events like clicks and page views.
Read more about Web Analytics.