Skip to content

Commit 770961c

Browse files
authored
Merging devel into master (#47)
1 parent e94d3ef commit 770961c

File tree

6 files changed

+485
-261
lines changed

6 files changed

+485
-261
lines changed

src/Optimizely/Bucketer.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,6 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
127127
return new Variation();
128128
}
129129

130-
// Check if user is whitelisted for a variation.
131-
$forcedVariations = $experiment->getForcedVariations();
132-
if (!is_null($forcedVariations) && isset($forcedVariations[$userId])) {
133-
$variationKey = $forcedVariations[$userId];
134-
$variation = $config->getVariationFromKey($experiment->getKey(), $variationKey);
135-
if ($variationKey) {
136-
$this->_logger->log(
137-
Logger::INFO,
138-
sprintf('User "%s" is forced in variation "%s".', $userId, $variationKey)
139-
);
140-
}
141-
return $variation;
142-
}
143-
144130
// Determine if experiment is in a mutually exclusive group.
145131
if ($experiment->isInMutexGroup()) {
146132
$group = $config->getGroup($experiment->getGroupId());
@@ -184,4 +170,32 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
184170
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no variation.', $userId));
185171
return new Variation();
186172
}
173+
174+
/**
175+
* Determine variation the user has been forced into.
176+
*
177+
* @param $config ProjectConfig Configuration for the project.
178+
* @param $experiment Experiment Experiment in which user is to be bucketed.
179+
* @param $userId string User identifier.
180+
*
181+
* @return null|Variation Representing the variation the user is forced into.
182+
*/
183+
public function getForcedVariation($config, $experiment, $userId)
184+
{
185+
// Check if user is whitelisted for a variation.
186+
$forcedVariations = $experiment->getForcedVariations();
187+
if (!is_null($forcedVariations) && isset($forcedVariations[$userId])) {
188+
$variationKey = $forcedVariations[$userId];
189+
$variation = $config->getVariationFromKey($experiment->getKey(), $variationKey);
190+
if ($variationKey) {
191+
$this->_logger->log(
192+
Logger::INFO,
193+
sprintf('User "%s" is forced in variation "%s" of experiment "%s".', $userId, $variationKey, $experiment->getKey())
194+
);
195+
}
196+
return $variation;
197+
}
198+
199+
return null;
200+
}
187201
}

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
namespace Optimizely\Event\Builder;
1919
include('Params.php');
2020

21-
use Optimizely\Bucketer;
2221
use Optimizely\Entity\Experiment;
2322
use Optimizely\Event\LogEvent;
2423
use 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

Comments
 (0)