|
17 | 17 | package com.optimizely.ab.event.internal; |
18 | 18 |
|
19 | 19 | import com.optimizely.ab.annotations.VisibleForTesting; |
20 | | -import com.optimizely.ab.bucketing.Bucketer; |
21 | | -import com.optimizely.ab.bucketing.UserProfile; |
22 | 20 | import com.optimizely.ab.config.Attribute; |
23 | 21 | import com.optimizely.ab.config.Experiment; |
24 | 22 | import com.optimizely.ab.config.ProjectConfig; |
25 | 23 | import com.optimizely.ab.config.Variation; |
26 | 24 | import com.optimizely.ab.event.LogEvent; |
27 | 25 | import com.optimizely.ab.event.internal.payload.Conversion; |
28 | 26 | import com.optimizely.ab.event.internal.payload.Decision; |
29 | | -import com.optimizely.ab.event.internal.payload.EventMetric; |
30 | 27 | import com.optimizely.ab.event.internal.payload.Event.ClientEngine; |
| 28 | +import com.optimizely.ab.event.internal.payload.EventMetric; |
31 | 29 | import com.optimizely.ab.event.internal.payload.Feature; |
32 | 30 | import com.optimizely.ab.event.internal.payload.Impression; |
33 | 31 | import com.optimizely.ab.event.internal.payload.LayerState; |
34 | 32 | import com.optimizely.ab.event.internal.serializer.DefaultJsonSerializer; |
35 | 33 | import com.optimizely.ab.event.internal.serializer.Serializer; |
36 | 34 | import com.optimizely.ab.internal.EventTagUtils; |
37 | | -import com.optimizely.ab.internal.ProjectValidationUtils; |
38 | | - |
39 | 35 | import org.slf4j.Logger; |
40 | 36 | import org.slf4j.LoggerFactory; |
41 | 37 |
|
42 | | -import javax.annotation.CheckForNull; |
43 | 38 | import javax.annotation.Nonnull; |
44 | | -import javax.annotation.Nullable; |
45 | | - |
46 | 39 | import java.util.ArrayList; |
47 | 40 | import java.util.Collections; |
48 | 41 | import java.util.List; |
@@ -106,45 +99,41 @@ public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig, |
106 | 99 | } |
107 | 100 |
|
108 | 101 | public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig, |
109 | | - @Nonnull Bucketer bucketer, |
110 | | - @Nullable UserProfile userProfile, |
| 102 | + @Nonnull Map<Experiment, Variation> experimentVariationMap, |
111 | 103 | @Nonnull String userId, |
112 | 104 | @Nonnull String eventId, |
113 | 105 | @Nonnull String eventName, |
114 | 106 | @Nonnull Map<String, String> attributes, |
115 | 107 | @Nonnull Map<String, ?> eventTags) { |
116 | 108 |
|
117 | | - Conversion conversionPayload = new Conversion(); |
118 | | - conversionPayload.setVisitorId(userId); |
119 | | - conversionPayload.setTimestamp(System.currentTimeMillis()); |
120 | | - conversionPayload.setProjectId(projectConfig.getProjectId()); |
121 | | - conversionPayload.setAccountId(projectConfig.getAccountId()); |
122 | | - conversionPayload.setUserFeatures(createUserFeatures(attributes, projectConfig)); |
123 | | - |
124 | | - List<LayerState> layerStates = createLayerStates(projectConfig, bucketer, userProfile, userId, eventName, attributes); |
125 | | - if (layerStates.isEmpty()) { |
| 109 | + if (experimentVariationMap.isEmpty()) { |
126 | 110 | return null; |
127 | 111 | } |
128 | | - conversionPayload.setLayerStates(layerStates); |
129 | 112 |
|
130 | | - conversionPayload.setEventEntityId(eventId); |
131 | | - conversionPayload.setEventName(eventName); |
| 113 | + List<LayerState> layerStates = createLayerStates(projectConfig, experimentVariationMap); |
132 | 114 |
|
133 | 115 | Long eventValue = EventTagUtils.getRevenueValue(eventTags); |
| 116 | + List<EventMetric> eventMetrics = Collections.emptyList(); |
134 | 117 | if (eventValue != null) { |
135 | | - conversionPayload.setEventMetrics( |
136 | | - Collections.singletonList(new EventMetric(EventMetric.REVENUE_METRIC_TYPE, eventValue))); |
137 | | - } else { |
138 | | - conversionPayload.setEventMetrics(Collections.<EventMetric>emptyList()); |
| 118 | + eventMetrics = Collections.singletonList(new EventMetric(EventMetric.REVENUE_METRIC_TYPE, eventValue)); |
139 | 119 | } |
140 | 120 |
|
141 | | - conversionPayload.setEventFeatures(Collections.<Feature>emptyList()); |
142 | | - conversionPayload.setIsGlobalHoldback(false); |
| 121 | + Conversion conversionPayload = new Conversion(); |
| 122 | + conversionPayload.setAccountId(projectConfig.getAccountId()); |
143 | 123 | conversionPayload.setAnonymizeIP(projectConfig.getAnonymizeIP()); |
144 | 124 | conversionPayload.setClientEngine(clientEngine); |
145 | 125 | conversionPayload.setClientVersion(clientVersion); |
146 | | - conversionPayload.setRevision(projectConfig.getRevision()); |
| 126 | + conversionPayload.setEventEntityId(eventId); |
147 | 127 | conversionPayload.setEventFeatures(createEventFeatures(eventTags)); |
| 128 | + conversionPayload.setEventName(eventName); |
| 129 | + conversionPayload.setEventMetrics(eventMetrics); |
| 130 | + conversionPayload.setIsGlobalHoldback(false); |
| 131 | + conversionPayload.setLayerStates(layerStates); |
| 132 | + conversionPayload.setProjectId(projectConfig.getProjectId()); |
| 133 | + conversionPayload.setRevision(projectConfig.getRevision()); |
| 134 | + conversionPayload.setTimestamp(System.currentTimeMillis()); |
| 135 | + conversionPayload.setUserFeatures(createUserFeatures(attributes, projectConfig)); |
| 136 | + conversionPayload.setVisitorId(userId); |
148 | 137 |
|
149 | 138 | String payload = this.serializer.serialize(conversionPayload); |
150 | 139 | return new LogEvent(RequestMethod.POST, CONVERSION_ENDPOINT, Collections.<String, String>emptyMap(), payload); |
@@ -208,33 +197,18 @@ private List<Feature> createEventFeatures(Map<String, ?> eventTags) { |
208 | 197 | * no good reason. |
209 | 198 | * |
210 | 199 | * @param projectConfig the current project config |
211 | | - * @param bucketer the bucketing algorithm to use |
212 | | - * @param userId the user's id for the impression event |
213 | | - * @param eventKey the goal that the bucket map will be filtered by |
214 | | - * @param attributes the user's attributes |
| 200 | + * @param experimentVariationMap the mapping of experiments associated with this event |
| 201 | + * and the variations the user was bucketed into for that experiment |
| 202 | + * |
215 | 203 | */ |
216 | | - private List<LayerState> createLayerStates(ProjectConfig projectConfig, Bucketer bucketer, UserProfile userProfile, String userId, |
217 | | - String eventKey, Map<String, String> attributes) { |
218 | | - List<Experiment> allExperiments = projectConfig.getExperiments(); |
219 | | - List<String> experimentIds = projectConfig.getExperimentIdsForGoal(eventKey); |
| 204 | + private List<LayerState> createLayerStates(ProjectConfig projectConfig, Map<Experiment, Variation> experimentVariationMap) { |
220 | 205 | List<LayerState> layerStates = new ArrayList<LayerState>(); |
221 | 206 |
|
222 | | - for (Experiment experiment : allExperiments) { |
223 | | - if (experimentIds.contains(experiment.getId()) && |
224 | | - ProjectValidationUtils.validatePreconditions(projectConfig, userProfile, experiment, userId, attributes)) { |
225 | | - if (experiment.isRunning()) { |
226 | | - Variation bucketedVariation = bucketer.bucket(experiment, userId); |
227 | | - if (bucketedVariation != null) { |
228 | | - Decision decision = new Decision(bucketedVariation.getId(), false, experiment.getId()); |
229 | | - layerStates.add( |
230 | | - new LayerState(experiment.getLayerId(), projectConfig.getRevision(), decision, true)); |
231 | | - } |
232 | | - } else { |
233 | | - logger.info( |
234 | | - "Not tracking event \"{}\" for experiment \"{}\" because experiment has status \"Launched\".", |
235 | | - eventKey, experiment.getKey()); |
236 | | - } |
237 | | - } |
| 207 | + for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) { |
| 208 | + Experiment experiment = entry.getKey(); |
| 209 | + Variation variation = entry.getValue(); |
| 210 | + Decision decision = new Decision(variation.getId(), false, experiment.getId()); |
| 211 | + layerStates.add(new LayerState(experiment.getLayerId(), projectConfig.getRevision(), decision, true)); |
238 | 212 | } |
239 | 213 |
|
240 | 214 | return layerStates; |
|
0 commit comments