|
| 1 | +// |
| 2 | +/**************************************************************************** |
| 3 | + * Copyright 2019, Optimizely, Inc. and contributors * |
| 4 | + * * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 6 | + * you may not use this file except in compliance with the License. * |
| 7 | + * You may obtain a copy of the License at * |
| 8 | + * * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 10 | + * * |
| 11 | + * Unless required by applicable law or agreed to in writing, software * |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 14 | + * See the License for the specific language governing permissions and * |
| 15 | + * limitations under the License. * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | + |
| 19 | +#import "SamplesForAPI.h" |
| 20 | +@import Optimizely; |
| 21 | + |
| 22 | +@implementation SamplesForAPI |
| 23 | + |
| 24 | ++(void)run:(OptimizelyClient*)optimizely { |
| 25 | + NSDictionary *attributes = @{ |
| 26 | + @"device": @"iPhone", |
| 27 | + @"lifetime": @24738388, |
| 28 | + @"is_logged_in": @true |
| 29 | + }; |
| 30 | + |
| 31 | + NSDictionary *tags = @{ |
| 32 | + @"category" : @"shoes", |
| 33 | + @"count": @5 |
| 34 | + }; |
| 35 | + |
| 36 | + // MARK: - activate |
| 37 | + |
| 38 | + { |
| 39 | + NSError *error = nil; |
| 40 | + NSString *variationKey = [optimizely activateWithExperimentKey:@"my_experiment_key" |
| 41 | + userId:@"user_123" |
| 42 | + attributes:attributes |
| 43 | + error:&error]; |
| 44 | + if (variationKey == nil) { |
| 45 | + NSLog(@"Error: %@", error); |
| 46 | + } else { |
| 47 | + NSLog(@"[activate] %@", variationKey); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - getVariationKey |
| 52 | + |
| 53 | + { |
| 54 | + NSError *error = nil; |
| 55 | + NSString *variationKey = [optimizely getVariationKeyWithExperimentKey:@"my_experiment_key" |
| 56 | + userId:@"user_123" |
| 57 | + attributes:attributes |
| 58 | + error:&error]; |
| 59 | + if (variationKey == nil) { |
| 60 | + NSLog(@"Error: %@", error); |
| 61 | + } else { |
| 62 | + NSLog(@"[getVariationKey] %@", variationKey); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // MARK: - getForcedVariation |
| 67 | + |
| 68 | + { |
| 69 | + NSString *variationKey = [optimizely getForcedVariationWithExperimentKey:@"my_experiment_key" |
| 70 | + userId:@"user_123"]; |
| 71 | + NSLog(@"[getForcedVariation] %@", variationKey); |
| 72 | + } |
| 73 | + |
| 74 | + // MARK: - setForcedVariation |
| 75 | + |
| 76 | + { |
| 77 | + BOOL result = [optimizely setForcedVariationWithExperimentKey:@"my_experiment_key" |
| 78 | + userId:@"user_123" |
| 79 | + variationKey:@"some_variation_key"]; |
| 80 | + NSLog(@"[setForcedVariation] %d", result); |
| 81 | + } |
| 82 | + |
| 83 | + // MARK: - isFeatureEnabled |
| 84 | + |
| 85 | + { |
| 86 | + NSError *error = nil; |
| 87 | + NSNumber *enabled = [optimizely isFeatureEnabledWithFeatureKey:@"my_feature_key" |
| 88 | + userId:@"user_123" |
| 89 | + attributes:attributes |
| 90 | + error:&error]; |
| 91 | + |
| 92 | + if (enabled == nil) { |
| 93 | + NSLog(@"Error: %@", error); |
| 94 | + } else { |
| 95 | + NSLog(@"[isFeatureEnabled] %d", enabled.boolValue); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - getFeatureVariable |
| 100 | + |
| 101 | + { |
| 102 | + NSError *error = nil; |
| 103 | + NSNumber *featureVariableValue = [optimizely getFeatureVariableDoubleWithFeatureKey:@"my_feature_key" |
| 104 | + variableKey:@"double_variable_key" |
| 105 | + userId:@"user_123" |
| 106 | + attributes:attributes |
| 107 | + error:&error]; |
| 108 | + if (featureVariableValue == nil) { |
| 109 | + NSLog(@"Error: %@", error); |
| 110 | + } else { |
| 111 | + NSLog(@"[getFeatureVariableDouble] %@", featureVariableValue); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // MARK: - getEnabledFeatures |
| 116 | + |
| 117 | + { |
| 118 | + NSError *error = nil; |
| 119 | + NSArray *enabledFeatures = [optimizely getEnabledFeaturesWithUserId:@"user_123" |
| 120 | + attributes:attributes |
| 121 | + error:&error]; |
| 122 | + if (enabledFeatures == nil) { |
| 123 | + NSLog(@"Error: %@", error); |
| 124 | + } else { |
| 125 | + NSLog(@"[getEnabledFeatures] %@", enabledFeatures); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // MARK: - track |
| 130 | + |
| 131 | + { |
| 132 | + NSError *error = nil; |
| 133 | + BOOL success = [optimizely trackWithEventKey:@"my_purchase_event_key" |
| 134 | + userId:@"user_123" |
| 135 | + attributes:attributes |
| 136 | + eventTags:tags |
| 137 | + error:&error]; |
| 138 | + if (success == false) { |
| 139 | + NSLog(@"Error: %@", error); |
| 140 | + } else { |
| 141 | + NSLog(@"[track]"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | +@end |
0 commit comments