Skip to content

Commit 9f9e2b7

Browse files
committed
Added support for multiple user contexts for single sdkKey.
1 parent 2dc0fb2 commit 9f9e2b7

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

ios/Classes/HelperClasses/Constants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct DecideOption {
5050
struct RequestParameterKey {
5151
static let sdkKey = "sdkKey"
5252
static let userId = "userID"
53+
static let userContextId = "userContextId"
5354
static let notificationId = "id"
5455
static let notificationType = "type"
5556
static let notificationPayload = "payload"

ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
2626
// to keep track of optimizely clients against their sdkKeys
2727
var optimizelyClientsTracker = [String: OptimizelyClient?]()
2828
// to keep track of optimizely user contexts against their sdkKeys
29-
var userContextsTracker = [String: OptimizelyUserContext?]()
29+
var userContextsTracker = [String: [String: OptimizelyUserContext?]]()
3030

3131
// to communicate with optimizely flutter sdk
3232
static var channel: FlutterMethodChannel!
3333

34+
// to track each unique userContext
35+
var uuid: String {
36+
return UUID().uuidString
37+
}
38+
3439
/// Registers optimizely_flutter_sdk channel to communicate with the flutter sdk to receive requests and send responses
3540
public static func register(with registrar: FlutterPluginRegistrar) {
3641
channel = FlutterMethodChannel(name: "optimizely_flutter_sdk", binaryMessenger: registrar.messenger())
@@ -91,7 +96,7 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
9196

9297
let datafileHandler = DefaultDatafileHandler()
9398
if let datafileHostPrefix = parameters[RequestParameterKey.datafileHostPrefix] as? String, let datafileHostSuffix = parameters[RequestParameterKey.datafileHostSuffix] as? String {
94-
datafileHandler.endPointStringFormat = String(format: datafileHostPrefix + datafileHostSuffix, sdkKey)
99+
datafileHandler.endPointStringFormat = String(format: "\(datafileHostPrefix)\(datafileHostSuffix)", sdkKey)
95100
}
96101

97102
// Delete old user context
@@ -195,12 +200,14 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
195200
return
196201
}
197202

198-
if let attributes = Utils.getTypedMap(arguments: parameters[RequestParameterKey.attributes] as? Any) {
199-
userContextsTracker[sdkKey] = optimizelyClient.createUserContext(userId: userId, attributes: attributes)
203+
let userContextId = uuid
204+
let userContext = optimizelyClient.createUserContext(userId: userId, attributes: Utils.getTypedMap(arguments: parameters[RequestParameterKey.attributes] as? Any))
205+
if userContextsTracker[sdkKey] != nil {
206+
userContextsTracker[sdkKey]![userContextId] = userContext
200207
} else {
201-
userContextsTracker[sdkKey] = optimizelyClient.createUserContext(userId: userId)
208+
userContextsTracker[sdkKey] = [userContextId: userContext]
202209
}
203-
result(self.createResponse(success: true, reason: SuccessMessage.userContextCreated))
210+
result(self.createResponse(success: true,result: [RequestParameterKey.userContextId: userContextId], reason: SuccessMessage.userContextCreated))
204211
}
205212

206213
/// Sets attributes for the user context.
@@ -379,10 +386,10 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
379386

380387
/// Returns saved user context
381388
func getUserContext(arguments: Any?) -> OptimizelyUserContext? {
382-
guard let parameters = arguments as? Dictionary<String, Any?>, let sdkKey = parameters[RequestParameterKey.sdkKey] as? String else {
389+
guard let parameters = arguments as? Dictionary<String, Any?>, let sdkKey = parameters[RequestParameterKey.sdkKey] as? String, let userContextId = parameters[RequestParameterKey.userContextId] as? String else {
383390
return nil
384391
}
385-
return userContextsTracker[sdkKey] ?? nil
392+
return userContextsTracker[sdkKey]?[userContextId] ?? nil
386393
}
387394

388395
func createResponse(success: Bool, result: Any? = nil, reason: String? = nil) -> [String: Any] {

lib/src/optimizely_client_wrapper.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ class OptimizelyClientWrapper {
105105
Constants.userID: userId,
106106
Constants.attributes: Utils.convertToTypedMap(attributes)
107107
}));
108+
108109
if (result[Constants.responseSuccess] == true) {
109-
return OptimizelyUserContext(sdkKey, _channel);
110+
final response =
111+
Map<String, dynamic>.from(result[Constants.responseResult]);
112+
return OptimizelyUserContext(
113+
sdkKey, response[Constants.userContextId], _channel);
110114
}
111115
return null;
112116
}

lib/src/user_context/optimizely_user_context.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ enum OptimizelyDecideOption {
4545
///
4646
class OptimizelyUserContext {
4747
final String _sdkKey;
48+
final String _userContextId;
4849
final MethodChannel _channel;
4950

50-
OptimizelyUserContext(this._sdkKey, this._channel);
51+
OptimizelyUserContext(this._sdkKey, this._userContextId, this._channel);
5152

5253
/// Sets attributes for the user context.
5354
Future<BaseResponse> setAttributes(Map<String, dynamic> attributes) async {
5455
final result = Map<String, dynamic>.from(
5556
await _channel.invokeMethod(Constants.setAttributesMethod, {
5657
Constants.sdkKey: _sdkKey,
58+
Constants.userContextId: _userContextId,
5759
Constants.attributes: Utils.convertToTypedMap(attributes)
5860
}));
5961
return BaseResponse(result);
@@ -65,6 +67,7 @@ class OptimizelyUserContext {
6567
final result = Map<String, dynamic>.from(
6668
await _channel.invokeMethod(Constants.trackEventMethod, {
6769
Constants.sdkKey: _sdkKey,
70+
Constants.userContextId: _userContextId,
6871
Constants.eventKey: eventKey,
6972
Constants.eventTags: Utils.convertToTypedMap(eventTags)
7073
}));
@@ -101,6 +104,7 @@ class OptimizelyUserContext {
101104
var result = Map<String, dynamic>.from(
102105
await _channel.invokeMethod(Constants.decideMethod, {
103106
Constants.sdkKey: _sdkKey,
107+
Constants.userContextId: _userContextId,
104108
Constants.keys: keys,
105109
Constants.optimizelyDecideOption: convertedOptions,
106110
}));
@@ -112,6 +116,7 @@ class OptimizelyUserContext {
112116
OptimizelyForcedDecision decision) async {
113117
Map<String, dynamic> request = {
114118
Constants.sdkKey: _sdkKey,
119+
Constants.userContextId: _userContextId,
115120
Constants.flagKey: context.flagKey,
116121
Constants.variationKey: decision.variationKey
117122
};
@@ -128,6 +133,7 @@ class OptimizelyUserContext {
128133
OptimizelyDecisionContext context) async {
129134
Map<String, dynamic> request = {
130135
Constants.sdkKey: _sdkKey,
136+
Constants.userContextId: _userContextId,
131137
Constants.flagKey: context.flagKey,
132138
};
133139
if (context.ruleKey != null) {
@@ -144,6 +150,7 @@ class OptimizelyUserContext {
144150
OptimizelyDecisionContext context) async {
145151
Map<String, dynamic> request = {
146152
Constants.sdkKey: _sdkKey,
153+
Constants.userContextId: _userContextId,
147154
Constants.flagKey: context.flagKey,
148155
};
149156
if (context.ruleKey != null) {
@@ -159,6 +166,7 @@ class OptimizelyUserContext {
159166
final result = Map<String, dynamic>.from(
160167
await _channel.invokeMethod(Constants.removeAllForcedDecisions, {
161168
Constants.sdkKey: _sdkKey,
169+
Constants.userContextId: _userContextId,
162170
}));
163171
return BaseResponse(result);
164172
}

lib/src/utils/constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Constants {
4040
// Request parameter keys
4141
static const String id = "id";
4242
static const String sdkKey = "sdkKey";
43+
static const String userContextId = "userContextId";
4344
static const String userContext = "userContext";
4445
static const String userID = "userID";
4546
static const String attributes = "attributes";

test/optimizely_flutter_sdk_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void main() {
9292
return {
9393
Constants.responseSuccess: true,
9494
Constants.responseReason: Constants.userContextCreated,
95+
Constants.responseResult: {Constants.userContextId: "123"},
9596
};
9697
case Constants.setAttributesMethod:
9798
return {

0 commit comments

Comments
 (0)