|
| 1 | +--- |
| 2 | +title: "OptimizelyConfig" |
| 3 | +excerpt: "" |
| 4 | +slug: "optimizelyconfig-go" |
| 5 | +hidden: true |
| 6 | +createdAt: "2020-01-17T19:10:55.970Z" |
| 7 | +updatedAt: "2020-01-28T21:53:26.607Z" |
| 8 | +--- |
| 9 | +### Overview |
| 10 | + |
| 11 | +Full Stack SDKs open a well-defined set of public APIs, hiding all implementation details. However, some clients may need access to project configuration data within the "datafile". |
| 12 | + |
| 13 | +In this document, we extend our public APIs to define data models and access methods, which clients can use to access project configuration data. |
| 14 | + |
| 15 | +### OptimizelyConfig API |
| 16 | + |
| 17 | +A public configuration data model (OptimizelyConfig) is defined below as a structured format of static Optimizely Project data. |
| 18 | + |
| 19 | +OptimizelyConfig can be accessed from OptimizelyClient (top-level) with this public API call: |
| 20 | +```go |
| 21 | +client, e := optimizelyFactory.Client() |
| 22 | +var config = client.GetOptimizelyConfig() |
| 23 | +``` |
| 24 | +`GetOptimizelyConfig` returns an `config.OptimizelyConfig` instance which include a datafile revision number, all experiments, and feature flags mapped by their key values. |
| 25 | + |
| 26 | +>ℹ️ Note |
| 27 | +> |
| 28 | +> When the SDK datafile is updated (the client can add a notification listener for `ProjectConfigUpdateNotification` to get notified), the client is expected to call the method to get the updated OptimizelyConfig data. See examples below. |
| 29 | +
|
| 30 | +```go |
| 31 | +// OptimizelyConfig is an object describing the current project configuration data |
| 32 | +type OptimizelyConfig struct { |
| 33 | + Revision string |
| 34 | + ExperimentsMap map[string]OptimizelyExperiment |
| 35 | + FeaturesMap map[string]OptimizelyFeature |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +// OptimizelyFeature is an object describing a feature |
| 40 | +type OptimizelyFeature struct { |
| 41 | + ID string |
| 42 | + Key string |
| 43 | + ExperimentsMap map[string]OptimizelyExperiment |
| 44 | + VariablesMap map[string]OptimizelyVariable |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +// OptimizelyExperiment is an object describing a feature test or an A/B test |
| 49 | +type OptimizelyExperiment struct { |
| 50 | + ID string |
| 51 | + Key string |
| 52 | + VariationsMap map[string]OptimizelyVariation |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +// OptimizelyVariation is an object describing a variation in a feature test or A/B //test |
| 57 | +type OptimizelyVariation struct { |
| 58 | + ID string |
| 59 | + Key string |
| 60 | + FeatureEnabled bool |
| 61 | + VariablesMap map[string]OptimizelyVariable |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// OptimizelyVariable is an object describing a feature variable |
| 66 | +type OptimizelyVariable struct { |
| 67 | + ID string |
| 68 | + Key string |
| 69 | + Type string |
| 70 | + Value string |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Examples |
| 75 | +OptimizelyConfig can be accessed from OptimizelyClient (top-level) like this: |
| 76 | + |
| 77 | +```go |
| 78 | +client, e := optimizelyFactory.Client() |
| 79 | +// all experiments |
| 80 | +var experimentsMap = optimizelyConfig.ExperimentsMap |
| 81 | +var experiments = []config.OptimizelyExperiment{} |
| 82 | +var experimentKeys = []string{} |
| 83 | + |
| 84 | +for experimentKey, experiment := range experimentsMap { |
| 85 | + experimentKeys = append(experimentKeys, experimentKey) |
| 86 | + experiments = append(experiments, experiment) |
| 87 | +} |
| 88 | + |
| 89 | +for _, experimentKey := range experimentKeys { |
| 90 | + var experiment = experimentsMap[experimentKey] |
| 91 | + |
| 92 | + // all variations for an experiment |
| 93 | + var variationsMap = experiment.VariationsMap |
| 94 | + var variations = []config.OptimizelyVariation{} |
| 95 | + var variationKeys = []string{} |
| 96 | + |
| 97 | + for variationKey, variation := range variationsMap { |
| 98 | + variations = append(variations, variation) |
| 99 | + variationKeys = append(variationKeys, variationKey) |
| 100 | + } |
| 101 | + |
| 102 | + for _, variationKey := range variationKeys { |
| 103 | + var variation = variationsMap[variationKey] |
| 104 | + |
| 105 | + // all variables for a variation |
| 106 | + var variablesMap = variation.VariablesMap |
| 107 | + var variables = []config.OptimizelyVariable{} |
| 108 | + var variableKeys = []string{} |
| 109 | + |
| 110 | + for variableKey, variable := range variablesMap { |
| 111 | + variables = append(variables, variable) |
| 112 | + variableKeys = append(variableKeys, variableKey) |
| 113 | + } |
| 114 | + |
| 115 | + for _, variableKey := range variableKeys { |
| 116 | + var variable = variablesMap[variableKey] |
| 117 | + // use variable data here... |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// all features |
| 123 | +var featuresMap = optimizelyConfig.FeaturesMap |
| 124 | +var features = []config.OptimizelyFeature{} |
| 125 | +var featureKeys = []string{} |
| 126 | + |
| 127 | +for featureKey, feature := range featuresMap { |
| 128 | + features = append(features, feature) |
| 129 | + featureKeys = append(featureKeys, featureKey) |
| 130 | +} |
| 131 | + |
| 132 | +for _, featureKey := range featureKeys { |
| 133 | + var feature = featuresMap[featureKey] |
| 134 | + |
| 135 | + // all experiments for a feature |
| 136 | + var experimentsMap = feature.ExperimentsMap |
| 137 | + var experiments = []config.OptimizelyExperiment{} |
| 138 | + var experimentKeys = []string{} |
| 139 | + |
| 140 | + for experimentKey, experiment := range experimentsMap { |
| 141 | + experiments = append(experiments, experiment) |
| 142 | + experimentKeys = append(experimentKeys, experimentKey) |
| 143 | + } |
| 144 | + |
| 145 | + // use experiments and other feature data here... |
| 146 | +} |
| 147 | + |
| 148 | +// listen to ProjectConfigUpdateNotification to get updated data |
| 149 | +callback := func(notification notification.ProjectConfigUpdateNotification) { |
| 150 | + var newConfig = client.GetOptimizelyConfig() |
| 151 | + // ... |
| 152 | +} |
| 153 | +client.ConfigManager.OnProjectConfigUpdate(callback) |
| 154 | +``` |
0 commit comments