Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 41961f7

Browse files
committed
update optimizely sdks
clanup
1 parent 1f56720 commit 41961f7

File tree

6 files changed

+153
-86
lines changed

6 files changed

+153
-86
lines changed
File renamed without changes.

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ group 'com.homexlabs.optimizely_dart'
22
version '1.0-SNAPSHOT'
33

44
buildscript {
5-
ext.kotlin_version = '1.3.50'
5+
ext.kotlin_version = '1.3.72'
66
repositories {
77
google()
88
jcenter()
@@ -37,7 +37,7 @@ android {
3737
disable 'InvalidPackage'
3838
}
3939
dependencies {
40-
implementation 'com.optimizely.ab:android-sdk:3.8.0'
40+
implementation 'com.optimizely.ab:android-sdk:3.10.1'
4141
implementation 'com.noveogroup.android:android-logger:1.3.6'
4242
}
4343
}

android/src/main/kotlin/com/homexlabs/optimizely_dart/OptimizelyPlugin.kt

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package com.homexlabs.optimizely_dart
22

33
import android.app.Activity
44
import androidx.annotation.NonNull
5+
import com.optimizely.ab.OptimizelyUserContext
56
import com.optimizely.ab.android.sdk.OptimizelyClient
67
import com.optimizely.ab.android.sdk.OptimizelyManager
78
import com.optimizely.ab.config.Variation
9+
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption
810
import io.flutter.embedding.engine.plugins.FlutterPlugin
911
import io.flutter.embedding.engine.plugins.activity.ActivityAware
1012
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -13,6 +15,7 @@ import io.flutter.plugin.common.MethodChannel
1315
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1416
import io.flutter.plugin.common.MethodChannel.Result
1517
import io.flutter.plugin.common.PluginRegistry.Registrar
18+
import java.util.concurrent.TimeUnit
1619

1720

1821
/** OptimizelyPlugin */
@@ -25,6 +28,7 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
2528
private lateinit var activity: Activity
2629

2730
private lateinit var optimizelyClient: OptimizelyClient
31+
private lateinit var user: OptimizelyUserContext
2832

2933
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
3034
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "optimizely_plugin")
@@ -61,19 +65,30 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
6165
initOptimizelyManagerAsync(sdkKey!!)
6266
result.success("")
6367
}
68+
"setUser" -> {
69+
val userId = call.argument<String>("user_id")
70+
val attributes = call.argument<MutableMap<String, Any>>("attributes")
71+
setUser(userId!!, attributes!!)
72+
}
6473
"isFeatureEnabled" -> {
6574
val featureKey = call.argument<String>("feature_key")
66-
val userId = call.argument<String>("user_id")
67-
val flag = isFeatureEnabled(featureKey!!, userId!!)
75+
val flag = isFeatureEnabled(featureKey!!)
6876
result.success(flag)
6977
}
7078
"getAllFeatureVariables" -> {
7179
val featureKey = call.argument<String>("feature_key")
72-
val userId = call.argument<String>("user_id")
73-
val attributes = call.argument<MutableMap<String, Any>>("attributes")
74-
val variables = getAllFeatureVariables(featureKey!!, userId!!, attributes!!)
80+
val variables = getAllFeatureVariables(featureKey!!)
7581
result.success(variables)
7682
}
83+
"getAllEnabledFeatures" -> {
84+
val features = getAllEnabledFeatures()
85+
result.success(features)
86+
}
87+
"activateGetVariation" -> {
88+
val featureKey = call.argument<String>("feature_key")
89+
val variation = activateGetVariation(featureKey!!)
90+
result.success(variation)
91+
}
7792
"getvariation" -> {
7893
val featureKey = call.argument<String>("feature_key")
7994
val userId = call.argument<String>("user_id")
@@ -83,10 +98,8 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
8398
}
8499
"trackEvent" -> {
85100
val featureKey = call.argument<String>("feature_key")
86-
val userId = call.argument<String>("user_id")
87-
val attributes = call.argument<MutableMap<String, Any>>("attributes")
88101
val eventTags = call.argument<MutableMap<String, Any>>("event_tags")
89-
trackEvent(featureKey!!, userId!!, attributes!!, eventTags!!)
102+
trackEvent(featureKey!!, eventTags!!)
90103
result.success("")
91104
}
92105
else -> result.notImplemented()
@@ -114,47 +127,60 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
114127
}
115128

116129
private fun initOptimizelyManager(sdkKey: String, dataFile: String) {
117-
val builder = OptimizelyManager.builder()
118-
// In Android, the minimum polling interval is 60 seconds. In iOS, the minimum polling
119-
// interval is 2 minutes while the app is open. If you specify shorter polling intervals
120-
// than these minimums, the SDK will automatically reset the intervals to 60 seconds (Android)
121-
// or 2 minutes (iOS).
122-
val optimizelyManager = builder.withDatafileDownloadInterval(60)
130+
val optimizelyManager = OptimizelyManager.builder()
123131
.withSDKKey(sdkKey)
132+
.withDatafileDownloadInterval(15, TimeUnit.MINUTES)
133+
.withEventDispatchInterval(30, TimeUnit.SECONDS)
124134
.build(activity.applicationContext)
125135

126136
optimizelyClient = optimizelyManager.initialize(activity.applicationContext, dataFile, true, true)
127137
}
128138

