|
| 1 | +--- |
| 2 | +title: Assignments |
| 3 | +sidebar_position: 4 |
| 4 | +--- |
| 5 | + |
| 6 | +import ApiOptionRef from '@site/src/components/ApiOptionRef'; |
| 7 | + |
| 8 | +Assignments are the mechanism through which a given Subject is assigned to a variation for a feature flag, experiment, or bandit. |
| 9 | + |
| 10 | +## Assignment Types |
| 11 | + |
| 12 | +The Eppo SDK supports the following assignment types: |
| 13 | + |
| 14 | +- String |
| 15 | +- Boolean |
| 16 | +- JSON |
| 17 | +- Integer |
| 18 | +- Double |
| 19 | + |
| 20 | +### String Assignments |
| 21 | + |
| 22 | +String assignments are the most common type. They return a string value that is set as the variation for the experiment. |
| 23 | + |
| 24 | +```java |
| 25 | +String assignedVariation = eppoClient.getStringAssignment( |
| 26 | + "flagkey", |
| 27 | + "subjectKey", |
| 28 | + "defaultValue" |
| 29 | +); |
| 30 | +``` |
| 31 | + |
| 32 | +### Boolean Assignments |
| 33 | + |
| 34 | +Boolean flags support simple on/off toggles: |
| 35 | + |
| 36 | +```java |
| 37 | +Boolean isEnabled = eppoClient.getBooleanAssignment( |
| 38 | + "flagkey", |
| 39 | + "subjectKey", |
| 40 | + false |
| 41 | +); |
| 42 | +``` |
| 43 | + |
| 44 | +### JSON Assignments |
| 45 | + |
| 46 | +JSON assignments are useful for complex configuration values: |
| 47 | + |
| 48 | +```java |
| 49 | +JsonNode config = eppoClient.getJSONAssignment( |
| 50 | + "flagkey", |
| 51 | + "subjectKey", |
| 52 | + defaultJsonNode |
| 53 | +); |
| 54 | +``` |
| 55 | + |
| 56 | +If you prefer to use a different JSON library, you can use `getJSONStringAssignment()` to get the unparsed JSON string. |
| 57 | + |
| 58 | +### Numeric Assignments |
| 59 | + |
| 60 | +For numeric values, you can use either integer or double assignments: |
| 61 | + |
| 62 | +```java |
| 63 | +Integer count = eppoClient.getIntegerAssignment( |
| 64 | + "flagkey", |
| 65 | + "subjectKey", |
| 66 | + 0 |
| 67 | +); |
| 68 | + |
| 69 | +Double price = eppoClient.getDoubleAssignment( |
| 70 | + "flagkey", |
| 71 | + "subjectKey", |
| 72 | + 0.0 |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +## Subject Attributes |
| 77 | + |
| 78 | +All assignment methods support optional subject attributes that can be used for targeting: |
| 79 | + |
| 80 | +```java |
| 81 | +Attributes subjectAttributes = new Attributes( |
| 82 | + Map.of( |
| 83 | + "country", EppoValue.valueOf("FR"), |
| 84 | + "age", EppoValue.valueOf(60), |
| 85 | + "isReturningUser", EppoValue.valueOf(true) |
| 86 | + ) |
| 87 | +); |
| 88 | + |
| 89 | +String assignedVariation = eppoClient.getStringAssignment( |
| 90 | + "flagkey", |
| 91 | + "subjectKey", |
| 92 | + subjectAttributes, |
| 93 | + "defaultValue" |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +## Assignment Logger |
| 98 | + |
| 99 | +For experiments, you'll need to implement an assignment logger to track exposure events. The logger receives an assignment object with the following fields: |
| 100 | + |
| 101 | +<ApiOptionRef |
| 102 | + name="timestamp" |
| 103 | + type="Date" |
| 104 | + defaultValue="undefined" |
| 105 | +> |
| 106 | + |
| 107 | +The time when the subject was assigned to the variation |
| 108 | +</ApiOptionRef> |
| 109 | + |
| 110 | +<ApiOptionRef |
| 111 | + name="experiment" |
| 112 | + type="String" |
| 113 | + defaultValue="undefined" |
| 114 | +> |
| 115 | + |
| 116 | +The key of the experiment |
| 117 | +</ApiOptionRef> |
| 118 | + |
| 119 | +<ApiOptionRef |
| 120 | + name="featureFlag" |
| 121 | + type="String" |
| 122 | + defaultValue="undefined" |
| 123 | +> |
| 124 | + |
| 125 | +The key of the feature flag |
| 126 | +</ApiOptionRef> |
| 127 | + |
| 128 | +<ApiOptionRef |
| 129 | + name="allocation" |
| 130 | + type="String" |
| 131 | + defaultValue="undefined" |
| 132 | +> |
| 133 | + |
| 134 | +The key of the allocation |
| 135 | +</ApiOptionRef> |
| 136 | + |
| 137 | +<ApiOptionRef |
| 138 | + name="variation" |
| 139 | + type="String" |
| 140 | + defaultValue="undefined" |
| 141 | +> |
| 142 | + |
| 143 | +The identifier of the assigned variation |
| 144 | +</ApiOptionRef> |
| 145 | + |
| 146 | +<ApiOptionRef |
| 147 | + name="subject" |
| 148 | + type="String" |
| 149 | + defaultValue="undefined" |
| 150 | +> |
| 151 | + |
| 152 | +The identifier of the subject |
| 153 | +</ApiOptionRef> |
| 154 | + |
| 155 | +<ApiOptionRef |
| 156 | + name="subjectAttributes" |
| 157 | + type="Attributes" |
| 158 | + defaultValue="{}" |
| 159 | +> |
| 160 | + |
| 161 | +A map of metadata about the subject |
| 162 | +</ApiOptionRef> |
| 163 | + |
| 164 | +:::info |
| 165 | +More details about logging and examples (with Segment, Rudderstack, mParticle, and Snowplow) can be found in the [event logging](/sdks/event-logging/assignment-logging) page. |
| 166 | +::: |
0 commit comments