Evolv AI’s SDKs act as a layer to simplify interaction with our APIs.
On this page:
- Overview
- Installation
- Initialization
- Subscribing to the value of a key
- Setting the Context
- Firing Events
Overview
The main responsibilities of the SDKs are:
- Evaluates the predicates of dynamic variables and variants
- Requests all required data
- Maintains context state
- Sends important events to the Evolv AI server
- Confirmations
- Contaminations
- Custom events
Installation
Find the NPM package here
Find the Android project here
Find the Swift project here
Initialization
An instance must be instantiated with the user’s ID. Evolv ensures you receive consistent allocations for the same user ID.
import EvolvClient, { MiniPromise } from '@evolv/client';
let options = {
environment: 'YOUR_ENVIRONMENT_ID',
autoConfirm: true,
analytics: true,
clientName: 'OPTIONAL_CLIENT_IDENTIFIER'
};
const client = new EvolvClient(options);
client.initialize('YOUR_USER_ID');
HttpClient httpClient = new OkHttpClient(TimeUnit.MILLISECONDS, 7000);
EvolvConfig config = EvolvConfig.builder('YOUR_ENVIRONMENT_ID', httpClient).build();
client = EvolvClientFactory.init(config, new EvolvParticipant("YOUR_USER_ID"));
import EvolvSwiftSDK
let options = EvolvClientOptions(participantID: "YOUR_USER_ID", environmentId: "YOUR_ENVIRONMENT_ID", autoConfirm: true, analytics: true)
EvolvClientFactory(with: options).initializeClient()
.sink(receiveCompletion: { publisherResult in
switch publisherResult {
case .finished:
print("Evolv client initialization is finished successfully.")
case .failure(let error):
print("Evolv client initialization is finished with error: \(error.localizedDescription)")
}
}, receiveValue: { evolvClient in
self.client = evolvClient
})
.store(in: &cancellables)
Subscribing to the value of a key
Once the client is initialized, we want to subscribe to the value of the keys. Any time these values change, the attached function will fire. This will update when the key is active. A default value should be used in case of no connectivity.
client.get('VARIABLE_KEY').then((value) => {
// EXECUTE CODE
// Example 1: The value is a color or text value for a button
// Example 2: The value defines which option in a switch statement is executed
});
client.subscribeGet("VARIABLE_KEY", "Default text", (EvolvAction<JsonElement>) value ->
runOnUiThread(() -> {
TextView textView = findViewById(R.id.homeButton);
textView.setText(value.getAsString());
}));
client
.get(subscriptionDecodableOnValueForKey: "VARIABLE_KEY", type: String.self)
.compactMap { $0 ?? "Default text" }
.sink { [weak self] label in
self?.dynamicLabel.text = label
}.store(in: &cancellables)
Setting the Context
Variable keys are only active when the context value matches the predicate conditions. To activate the keys, and enter the project the context must be set. The context values can be local or remote. Local context values will not be sent back to Evolv.
client.context.set('pageType', 'CategoryPage', false /*Not local context*/)
evolvContext = ((EvolvClientImpl) client).getEvolvContext();
evolvContext.set("pageType", "CategoryPage", false);
client.set(key: "pageType", value: "CategoryPage", local: false)
Firing Events
Confirming will fire a confirmation to include this user in the Project for combinations with an active entry point (note this is not necessary with a configuration of autoConfirm. Some SDKs will always autoconfirm). Contaminating will fire a contamination event to exclude this user in the Project for combinations with active keys. Custom events are required to let Evolv know when important events have occurred - these are used as Metrics to optimize against.
client.confirm(); // Not necessary when autoconfirm is on.
client.contaminate(); // Passing in true will contaminate the session for all projects
client.emit('checkout');
client.confirm()
client.contaminate()
JSONObject properties = new JSONObject();
evolvClient.emit("checkout", properties, false)
// Contaminate a session
client.contaminate()
// Emit an event
evolvClient.emit(eventType: "checkout", flush: false)