1818namespace Optimizely \Event \Builder ;
1919include ('Params.php ' );
2020
21- use Optimizely \Bucketer ;
2221use Optimizely \Entity \Experiment ;
2322use Optimizely \Event \LogEvent ;
2423use Optimizely \ProjectConfig ;
@@ -63,21 +62,6 @@ class EventBuilder
6362 */
6463 private $ _eventParams ;
6564
66- /**
67- * @var Bucketer Providing Optimizely's bucket method.
68- */
69- private $ _bucketer ;
70-
71- /**
72- * EventBuilder constructor.
73- *
74- * @param $bucketer Bucketer
75- */
76- public function __construct (Bucketer $ bucketer )
77- {
78- $ this ->_bucketer = $ bucketer ;
79- }
80-
8165 /**
8266 * Helper function to reset event params.
8367 */
@@ -153,11 +137,11 @@ private function setImpressionParams(Experiment $experiment, $variationId)
153137 *
154138 * @param $config ProjectConfig Configuration for the project.
155139 * @param $eventKey string Key representing the event.
156- * @param $experiments array Experiments for which conversion event needs to be recorded .
140+ * @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into .
157141 * @param $userId string ID of user.
158142 * @param $eventTags array Hash representing metadata associated with the event.
159143 */
160- private function setConversionParams ($ config , $ eventKey , $ experiments , $ userId , $ eventTags )
144+ private function setConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags )
161145 {
162146 $ this ->_eventParams [EVENT_FEATURES ] = [];
163147 $ this ->_eventParams [EVENT_METRICS ] = [];
@@ -190,39 +174,40 @@ private function setConversionParams($config, $eventKey, $experiments, $userId,
190174 $ this ->_eventParams [EVENT_NAME ] = $ eventKey ;
191175
192176 $ this ->_eventParams [LAYER_STATES ] = [];
193- forEach ($ experiments as $ experiment ) {
194- $ variation = $ this ->_bucketer ->bucket ($ config , $ experiment , $ userId );
195- if (!is_null ($ variation ->getKey ())) {
196- array_push ($ this ->_eventParams [LAYER_STATES ], [
197- LAYER_ID => $ experiment ->getLayerId (),
198- ACTION_TRIGGERED => true ,
199- REVISION => $ config ->getRevision (),
200- DECISION => [
201- EXPERIMENT_ID => $ experiment ->getId (),
202- VARIATION_ID => $ variation ->getId (),
203- IS_LAYER_HOLDBACK => false
204- ]
205- ]);
206- }
177+ forEach ($ experimentVariationMap as $ experimentId => $ variationId ) {
178+ $ experiment = $ config ->getExperimentFromId ($ experimentId );
179+ array_push ($ this ->_eventParams [LAYER_STATES ], [
180+ LAYER_ID => $ experiment ->getLayerId (),
181+ ACTION_TRIGGERED => true ,
182+ REVISION => $ config ->getRevision (),
183+ DECISION => [
184+ EXPERIMENT_ID => $ experimentId ,
185+ VARIATION_ID => $ variationId ,
186+ IS_LAYER_HOLDBACK => false
187+ ]
188+ ]);
207189 }
208190 }
209191
210192 /**
211193 * Create impression event to be sent to the logging endpoint.
212194 *
213195 * @param $config ProjectConfig Configuration for the project.
214- * @param $experiment Experiment Experiment being activated.
215- * @param $variationId string Variation user
196+ * @param $experimentKey Experiment Experiment being activated.
197+ * @param $variationKey string Variation user
216198 * @param $userId string ID of user.
217199 * @param $attributes array Attributes of the user.
218200 *
219201 * @return LogEvent Event object to be sent to dispatcher.
220202 */
221- public function createImpressionEvent ($ config , Experiment $ experiment , $ variationId , $ userId , $ attributes )
203+ public function createImpressionEvent ($ config , $ experimentKey , $ variationKey , $ userId , $ attributes )
222204 {
223205 $ this ->resetParams ();
224206 $ this ->setCommonParams ($ config , $ userId , $ attributes );
225- $ this ->setImpressionParams ($ experiment , $ variationId );
207+
208+ $ experiment = $ config ->getExperimentFromKey ($ experimentKey );
209+ $ variation = $ config ->getVariationFromKey ($ experimentKey , $ variationKey );
210+ $ this ->setImpressionParams ($ experiment , $ variation ->getId ());
226211
227212 return new LogEvent (self ::$ IMPRESSION_ENDPOINT , $ this ->getParams (), self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
228213 }
@@ -232,18 +217,18 @@ public function createImpressionEvent($config, Experiment $experiment, $variatio
232217 *
233218 * @param $config ProjectConfig Configuration for the project.
234219 * @param $eventKey string Key representing the event.
235- * @param $experiments array Experiments for which conversion event needs to be recorded .
220+ * @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into .
236221 * @param $userId string ID of user.
237222 * @param $attributes array Attributes of the user.
238223 * @param $eventTags array Hash representing metadata associated with the event.
239224 *
240225 * @return LogEvent Event object to be sent to dispatcher.
241226 */
242- public function createConversionEvent ($ config , $ eventKey , $ experiments , $ userId , $ attributes , $ eventTags )
227+ public function createConversionEvent ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ attributes , $ eventTags )
243228 {
244229 $ this ->resetParams ();
245230 $ this ->setCommonParams ($ config , $ userId , $ attributes );
246- $ this ->setConversionParams ($ config , $ eventKey , $ experiments , $ userId , $ eventTags );
231+ $ this ->setConversionParams ($ config , $ eventKey , $ experimentVariationMap , $ userId , $ eventTags );
247232
248233 return new LogEvent (self ::$ CONVERSION_ENDPOINT , $ this ->getParams (), self ::$ HTTP_VERB , self ::$ HTTP_HEADERS );
249234 }
0 commit comments