129139
private fun initOptimizelyManagerAsync(sdkKey: String) {
130-
val builder = OptimizelyManager.builder()
131-
// In Android, the minimum polling interval is 60 seconds. In iOS, the minimum polling
132-
// interval is 2 minutes while the app is open. If you specify shorter polling intervals
133-
// than these minimums, the SDK will automatically reset the intervals to 60 seconds (Android)
134-
// or 2 minutes (iOS).
135-
val optimizelyManager = builder.withDatafileDownloadInterval(60)
140+
val optimizelyManager = OptimizelyManager.builder()
136141
.withSDKKey(sdkKey)
142+
.withDatafileDownloadInterval(15, TimeUnit.MINUTES)
143+
.withEventDispatchInterval(30, TimeUnit.SECONDS)
137144
.build(activity.applicationContext)
138145

139146
optimizelyManager.initialize(activity.applicationContext, null) {
140147
client -> optimizelyClient = client
141148
}
142149
}
143150

144-
private fun isFeatureEnabled(featureKey: String, userId: String): Boolean{
145-
return optimizelyClient.isFeatureEnabled(featureKey, userId)
151+
private fun setUser(userId: String, attributes: MutableMap<String, Any>){
152+
val userTemp = optimizelyClient.createUserContext(userId, attributes)
153+
if(userTemp != null){
154+
user = userTemp
155+
}
156+
}
157+
158+
private fun isFeatureEnabled(featureKey: String): Boolean{
159+
val decision = user.decide(featureKey)
160+
return decision.enabled
146161
}
147162

148-
private fun getAllFeatureVariables(featureKey: String, userId: String, attributes: MutableMap<String, Any>): Map<String, Any>? {
149-
val json = optimizelyClient.getAllFeatureVariables(featureKey, userId, attributes)
150-
return json?.toMap()
163+
private fun getAllFeatureVariables(featureKey: String): Map<String, Any>? {
164+
val decision = user.decide(featureKey)
165+
return decision.variables.toMap()
166+
}
167+
168+
private fun getAllEnabledFeatures(): MutableSet<String> {
169+
val options: List<OptimizelyDecideOption> = listOf(OptimizelyDecideOption.ENABLED_FLAGS_ONLY)
170+
val decisions = user.decideAll(options)
171+
return decisions.keys
151172
}
152173

153174
private fun getVariation(featureKey: String, userId: String, attributes: MutableMap<String, Any>): Variation? {
154175
return optimizelyClient.getVariation(featureKey, userId, attributes)
155176
}
156177

157-
private fun trackEvent(eventKey: String, userId: String, attributes: MutableMap<String, Any>, eventTags: MutableMap<String, Any>) {
158-
optimizelyClient.track(eventKey, userId, attributes, eventTags)
178+
private fun activateGetVariation(featureKey: String): String? {
179+
val decision = user.decide(featureKey)
180+
return decision.variationKey
181+
}
182+
183+
private fun trackEvent(eventKey: String, eventTags: MutableMap<String, Any>) {
184+
user.trackEvent(eventKey, eventTags)
159185
}
160186
}

ios/Classes/SwiftOptimizelyPlugin.swift

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
1010

1111
typealias GetFeatureItems = (featureKey: String, userId: String, attributes: OptimizelyAttributes?, eventTags: OptimizelyEventTags?)
1212
var client: OptimizelyClient?
13+
var user: OptimizelyUserContext?
1314

1415
public static func register(with registrar: FlutterPluginRegistrar) {
1516
let channel = FlutterMethodChannel(
@@ -40,7 +41,7 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
4041
periodicDownloadInterval: 60
4142
)
4243

43-
try startClient(client, dataFile: dataFile)
44+
try client.start(datafile: dataFile!)
4445
self.client = client
4546

4647
result(nil)
@@ -55,7 +56,7 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
5556
periodicDownloadInterval: 60
5657
)
5758

58-
try startClient(client) { initResult in
59+
client.start { initResult in
5960
switch initResult {
6061
case .failure(let error):
6162
result(error.localizedDescription)
@@ -70,53 +71,78 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
7071
}
7172
case "isFeatureEnabled":
7273
do {
73-
let client = try ensureClient()
74-
let items = try getFeatureItems(from: arguments)
75-
let enabled = client.isFeatureEnabled(
76-
featureKey: items.featureKey,
77-
userId: items.userId,
78-
attributes: items.attributes
79-
)
74+
let user = try ensureUser()
75+
let featureKey: String = try arguments.argument(for: "feature_key")
76+
let decision = user.decide(key: featureKey)
77+
let enabled = decision.enabled
8078
result(enabled)
8179
} catch {
8280
result(error.localizedDescription)
8381
}
84-
case "getAllFeatureVariables":
82+
case "setUser":
8583
do {
8684
let client = try ensureClient()
87-
let items = try getFeatureItems(from: arguments)
88-
let json: OptimizelyJSON = try client.getAllFeatureVariables(
89-
featureKey: items.featureKey,
90-
userId: items.userId,
91-
attributes: items.attributes
92-
)
85+
let userId: String = try arguments.argument(for: "user_id")
86+
let attributes: OptimizelyAttributes? = try arguments.optionalArgument(for: "attributes")
87+
if(attributes != nil && !attributes!.isEmpty){
88+
self.user = client.createUserContext(userId: userId, attributes: attributes! as [String : Any])
89+
}
90+
else{
91+
self.user = client.createUserContext(userId: userId)
92+
}
93+
} catch {
94+
result(error.localizedDescription)
95+
}
96+
case "getAllFeatureVariables":
97+
do {
98+
let user = try ensureUser()
99+
let featureKey: String = try arguments.argument(for: "feature_key")
100+
let decision = user.decide(key: featureKey)
101+
let json: OptimizelyJSON = decision.variables
93102
result(json.toMap())
94103
} catch {
95104
result(error.localizedDescription)
96105
}
97106
case "getVariation":
98107
do {
99108
let client = try ensureClient()
100-
let items = try getFeatureItems(from: arguments)
109+
let userId: String = try arguments.argument(for: "user_id")
110+
let attributes: OptimizelyAttributes? = try arguments.optionalArgument(for: "attributes")
111+
let featureKey: String = try arguments.argument(for: "feature_key")
101112
let res: String = try client.getVariationKey(
102-
experimentKey: items.featureKey,
103-
userId: items.userId,
104-
attributes: items.attributes
113+
experimentKey: featureKey,
114+
userId: userId,
115+
attributes: attributes
105116
)
106117
result(res)
107118
} catch {
108119
result(error.localizedDescription)
109120
}
121+
case "activateGetVariation":
122+
do {
123+
let user = try ensureUser()
124+
let featureKey: String = try arguments.argument(for: "feature_key")
125+
let decision = user.decide(key: featureKey)
126+
result(decision.variationKey)
127+
} catch {
128+
result(error.localizedDescription)
129+
}
130+
case "getAllEnabledFeatures":
131+
do {
132+
let user = try ensureUser()
133+
let decisions = user.decideAll(options: [.enabledFlagsOnly])
134+
let enabledFlags = decisions.keys
135+
result(enabledFlags)
136+
} catch {
137+
result(error.localizedDescription)
138+
}
110139
case "trackEvent":
111140
do {
112-
let client = try ensureClient()
113-
let items = try getFeatureItems(from: arguments)
114-
try client.track(
115-
eventKey: items.featureKey,
116-
userId: items.userId,
117-
attributes: items.attributes,
118-
eventTags: items.eventTags
119-
)
141+
let user = try ensureUser()
142+
let eventKey: String = try arguments.argument(for: "event_key")
143+
let eventTags: OptimizelyEventTags? = try arguments.argument(for: "event_tags")
144+
try? user.trackEvent(eventKey: eventKey,
145+
eventTags: eventTags )
120146
} catch {
121147
result(error.localizedDescription)
122148
}
@@ -135,6 +161,17 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
135161
}
136162
return client
137163
}
164+
165+
func ensureUser() throws -> OptimizelyUserContext {
166+
guard let user = self.user else {
167+
throw FlutterError(
168+
code: "user",
169+
message: "Optimizely user not initialized",
170+
details: nil
171+
)
172+
}
173+
return user
174+
}
138175

139176
func startClient(_ client: OptimizelyClient, dataFile: String?) throws {
140177
if let dataFile = dataFile {
@@ -154,14 +191,6 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
154191
}
155192
}
156193
}
157-
158-
func getFeatureItems(from arguments: [String: Any]) throws -> GetFeatureItems {
159-
let featureKey: String = try arguments.argument(for: "feature_key")
160-
let userId: String = try arguments.argument(for: "user_id")
161-
let attributes: OptimizelyAttributes? = try arguments.optionalArgument(for: "attributes")
162-
let eventTags: OptimizelyEventTags? = try arguments.optionalArgument(for: "event_tags")
163-
return (featureKey, userId, attributes, eventTags)
164-
}
165194
}
166195

167196
// MARK: - Arguments

ios/optimizely_plugin.podspec renamed to ios/optimizely_dart.podspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ Pod::Spec.new do |s|
99
s.description = <<-DESC
1010
Flutter plugin for optimizely SDK
1111
DESC
12+
s.homepage = 'https://homex.com'
1213
s.license = { :file => '../LICENSE' }
14+
s.author = { 'HomexLabs' => '' }
1315
s.source = { :path => '.' }
1416
s.source_files = 'Classes/**/*'
1517
s.dependency 'Flutter'
16-
s.dependency 'OptimizelySwiftSDK', '~> 3.6.1'
18+
s.dependency 'OptimizelySwiftSDK', '~> 3.9.0'
1719
s.platform = :ios, '10.0'
1820

1921
# Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported.

0 commit comments

Comments
 (0)