Angular utilities
Integrate feature management into your Angular applications using the Harness FME Angular SDK and utility components.
This guide provides detailed information about our Angular utilities built on top of our JavaScript Browser SDK. An Angular Service and an Angular Guard are provided in this utilities in ESM2020, FESM2020 and FESM2015 module formats. The service provides an easy way to interact with the underneath SDK and work towards any use cases through simplified methods. You can also import from this utilities an Angular Guard to wait for SDK to be ready.
All of our SDKs are open source. Go to our Angular Utilities GitHub repository to see the source code.
Before you begin
These utilities guarantee support with Angular v18.0.0 or later.
Initialization
Set up FME in your code base with the following two steps:
1. Import the utilities into your project
Import the utilities into your project using the following NPM command:
npm install --save @splitsoftware/splitio-angular@4.0.02. Instantiate the service
import { SplitService } from '@splitsoftware/splitio-angular';
const sdkReady = false;
// Inject service
constructor(private splitService: SplitService){}
// Instantiate the Service
public initPlugin() {
// Create the config for the plugin.
const sdkConfig = {
core: {
authorizationKey: 'YOUR_SDK_KEY',
key: 'key'
}
};
// init method returns an observable for sdk readiness
this.splitService.init(sdkConfig).subscribe(() => {
this.sdkReady = true
});
}We recommend instantiating the service once as a singleton and reusing it throughout your application.
Configure the service with the SDK key for the FME environment that you would like to access. In legacy Split (app.split.io) the SDK key is found on your Admin settings page, in the API keys section. Select a client-side SDK API key. This is a special type of API token with limited privileges for use in browsers or mobile clients. See API keys to learn more.
Using the service
Basic use
When the SDK is instantiated, it starts background tasks to update an in-memory cache with small amounts of data fetched from Harness servers. This process can take up to a few hundred milliseconds depending on the size of data. If the SDK is asked to evaluate which treatment to show to a customer for a specific feature flag while its in this intermediate state, it may not have the data necessary to run the evaluation. In this case, the SDK does not fail, rather, it returns the control treatment.
To make sure the SDK is properly loaded before asking it for a treatment, block until the SDK is ready, as shown below. You can subscribe to splitService.sdkReady$ observable provided by splitService before asking for an evaluation.
After the observable calls back, you can use the getTreatment method to return the proper treatment based on the FEATURE_FLAG_NAME and the key variable you passed when instantiating the SDK.
Then use an if-else-if block as shown below and insert the code for the different treatments that you defined in Harness FME. Remember the final else branch in your code to handle the client returning control.
Attribute syntax
To target based on custom attributes, the splitService's getTreatment method needs to be passed an attribute map at runtime.
In the example below, we are rolling out a feature to users. The provided attributes plan_type, registered_date, permissions, paying_customer, and deal_size are passed to the getTreatment call. These attributes are compared and evaluated against the attributes used in the rollout plan as defined in Harness FME to decide whether to show the on or off treatment to this account.
The getTreatment method supports five types of attributes: strings, numbers, dates, booleans, and sets. The proper data type and syntax for each are:
Strings: Use type String.
Numbers: Use type Number.
Dates: Use type Date and express the value in
milliseconds since epoch. Note: Milliseconds since epoch is expressed in UTC. If your date or date-time combination is in a different timezone, first convert it to UTC, then transform it to milliseconds since epoch.Booleans: Use type Boolean.
Sets: Use type Array.
You can pass your attributes in exactly this way to the splitService.getTreatments method.
Multiple evaluations at once
In some instances, you may want to evaluate treatments for multiple feature flags at once. Use the different variations of getTreatments from the SDK factory client to do this.
getTreatments: Pass a list of the feature flag names you want treatments for.getTreatmentsByFlagSet: Evaluate all flags that are part of the provided set name and are cached on the SDK instance.getTreatmentsByFlagSets: Evaluates all flags that are part of the provided set names and are cached on the SDK instance.
Get treatments with configurations
To leverage dynamic configurations with your treatments, use the getTreatmentWithConfig method.
This method returns an object with the structure below:
From the object structure, the config is a stringified version of the configuration JSON defined in Harness FME. If no configuration is defined for a treatment, the SDK returns null for the config parameter. This method takes the same set of arguments as the standard getTreatment method. Refer to the examples below for proper usage:
If you need to get multiple evaluations at once, you can also use the getTreatmentsWithConfig methods. These methods take the exact same arguments as the getTreatments methods but return a mapping of feature flag names to TreatmentResults objects instead of strings. Example usage below.
Append properties to impressions
Impressions are generated by the SDK each time a getTreatment method is called. These impressions are periodically sent back to Harness servers for feature monitoring and experimentation.
You can append properties to an impression by passing an object of key-value pairs to the getTreatment method. These properties are then included in the impression sent by the SDK and can provide useful context to the impression data.
Three types of properties are supported: strings, numbers, and booleans.
Shutdown
Call the splitService.destroy() method before letting a process using the SDK exit, as this method gracefully shuts down the SDK by stopping all background threads, clearing caches, closing connections, and flushing the remaining unpublished impressions.
After destroy() is called and finishes, any subsequent invocations to getTreatment/getTreatments or manager methods result in control or empty list, respectively.
IMPORTANT!
A call to the destroy() method also destroys the splitService object. When creating new client instance, first initialize the service again.
Track
Use the track method to record any actions your customers perform. Each action is known as an event and corresponds to an event type. Calling track through one of our SDKs or via the API is the first step to getting experimentation data into Harness FME and allows you to measure the impact of your features on your users' actions and metrics. Learn more about using track events.
In the examples below, you can see that the .track() method can take up to four arguments. The proper data type and syntax for each are:
TRAFFIC_TYPE: The traffic type of the key in the track call. The expected data type is String. You can only pass values that match the names of traffic types that you have defined Harness FME.
EVENT_TYPE: The event type that this event should correspond to. The expected data type is String. Full requirements on this argument are:
Contains 63 characters or fewer.
Starts with a letter or number.
Contains only letters, numbers, hyphen, underscore, or period.
This is the regular expression we use to validate the value:
[a-zA-Z0-9][-_\.a-zA-Z0-9]{0,62}
VALUE: (Optional) The value is used to create the metric. This field can be sent in as null or 0 if you intend to purely use the count function when creating a metric. The expected data type is Integer or Float.
PROPERTIES: (Optional) An object of key value pairs that can be used to filter your metrics. Learn more about event property capture in the Events guide. FME currently supports three types of properties: strings, numbers, and booleans.
The track method returns a boolean value of true or false to indicate whether or not the splitService was able to successfully queue the event to be sent back to Harness servers on the next event post. The service returns false if the current queue size is equal to the config set by eventsQueueSize or if an incorrect input to the track method has been provided.
In the case that a bad input is provided, you can read more about our SDK's expected behavior.
Configuration
The SDK has a number of knobs for configuring performance. Each knob is tuned to a reasonable default. However, you can override the value while providing the config to the splitService.init method as shown in the Initialization section of this doc. To learn about the available configuration options, go to the JavaScript SDK Configuration section.
Manager
To get a list of features available to the SDK factory client, you can use the methods available on splitService as shown below:
The SplitView object referenced above has the following structure:
Listener
FME SDKs send impression data back to Harness servers periodically and as a result of evaluating feature flags. To additionally send this information to a location of your choice, define and attach an impression listener. For this purpose, the SDK's configurations have a parameter called impressionListener where an implementation of ImpressionListener could be added. This implementation must define the logImpression method and it receives data in the following schema.
Name
Type
Description
impression
Object
Impression object that has the feature, key, treatment, label, etc.
attributes
Object
A map of attributes passed to getTreatment/getTreatments (if any).
sdkLanguageVersion
String
The version of the SDK. In this case the language is angular plus the version currently running.
Implement custom impression listener
The following is an example of how to implement a custom impression listener:
An impression listener is called asynchronously from the corresponding evaluation, but is almost immediate.
Even though the SDK does not fail if there is an exception in the listener, do not block the call stack.
Logging
To enable SDK logging in the browser, open your DevTools console and type the following:
Reload the browser to start seeing the logs.
You can also enable the logging via SDK settings and programmatically by calling the Logger API.
Advanced use cases
This section describes advanced use cases and features provided by the SDK.
Instantiate multiple SDK clients
FME supports the ability to release based on multiple traffic types. For example, with traffic types, you can release to users in one feature flag and accounts in another. If you are unfamiliar with using multiple traffic types, refer to the Traffic type guide for more information.
Each SDK factory client is tied to one specific customer ID at a time, so if you need to roll out features by different traffic types, instantiate multiple SDK clients, one for each traffic type. For example, you may want to roll out the feature user-poll by users and the feature account-permissioning by accounts.
You can do this with the example below:
In every getTreatment and track method, you can add an user id as first parameter to define which client to use. If a user id parameter is not present, splitService uses the one in SDK config.
Subscribe to events
You can subscribe to four different observables of the splitService.
sdkReadyFromCache$. This event fires once the SDK is ready to evaluate treatments using a version of your rollout plan cached in localStorage from a previous session (which might be stale). If there is data in localStorage, this event fires almost immediately, since access to localStorage is fast; otherwise, it doesn't fire.sdkReady$. This event fires once the SDK is ready to evaluate treatments using the most up-to-date version of your rollout plan, downloaded from Harness servers.sdkReadyTimedOut$. This event fires if there is no cached version of your rollout plan cached in localStorage, and the SDK could not download the data from Harness servers within the time specified by thestartup.readyTimeoutconfiguration parameter. This event does not indicate that the SDK initialization was interrupted. The SDK continues downloading the rollout plan and fires thesdkReady$event when finished. This delayedsdkReady$event may happen with slow connections or large rollout plans with many feature flags, segments, or dynamic configurations.sdkUpdate$. This event fires whenever your rollout plan is changed. Listen for this event to refresh your app whenever a feature flag or segment is changed in Harness FME.
The syntax to subscribe for each Observable is shown below:
Angular Guard
These utilities provide an Angular Guard that allows you to avoid loading an angular component if the SDK is not ready.
Example apps
The following are example applications detailing how to configure and instantiate the FME Angular utilities on commonly used platforms.
Last updated
Was this helpful?