viaveritas_sdk 0.1.0
viaveritas_sdk: ^0.1.0 copied to clipboard
ViaVeritas for apps in one package; the app's connection (its key, the device's id, storage, a queue of events), feature flags, remote config and A/B tests.
viaveritas_sdk #
ViaVeritas for apps, in one package: one app's connection to ViaVeritas (the app's key, the device's id kept on the device, what describes the device, and one queue of events sent in batches), and on it feature flags, remote config and A/B tests. Features, their rules per environment, experiments and their metrics are made in the ViaVeritas portal; this package reads their values. Pure Dart: it runs in Flutter on every platform, the web included, and on servers.
dart pub add viaveritas_sdk
Or straight from a release's tag in the repository, without pub.dev:
dependencies:
viaveritas_sdk:
git:
url: https://github.com/ViaVeritasApp/sdks
path: flutter/packages/viaveritas_sdk
tag_pattern: v{{version}}
version: ^0.1.0
(tag_pattern needs the app's pubspec to require Dart 3.9 or later, sdk: ^3.9.0; before it, ref: v0.1.0 names
one tag.)
The client #
import 'package:viaveritas_sdk/viaveritas_sdk.dart';
final client = ViaVeritas(
key: 'vvpk_live_…', // the app's key, from the portal (Experiments › Apps › Keys)
attributes: {'app_version': '2.3.0', 'platform': 'android'},
);
await client.ready();
client.setUserId('u-42'); // a signed-in user; null on sign-out
await client.close(); // sends what waits
In an app (client key) #
The API evaluates the rules and answers only this device's values, kept on the device for the next start.
import 'package:viaveritas_sdk/viaveritas_sdk.dart';
final client = ViaVeritas(key: 'vvpk_live_…', attributes: {'app_version': '2.3.0'});
final flags = Experiments(client);
await flags.ready(); // the first read, or 3 s with the values kept
flags.getBool('new-checkout', false); // the fallback when missing or of another type
flags.getString('button-label', 'Buy');
flags.getInt('free-shipping-from', 50);
flags.getJson('onboarding', {'steps': 3});
flags.onChange((keys) => setState(() {}));
flags.track('purchase', amount: 24.9);
flags.identify('u-42');
Reading a value an experiment gave reports the exposure, once per variant. evaluation(key) says how a value was decided
without reporting anything.
In Flutter #
Give the client the app's storage and lifecycle, so the device id and the values outlive a restart and events go out when the app is put away:
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:viaveritas_sdk/viaveritas_sdk.dart';
class PreferencesStorage implements KeyValueStorage {
PreferencesStorage(this.preferences);
final SharedPreferences preferences;
@override
String? get(String key) => preferences.getString(key);
@override
Future<void> set(String key, String value) => preferences.setString(key, value);
@override
Future<void> remove(String key) => preferences.remove(key);
}
class FlutterLifecycle implements Lifecycle {
@override
void Function() onForeground(void Function() listener) => AppLifecycleListener(onResume: listener).dispose;
@override
void Function() onBackground(void Function() listener) => AppLifecycleListener(onHide: listener).dispose;
}
final client = ViaVeritas(
key: 'vvpk_live_…',
storage: PreferencesStorage(await SharedPreferences.getInstance()),
lifecycle: FlutterLifecycle(),
headers: {'x-app-package': 'com.example.shop'}, // Android; `x-ios-bundle-id` on iOS
);
On a server (server key) #
final flags = LocalExperiments(ViaVeritas(key: Platform.environment['VIAVERITAS_SERVER_KEY']!));
await flags.ready();
final device = {'device_id': deviceId, 'user_id': userId, 'country': 'GR'};
if (flags.getBool('new-checkout', false, device)) { /* … */ }
The ruleset is downloaded (and asked for again every 30 s, answered 304 while unchanged) and evaluated here; each device is told to ViaVeritas once a day, which is how it counts toward the month's active devices. Never ship a server key in an app.
MIT. Part of ViaVeritas's SDKs.