Skip to content

Commit 0453ea9

Browse files
Updated the iOS Demo app to include an option to create Optimizely via datafile download or from a locally stored datafile. This is a nice utility for testing.
1 parent 72f9c6e commit 0453ea9

File tree

3 files changed

+93
-35
lines changed

3 files changed

+93
-35
lines changed

OptimizelyiOSDemoApp/OptimizelyiOSDemoApp.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
7C6B9074803B4D4970B342BB /* Pods_OptimizelyiOSDemoApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FB412018B98235E5A62E95F /* Pods_OptimizelyiOSDemoApp.framework */; };
11+
EA2C23C11DE3F6300063ADA0 /* test_data_10_experiments.json in Resources */ = {isa = PBXBuildFile; fileRef = EA2C23C01DE3F6300063ADA0 /* test_data_10_experiments.json */; };
1112
EA5246C11DC7186800AF6685 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5246C01DC7186800AF6685 /* AppDelegate.swift */; };
1213
EA5246C31DC7186800AF6685 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5246C21DC7186800AF6685 /* ViewController.swift */; };
1314
EA5246C61DC7186800AF6685 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EA5246C41DC7186800AF6685 /* Main.storyboard */; };
@@ -60,6 +61,7 @@
6061
0AD2B4CCAE039F03823867E5 /* Pods-OptimizelyiOSDemoApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelyiOSDemoApp.release.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelyiOSDemoApp/Pods-OptimizelyiOSDemoApp.release.xcconfig"; sourceTree = "<group>"; };
6162
1FB412018B98235E5A62E95F /* Pods_OptimizelyiOSDemoApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OptimizelyiOSDemoApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6263
3321336732A089FCB7E90C94 /* Pods-OptimizelyiOSDemoApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelyiOSDemoApp.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelyiOSDemoApp/Pods-OptimizelyiOSDemoApp.debug.xcconfig"; sourceTree = "<group>"; };
64+
EA2C23C01DE3F6300063ADA0 /* test_data_10_experiments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = test_data_10_experiments.json; path = "Test Data/test_data_10_experiments.json"; sourceTree = "<group>"; };
6365
EA5246BD1DC7186800AF6685 /* OptimizelyiOSDemoApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OptimizelyiOSDemoApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
6466
EA5246C01DC7186800AF6685 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6567
EA5246C21DC7186800AF6685 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -112,6 +114,14 @@
112114
name = Pods;
113115
sourceTree = "<group>";
114116
};
117+
EA2C23BE1DE3F5CE0063ADA0 /* Test Data */ = {
118+
isa = PBXGroup;
119+
children = (
120+
EA2C23C01DE3F6300063ADA0 /* test_data_10_experiments.json */,
121+
);
122+
name = "Test Data";
123+
sourceTree = "<group>";
124+
};
115125
EA5246B41DC7186800AF6685 = {
116126
isa = PBXGroup;
117127
children = (
@@ -150,6 +160,7 @@
150160
EA5246D41DC7186800AF6685 /* OptimizelyiOSDemoAppTests */ = {
151161
isa = PBXGroup;
152162
children = (
163+
EA2C23BE1DE3F5CE0063ADA0 /* Test Data */,
153164
EA5246D51DC7186800AF6685 /* OptimizelyiOSDemoAppTests.swift */,
154165
EA5246D71DC7186800AF6685 /* OptimizelyiOSDemoAppTests-Info.plist */,
155166
);
@@ -318,6 +329,7 @@
318329
buildActionMask = 2147483647;
319330
files = (
320331
EA5246CB1DC7186800AF6685 /* LaunchScreen.storyboard in Resources */,
332+
EA2C23C11DE3F6300063ADA0 /* test_data_10_experiments.json in Resources */,
321333
EA5246C81DC7186800AF6685 /* Assets.xcassets in Resources */,
322334
EA5246C61DC7186800AF6685 /* Main.storyboard in Resources */,
323335
);

OptimizelyiOSDemoApp/OptimizelyiOSDemoApp/AppDelegate.swift

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,41 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323
var window: UIWindow?
2424

2525
// Optimizely SDK test parameters
26-
let projectId = "7871184663";
27-
let attributes = ["userType" : "new"];
28-
let eventKey = "userEvent";
29-
let experimentKey = "exp1";
30-
let userId = "1234";
31-
let revenue = NSNumber(value: 88);
26+
let userId = "1234"
27+
let revenue = NSNumber(value: 88)
28+
let eventHandlerDispatchInterval = 1000
29+
30+
// default parameters for initializing Optimizely from saved datafile
31+
let datafileName = "test_data_10_experiments"
32+
var projectId = "6377970066"
33+
var attributes = ["browser_type" : "firefox"]
34+
var eventKey = "testEventWithAudiences"
35+
var experimentKey = "testExperimentWithFirefoxAudience" // experiment ID: 6383811281
36+
let downloadDatafile = true
3237

3338
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
34-
let networkService = OPTLYNetworkService();
3539

36-
let eventDispatcherBuilderBlock : OPTLYEventDispatcherBuilderBlock = {(builder)in
37-
builder?.eventHandlerDispatchInterval = 1000;
40+
// use different parameters if initializing Optimizely from downloaded datafile
41+
if self.downloadDatafile == true {
42+
projectId = "7871184663"
43+
attributes = ["userType" : "new"]
44+
eventKey = "userEvent"
45+
experimentKey = "exp1"
46+
47+
// initialize Optimizely from a datafile download
48+
initializeOptimizely(projectId:projectId, completionHandler: { (optimizely) in
49+
let OPTLYVariation = optimizely?.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
50+
print("bucketed variation:", OPTLYVariation)
51+
optimizely?.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
52+
})
53+
} else {
54+
// initialize Optimizely from a saved datafile
55+
let optimizely = initializeOptimizely(datafileName: datafileName)
56+
optimizely.activateExperiment(self.experimentKey, userId: self.userId, attributes: self.attributes)
57+
optimizely.trackEvent(self.eventKey, userId: self.userId, attributes: self.attributes, eventValue: self.revenue)
3858
}
3959

40-
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock(eventDispatcherBuilderBlock)
41-
42-
networkService.downloadProjectConfig(projectId, completionHandler:
43-
{ [weak self] (data, response, error) in
44-
45-
46-
let logger : OPTLYLoggerDefault? = OPTLYLoggerDefault();
47-
let errorHandler = OPTLYErrorHandlerNoOp();
48-
49-
let projectConfig = OPTLYProjectConfig.init(datafile: data, with: logger, with: errorHandler);
50-
print(projectConfig);
51-
52-
53-
let defaultOptimizely : Optimizely? = (Optimizely.initWithBuilderBlock({ (builder)in
54-
builder!.datafile = data;
55-
builder!.eventDispatcher = eventDispatcher;
56-
builder!.logger = logger;
57-
//builder!.errorHandler = errorHandler;
58-
}));
59-
60-
defaultOptimizely?.activateExperiment(self!.experimentKey, userId: self!.userId, attributes: self?.attributes);
61-
defaultOptimizely?.trackEvent(self!.eventKey, userId: self!.userId, attributes: (self?.attributes)!, eventValue: (self?.revenue)!);
62-
});
63-
64-
return true
60+
return true;
6561
}
6662

6763
func applicationWillResignActive(_ application: UIApplication) {
@@ -86,6 +82,55 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
8682
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
8783
}
8884

89-
85+
// --- initialize Optimizely from a saved datafile ----
86+
func initializeOptimizely(datafileName:String) -> Optimizely {
87+
88+
var optimizely : Optimizely? = nil
89+
let bundle = Bundle.init(for: self.classForCoder)
90+
if let filePath = bundle.path(forResource: datafileName, ofType: "json") {
91+
do {
92+
let fileContents = try String.init(contentsOfFile: filePath, encoding: String.Encoding.utf8)
93+
let jsonData = fileContents.data(using: String.Encoding.utf8)!
94+
print(fileContents)
95+
96+
let eventDispatcherBuilderBlock : OPTLYEventDispatcherBuilderBlock = {(builder)in
97+
builder?.eventHandlerDispatchInterval = self.eventHandlerDispatchInterval
98+
}
99+
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock(eventDispatcherBuilderBlock)
100+
101+
optimizely = (Optimizely.initWithBuilderBlock({(builder)in
102+
builder!.datafile = jsonData
103+
builder!.eventDispatcher = eventDispatcher
104+
}))
105+
} catch {
106+
print("invalid json data")
107+
}
108+
}
109+
110+
return optimizely!
111+
}
112+
113+
// ---- initialize Optimizely from a datafile download ----
114+
func initializeOptimizely(projectId:String, completionHandler: @escaping (_ optimizely:Optimizely?) -> Void) -> Void {
115+
116+
let networkService = OPTLYNetworkService()
117+
let eventDispatcherBuilderBlock : OPTLYEventDispatcherBuilderBlock = {[weak self] (builder)in
118+
builder?.eventHandlerDispatchInterval = self!.eventHandlerDispatchInterval
119+
}
120+
let eventDispatcher = OPTLYEventDispatcher.initWithBuilderBlock(eventDispatcherBuilderBlock)
121+
122+
networkService.downloadProjectConfig(projectId) { (data, response, error) in
123+
let projectConfig = OPTLYProjectConfig.init(datafile: data, with:nil, with:nil)
124+
print(projectConfig)
125+
126+
let optimizely : Optimizely? = (Optimizely.initWithBuilderBlock({ (builder)in
127+
builder!.datafile = data;
128+
builder!.eventDispatcher = eventDispatcher;
129+
}));
130+
131+
completionHandler(optimizely)
132+
}
133+
}
134+
90135
}
91136

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"experiments": [{"status": "Running", "layerId": "1234", "key": "testExperiment4", "trafficAllocation": [{"entityId": "6373141147", "endOfRange": 5000}, {"entityId": "6373141148", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6373141147", "key": "control"}, {"id": "6373141148", "key": "variation"}], "forcedVariations": {}, "id": "6358043286"}, {"status": "Running", "layerId": "1234", "key": "testExperiment5", "trafficAllocation": [{"entityId": "6335242053", "endOfRange": 5000}, {"entityId": "6335242054", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6335242053", "key": "control"}, {"id": "6335242054", "key": "variation"}], "forcedVariations": {}, "id": "6364835526"}, {"status": "Paused", "layerId": "1234", "key": "testExperimentNotRunning", "trafficAllocation": [{"entityId": "6377281127", "endOfRange": 5000}, {"entityId": "6377281128", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6377281127", "key": "control"}, {"id": "6377281128", "key": "variation"}], "forcedVariations": {}, "id": "6367444440"}, {"status": "Running", "layerId": "1234", "key": "testExperiment1", "trafficAllocation": [{"entityId": "6384330451", "endOfRange": 5000}, {"entityId": "6384330452", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6384330451", "key": "control"}, {"id": "6384330452", "key": "variation"}], "forcedVariations": {"variation_user": "variation", "control_user": "control"}, "id": "6367863211"}, {"status": "Running", "layerId": "1234", "key": "testExperiment3", "trafficAllocation": [{"entityId": "6376141758", "endOfRange": 5000}, {"entityId": "6376141759", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6376141758", "key": "control"}, {"id": "6376141759", "key": "variation"}], "forcedVariations": {}, "id": "6370392407"}, {"status": "Running", "layerId": "1234", "key": "testExperiment6", "trafficAllocation": [{"entityId": "6379060914", "endOfRange": 5000}, {"entityId": "6379060915", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6379060914", "key": "control"}, {"id": "6379060915", "key": "variation"}], "forcedVariations": {"forced_variation_user": "variation"}, "id": "6370821515"}, {"status": "Running", "layerId": "1234", "key": "testExperiment2", "trafficAllocation": [{"entityId": "6386700062", "endOfRange": 5000}, {"entityId": "6386700063", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6386700062", "key": "control"}, {"id": "6386700063", "key": "variation"}], "forcedVariations": {"variation_user": "variation", "control_user": "control"}, "id": "6376870125"}, {"status": "Running", "layerId": "1234", "key": "testExperimentWithFirefoxAudience", "trafficAllocation": [{"entityId": "6333082303", "endOfRange": 5000}, {"entityId": "6333082304", "endOfRange": 10000}], "audienceIds": ["6369992312"], "variations": [{"id": "6333082303", "key": "control"}, {"id": "6333082304", "key": "variation"}], "forcedVariations": {}, "id": "6383811281"}], "version": "1", "audiences": [{"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"safari\"}]]]", "id": "6352892614", "name": "Safari users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"android\"}]]]", "id": "6355234780", "name": "Android users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"desktop\"}]]]", "id": "6360574256", "name": "Desktop users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"opera\"}]]]", "id": "6365864533", "name": "Opera users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"tablet\"}]]]", "id": "6369831151", "name": "Tablet users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"firefox\"}]]]", "id": "6369992312", "name": "Firefox users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"chrome\"}]]]", "id": "6373141157", "name": "Chrome users"}, {"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_dimension\", \"value\": \"ie\"}]]]", "id": "6378191386", "name": "IE users"}], "attributes": [{"id": "6359881003", "key": "browser_type"}], "groups": [{"policy": "random", "trafficAllocation": [], "experiments": [], "id": "6367902163"}, {"policy": "random", "trafficAllocation": [], "experiments": [], "id": "6393150032"}, {"policy": "random", "trafficAllocation": [{"entityId": "6450630664", "endOfRange": 5000}, {"entityId": "6447021179", "endOfRange": 10000}], "experiments": [{"status": "Running", "layerId": "1234", "key": "mutex_exp2", "trafficAllocation": [{"entityId": "6453410972", "endOfRange": 5000}, {"entityId": "6453410973", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6453410972", "key": "a"}, {"id": "6453410973", "key": "b"}], "forcedVariations": {"user_b": "b", "user_a": "a"}, "id": "6447021179"}, {"status": "Running", "layerId": "1234", "key": "mutex_exp1", "trafficAllocation": [{"entityId": "6451680205", "endOfRange": 5000}, {"entityId": "6451680206", "endOfRange": 10000}], "audienceIds": [], "variations": [{"id": "6451680205", "key": "a"}, {"id": "6451680206", "key": "b"}], "forcedVariations": {}, "id": "6450630664"}], "id": "6436903041"}], "projectId": "6377970066", "accountId": "6365361536", "events": [{"experimentIds": ["6450630664", "6447021179"], "id": "6370392432", "key": "testEventWithMultipleGroupedExperiments"}, {"experimentIds": ["6367863211"], "id": "6372590948", "key": "testEvent"}, {"experimentIds": ["6364835526", "6450630664", "6367863211", "6376870125", "6383811281", "6358043286", "6370392407", "6367444440", "6370821515", "6447021179"], "id": "6372952486", "key": "testEventWithMultipleExperiments"}, {"experimentIds": ["6367444440"], "id": "6380961307", "key": "testEventWithExperimentNotRunning"}, {"experimentIds": ["6383811281"], "id": "6384781388", "key": "testEventWithAudiences"}, {"experimentIds": [], "id": "6386521015", "key": "testEventWithoutExperiments"}, {"experimentIds": ["6450630664", "6383811281", "6376870125"], "id": "6316734272", "key": "Total Revenue"}], "revision": "83"}

0 commit comments

Comments
 (0)