-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoptimizely_flutter_sdk.dart
More file actions
343 lines (316 loc) · 17.1 KB
/
optimizely_flutter_sdk.dart
File metadata and controls
343 lines (316 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/// **************************************************************************
/// Copyright 2022-2023, Optimizely, Inc. and contributors *
/// *
/// Licensed under the Apache License, Version 2.0 (the "License"); *
/// you may not use this file except in compliance with the License. *
/// You may obtain a copy of the License at *
/// *
/// http://www.apache.org/licenses/LICENSE-2.0 *
/// *
/// Unless required by applicable law or agreed to in writing, software *
/// distributed under the License is distributed on an "AS IS" BASIS, *
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
/// See the License for the specific language governing permissions and *
/// limitations under the License. *
///**************************************************************************/
library optimizely_flutter_sdk;
import 'dart:async';
import 'package:optimizely_flutter_sdk/src/data_objects/activate_response.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/base_response.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/datafile_options.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/event_options.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/get_vuid_response.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/sdk_settings.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/get_variation_response.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/optimizely_config_response.dart';
import 'package:optimizely_flutter_sdk/src/optimizely_client_wrapper.dart';
import 'package:optimizely_flutter_sdk/src/user_context/optimizely_user_context.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/log_level.dart';
import 'package:optimizely_flutter_sdk/src/logger/flutter_logger.dart';
import 'package:optimizely_flutter_sdk/src/logger/logger_bridge.dart';
export 'package:optimizely_flutter_sdk/src/optimizely_client_wrapper.dart'
show ClientPlatform, ListenerType;
export 'package:optimizely_flutter_sdk/src/user_context/optimizely_forced_decision.dart'
show OptimizelyForcedDecision;
export 'package:optimizely_flutter_sdk/src/user_context/optimizely_decision_context.dart'
show OptimizelyDecisionContext;
export 'package:optimizely_flutter_sdk/src/user_context/optimizely_user_context.dart'
show OptimizelyUserContext, OptimizelyDecideOption, OptimizelySegmentOption;
export 'package:optimizely_flutter_sdk/src/data_objects/decide_response.dart'
show Decision;
export 'package:optimizely_flutter_sdk/src/data_objects/track_listener_response.dart'
show TrackListenerResponse;
export 'package:optimizely_flutter_sdk/src/data_objects/decision_listener_response.dart'
show DecisionListenerResponse;
export 'package:optimizely_flutter_sdk/src/data_objects/logevent_listener_response.dart'
show LogEventListenerResponse;
export 'package:optimizely_flutter_sdk/src/data_objects/event_options.dart'
show EventOptions;
export 'package:optimizely_flutter_sdk/src/data_objects/sdk_settings.dart'
show SDKSettings;
export 'package:optimizely_flutter_sdk/src/data_objects/datafile_options.dart'
show DatafileHostOptions;
export 'package:optimizely_flutter_sdk/src/data_objects/log_level.dart'
show OptimizelyLogLevel;
export 'package:optimizely_flutter_sdk/src/logger/flutter_logger.dart'
show OptimizelyLogger;
/// The main client class for the Optimizely Flutter SDK.
///
/// To use, create an instance of OptimizelyFlutterSdk class with a valid sdkKey, datafilePeriodicDownloadInterval (optional), eventOptions (optional) , defaultDecideOptions (optional) and
/// call initializeClient method.
/// If successful, call createUserContext to setup user context.
/// Once done, all API's should be available.
class OptimizelyFlutterSdk {
final String _sdkKey;
final EventOptions _eventOptions;
final int _datafilePeriodicDownloadInterval;
final Map<ClientPlatform, DatafileHostOptions> _datafileHostOptions;
final Set<OptimizelyDecideOption> _defaultDecideOptions;
final OptimizelyLogLevel _defaultLogLevel;
final SDKSettings _sdkSettings;
static OptimizelyLogger? _customLogger;
/// Set a custom logger for the SDK
static void setLogger(OptimizelyLogger logger) {
_customLogger = logger;
LoggerBridge.initialize(logger);
}
/// Get the current logger
static OptimizelyLogger? get logger {
return _customLogger;
}
OptimizelyFlutterSdk(this._sdkKey,
{EventOptions eventOptions = const EventOptions(),
int datafilePeriodicDownloadInterval = 10 * 60,
Map<ClientPlatform, DatafileHostOptions> datafileHostOptions = const {},
Set<OptimizelyDecideOption> defaultDecideOptions = const {},
OptimizelyLogLevel defaultLogLevel = OptimizelyLogLevel.info,
SDKSettings sdkSettings = const SDKSettings(),
OptimizelyLogger? logger})
: _eventOptions = eventOptions,
_datafilePeriodicDownloadInterval = datafilePeriodicDownloadInterval,
_datafileHostOptions = datafileHostOptions,
_defaultDecideOptions = defaultDecideOptions,
_defaultLogLevel = defaultLogLevel,
_sdkSettings = sdkSettings {
// Set the logger if provided
if (logger != null) {
setLogger(logger);
} else {
logWarning("Logger not provided.");
}
}
/// Starts Optimizely SDK (Synchronous) with provided sdkKey.
Future<BaseResponse> initializeClient() async {
return await OptimizelyClientWrapper.initializeClient(
_sdkKey,
_eventOptions,
_datafilePeriodicDownloadInterval,
_datafileHostOptions,
_defaultDecideOptions,
_defaultLogLevel,
_sdkSettings,
_customLogger
);
}
/// Use the activate method to start an experiment.
/// The activate call will conditionally activate an experiment for a user based on the provided experiment key and a randomized hash of the provided user ID.
/// If the user satisfies audience conditions for the experiment and the experiment is valid and running, the function returns the variation the user is bucketed into.
/// Otherwise, activate returns empty variationKey. Make sure that your code adequately deals with the case when the experiment is not activated (e.g. execute the default variation).
///
/// Takes [experimentKey] The key of the experiment to set with the forced variation.
/// Takes [userId] The ID of the user to force into the variation.
/// Takes [attributes] A [Map] of custom key-value string pairs specifying attributes for the user.
/// Returns [ActivateResponse] The key of the variation where the user is bucketed, or `empty variationKey` if the user
/// doesn't qualify for the experiment.
Future<ActivateResponse> activate(String experimentKey, String userId,
[Map<String, dynamic> attributes = const {}]) async {
return await OptimizelyClientWrapper.activate(
_sdkKey, experimentKey, userId, attributes);
}
/// Get variation for experiment and user ID with user attributes.
///
/// Takes [experimentKey] The key of the experiment to set with the forced variation.
/// Takes [userId] The ID of the user to force into the variation.
/// Takes [attributes] A [Map] of custom key-value string pairs specifying attributes for the user.
/// Returns [GetVariationResponse] The key of the variation where the user is bucketed, or `empty variationKey` if the user
/// doesn't qualify for the experiment.
Future<GetVariationResponse> getVariation(String experimentKey, String userId,
[Map<String, dynamic> attributes = const {}]) async {
return await OptimizelyClientWrapper.getVariation(
_sdkKey, experimentKey, userId, attributes);
}
/// Get forced variation for experiment and user ID.
///
/// Takes [experimentKey] The key of the experiment to set with the forced variation.
/// Takes [userId] The ID of the user to force into the variation.
/// Returns [GetVariationResponse] The key of the variation where the user is bucketed, or `empty variationKey` if the user
/// doesn't qualify for the experiment.
Future<GetVariationResponse> getForcedVariation(
String experimentKey, String userId) async {
return await OptimizelyClientWrapper.getForcedVariation(
_sdkKey, experimentKey, userId);
}
/// Set forced variation for experiment and user ID to variationKey.
///
/// Takes [experimentKey] The key of the experiment to set with the forced variation.
/// Takes [userId] The ID of the user to force into the variation.
/// Takes [variationKey] The key of the forced variation.
/// Returns [BaseResponse] success as `true` if the user was successfully forced into a variation, `false` if the `experimentKey`
/// isn't in the project file or the `variationKey` isn't in the experiment.
Future<BaseResponse> setForcedVariation(String experimentKey, String userId,
[String variationKey = ""]) async {
return await OptimizelyClientWrapper.setForcedVariation(
_sdkKey, experimentKey, userId, variationKey);
}
/// Returns [OptimizelyConfigResponse] a snapshot of the current project configuration.
Future<OptimizelyConfigResponse> getOptimizelyConfig() async {
return await OptimizelyClientWrapper.getOptimizelyConfig(_sdkKey);
}
/// Send an event to the ODP server.
///
/// Takes [action] The event action name.
/// Optional [type] The event type (default = "fullstack").
/// Optional [identifiers] A dictionary for identifiers.
/// Optional [data] A dictionary for associated data. The default event data will be added to this data before sending to the ODP server.
/// Returns [BaseResponse] A object containing success result or reason of failure.
Future<BaseResponse> sendOdpEvent(String action,
{String? type,
Map<String, String> identifiers = const {},
Map<String, dynamic> data = const {}}) async {
return await OptimizelyClientWrapper.sendOdpEvent(_sdkKey, action,
type: type, identifiers: identifiers, data: data);
}
/// Returns the device vuid.
///
/// Returns [GetVuidResponse] A object containing device vuid
Future<GetVuidResponse> getVuid() async {
return await OptimizelyClientWrapper.getVuid(_sdkKey);
}
/// Creates a context of the user for which decision APIs will be called.
///
/// NOTE: A user context will only be created successfully when the SDK is fully configured using initializeClient.
///
/// Optional [userId] the [String] user ID to be used for bucketing.
/// The device vuid will be used as an userId when userId is not provided.
/// Takes [attributes] An Optional [Map] of attribute names to current user attribute values.
/// Returns An [OptimizelyUserContext] associated with this OptimizelyClient.
Future<OptimizelyUserContext?> createUserContext(
{String? userId, Map<String, dynamic> attributes = const {}}) async {
return await OptimizelyClientWrapper.createUserContext(_sdkKey,
userId: userId, attributes: attributes);
}
/// Allows user to remove notification listener using id.
///
/// Takes [int] id which allows user to remove that specific listener.
/// Returns [BaseResponse] A object containing success result or reason of failure.
Future<BaseResponse> removeNotificationListener(int id) async {
return await OptimizelyClientWrapper.removeNotificationListener(
_sdkKey, id);
}
/// Allows user to clear all notification listeners of specific type.
///
/// Takes [ListenerType] A listener type which allows user to remove only specific type of listeners.
/// Returns [BaseResponse] A object containing success result or reason of failure.
Future<BaseResponse> clearNotificationListeners(
ListenerType listenerType) async {
return await OptimizelyClientWrapper.clearNotificationListeners(
_sdkKey, listenerType);
}
/// Allows user to check and clear all notification listeners.
///
/// Returns [BaseResponse] A object containing success result or reason of failure.
Future<BaseResponse> clearAllNotificationListeners() async {
return await OptimizelyClientWrapper.clearAllNotificationListeners(_sdkKey);
}
/// Checks if eventHandler are Closeable and calls close on them.
///
/// Returns [BaseResponse] A object containing success result or reason of failure.
Future<BaseResponse> close() async {
return await OptimizelyClientWrapper.close(_sdkKey);
}
/// Allows user to listen to supported Activate notifications.
///
/// Takes [callback] A [ActivateNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> addActivateNotificationListener(
ActivateNotificationCallback callback) async {
return await _addActivateNotificationListener(callback);
}
/// Allows user to listen to supported Decision notifications.
///
/// Takes [callback] A [DecisionNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> addDecisionNotificationListener(
DecisionNotificationCallback callback) async {
return await _addDecisionNotificationListener(callback);
}
/// Allows user to listen to supported Track notifications.
///
/// Takes [callback] A [TrackNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> addTrackNotificationListener(
TrackNotificationCallback callback) async {
return await _addTrackNotificationListener(callback);
}
/// Allows user to listen to supported LogEvent notifications.
///
/// Takes [callback] A [LogEventNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> addLogEventNotificationListener(
LogEventNotificationCallback callback) async {
return await _addLogEventNotificationListener(callback);
}
/// Allows user to listen to supported Project Config Update notifications.
///
/// Takes [callback] A [MultiUseCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> addConfigUpdateNotificationListener(
MultiUseCallback callback) async {
return await _addConfigUpdateNotificationListener(callback);
}
/// Allows user to listen to supported Activate notifications.
///
/// Takes [callback] A [ActivateNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> _addActivateNotificationListener(
ActivateNotificationCallback callback) async {
return await OptimizelyClientWrapper.addActivateNotificationListener(
_sdkKey, callback);
}
/// Allows user to listen to supported Decision notifications.
///
/// Takes [callback] A [DecisionNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> _addDecisionNotificationListener(
DecisionNotificationCallback callback) async {
return await OptimizelyClientWrapper.addDecisionNotificationListener(
_sdkKey, callback);
}
/// Allows user to listen to supported Track notifications.
///
/// Takes [callback] A [TrackNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> _addTrackNotificationListener(
TrackNotificationCallback callback) async {
return await OptimizelyClientWrapper.addTrackNotificationListener(
_sdkKey, callback);
}
/// Allows user to listen to supported LogEvent notifications.
///
/// Takes [callback] A [LogEventNotificationCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> _addLogEventNotificationListener(
LogEventNotificationCallback callback) async {
return await OptimizelyClientWrapper.addLogEventNotificationListener(
_sdkKey, callback);
}
/// Allows user to listen to supported Project Config Update notifications.
///
/// Takes [callback] A [MultiUseCallback] notification handler to be added.
/// Returns [int] Id of registered listener that allows the user to remove the added notification listener.
Future<int> _addConfigUpdateNotificationListener(
MultiUseCallback callback) async {
return await OptimizelyClientWrapper.addConfigUpdateNotificationListener(
_sdkKey, callback);
}
}