Skip to content

Commit 0820f04

Browse files
authored
Merge pull request #42 from launchdarkly/eb/ch32307/metric-value
support metric value in track()
2 parents 8aa23ea + c145908 commit 0820f04

File tree

5 files changed

+158
-103
lines changed

5 files changed

+158
-103
lines changed

src/LaunchDarkly/Impl/EventFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function newIdentifyEvent($user)
9393
);
9494
}
9595

96-
public function newCustomEvent($eventName, $user, $data)
96+
public function newCustomEvent($eventName, $user, $data, $metricValue)
9797
{
9898
$e = array(
9999
'kind' => 'custom',
@@ -104,6 +104,9 @@ public function newCustomEvent($eventName, $user, $data)
104104
if (isset($data)) {
105105
$e['data'] = $data;
106106
}
107+
if (isset($metricValue)) {
108+
$e['metricValue'] = $metricValue;
109+
}
107110
return $e;
108111
}
109112

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace LaunchDarkly;
3+
4+
class NullEventProcessor
5+
{
6+
public function enqueue($event)
7+
{
8+
return true;
9+
}
10+
11+
public function flush()
12+
{
13+
}
14+
}

src/LaunchDarkly/LDClient.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace LaunchDarkly;
33

44
use LaunchDarkly\Impl\EventFactory;
5+
use LaunchDarkly\Impl\NullEventPublisher;
56
use LaunchDarkly\Integrations\Guzzle;
67
use Monolog\Handler\ErrorLogHandler;
78
use Monolog\Logger;
@@ -107,7 +108,17 @@ public function __construct($sdkKey, $options = array())
107108
$this->_eventFactoryDefault = new EventFactory(false);
108109
$this->_eventFactoryWithReasons = new EventFactory(true);
109110

110-
$this->_eventProcessor = new EventProcessor($sdkKey, $options);
111+
if (isset($options['event_processor'])) {
112+
$ep = $options['event_processor'];
113+
if (is_callable($ep)) {
114+
$ep = $ep($sdkKey, $options);
115+
}
116+
$this->_eventProcessor = $ep;
117+
} elseif ($this->_offline || !$this->_send_events) {
118+
$this->_eventProcessor = new EventProcessor($sdkKey, $options);
119+
} else {
120+
$this->_eventProcessor = new EventProcessor($sdkKey, $options);
121+
}
111122

112123
$this->_featureRequester = $this->getFeatureRequester($sdkKey, $options);
113124
}
@@ -184,9 +195,6 @@ private function variationDetailInternal($key, $user, $default, $eventFactory)
184195
return new EvaluationDetail($default, null, EvaluationReason::error($errorKind));
185196
};
186197
$sendEvent = function ($detail, $flag) use ($key, $user, $default, $eventFactory) {
187-
if ($this->isOffline() || !$this->_send_events) {
188-
return;
189-
}
190198
if ($flag) {
191199
$event = $eventFactory->newEvalEvent($flag, $user, $detail, $default);
192200
} else {
@@ -222,10 +230,8 @@ private function variationDetailInternal($key, $user, $default, $eventFactory)
222230
return $result;
223231
}
224232
$evalResult = $flag->evaluate($user, $this->_featureRequester, $eventFactory);
225-
if (!$this->isOffline() && $this->_send_events) {
226-
foreach ($evalResult->getPrerequisiteEvents() as $e) {
227-
$this->_eventProcessor->enqueue($e);
228-
}
233+
foreach ($evalResult->getPrerequisiteEvents() as $e) {
234+
$this->_eventProcessor->enqueue($e);
229235
}
230236
$detail = $evalResult->getDetail();
231237
if ($detail->isDefaultValue()) {
@@ -271,28 +277,25 @@ public function isOffline()
271277
*
272278
* @param $eventName string The name of the event
273279
* @param $user LDUser The user that performed the event
274-
* @param $data mixed
280+
* @param $data mixed Optional additional information to associate with the event
281+
* @param $metricValue number A numeric value used by the LaunchDarkly experimentation feature in
282+
* numeric custom metrics. Can be omitted if this event is used by only non-numeric metrics. This
283+
* field will also be returned as part of the custom event for Data Export.
275284
*/
276-
public function track($eventName, $user, $data)
285+
public function track($eventName, $user, $data = null, $metricValue = null)
277286
{
278-
if ($this->isOffline()) {
279-
return;
280-
}
281287
if (is_null($user) || $user->isKeyBlank()) {
282288
$this->_logger->warning("Track called with null user or null/empty user key!");
283289
return;
284290
}
285-
$this->_eventProcessor->enqueue($this->_eventFactoryDefault->newCustomEvent($eventName, $user, $data));
291+
$this->_eventProcessor->enqueue($this->_eventFactoryDefault->newCustomEvent($eventName, $user, $data, $metricValue));
286292
}
287293

288294
/**
289295
* @param $user LDUser
290296
*/
291297
public function identify($user)
292298
{
293-
if ($this->isOffline()) {
294-
return;
295-
}
296299
if (is_null($user) || $user->isKeyBlank()) {
297300
$this->_logger->warning("Track called with null user or null/empty user key!");
298301
return;

0 commit comments

Comments
 (0)