Skip to content

Commit 13c476d

Browse files
msohailhussainMike Ng
authored and
Mike Ng
committed
fix(track): Send decisions for all experiments using an event when using track (#124)
1 parent a9156d0 commit 13c476d

File tree

4 files changed

+108
-28
lines changed

4 files changed

+108
-28
lines changed

Diff for: src/Optimizely/Event/Builder/EventBuilder.php

+32-27
Original file line numberDiff line numberDiff line change
@@ -180,45 +180,50 @@ private function getImpressionParams(Experiment $experiment, $variationId)
180180
private function getConversionParams($config, $eventKey, $experimentVariationMap, $eventTags)
181181
{
182182
$conversionParams = [];
183+
$singleSnapshot = [];
184+
$decisions = [];
185+
183186
foreach ($experimentVariationMap as $experimentId => $variationId) {
184-
$singleSnapshot = [];
187+
188+
185189
$experiment = $config->getExperimentFromId($experimentId);
186190
$eventEntity = $config->getEvent($eventKey);
187191

188-
$singleSnapshot[DECISIONS] = [
189-
[
190-
CAMPAIGN_ID => $experiment->getLayerId(),
191-
EXPERIMENT_ID => $experiment->getId(),
192-
VARIATION_ID => $variationId
193-
]
194-
];
195-
196-
$singleSnapshot[EVENTS] = [
197-
[
198-
ENTITY_ID => $eventEntity->getId(),
199-
TIMESTAMP => time()*1000,
200-
UUID => GeneratorUtils::getRandomUuid(),
201-
KEY => $eventKey
202-
]
192+
$decision = [
193+
CAMPAIGN_ID => $experiment->getLayerId(),
194+
EXPERIMENT_ID => $experiment->getId(),
195+
VARIATION_ID => $variationId
203196
];
197+
$decisions [] = $decision;
198+
}
204199

205-
if (!is_null($eventTags)) {
206-
$revenue = EventTagUtils::getRevenueValue($eventTags, $this->_logger);
207-
if (!is_null($revenue)) {
208-
$singleSnapshot[EVENTS][0][EventTagUtils::REVENUE_EVENT_METRIC_NAME] = $revenue;
209-
}
200+
$eventDict = [
201+
ENTITY_ID => $eventEntity->getId(),
202+
TIMESTAMP => time()*1000,
203+
UUID => GeneratorUtils::getRandomUuid(),
204+
KEY => $eventKey
205+
];
210206

211-
$eventValue = EventTagUtils::getNumericValue($eventTags, $this->_logger);
212-
if (!is_null($eventValue)) {
213-
$singleSnapshot[EVENTS][0][EventTagUtils::NUMERIC_EVENT_METRIC_NAME] = $eventValue;
214-
}
207+
if (!is_null($eventTags)) {
208+
$revenue = EventTagUtils::getRevenueValue($eventTags, $this->_logger);
209+
if (!is_null($revenue)) {
210+
$eventDict[EventTagUtils::REVENUE_EVENT_METRIC_NAME] = $revenue;
211+
}
215212

216-
$singleSnapshot[EVENTS][0]['tags'] = $eventTags;
213+
$eventValue = EventTagUtils::getNumericValue($eventTags, $this->_logger);
214+
if (!is_null($eventValue)) {
215+
$eventDict[EventTagUtils::NUMERIC_EVENT_METRIC_NAME] = $eventValue;
217216
}
218217

219-
$conversionParams [] = $singleSnapshot;
218+
if(count($eventTags) > 0) {
219+
$eventDict['tags'] = $eventTags;
220+
}
220221
}
221222

223+
$singleSnapshot[DECISIONS] = $decisions;
224+
$singleSnapshot[EVENTS] [] = $eventDict;
225+
$conversionParams [] = $singleSnapshot;
226+
222227
return $conversionParams;
223228
}
224229

Diff for: tests/EventTests/EventBuilderTest.php

+66
Original file line numberDiff line numberDiff line change
@@ -823,4 +823,70 @@ public function testCreateConversionEventWithUserAgentAttribute()
823823
$result = $this->areLogEventsEqual($expectedLogEvent, $logEvent);
824824
$this->assertTrue($result[0], $result[1]);
825825
}
826+
827+
public function testCreateConversionEventWhenEventUsedInMultipleExp()
828+
{
829+
$eventTags = [
830+
'revenue' => 4200,
831+
'value' => 1.234,
832+
'non-revenue' => 'abc'
833+
];
834+
$this->expectedEventParams['visitors'][0]['snapshots'][0]['decisions'][] = [
835+
'campaign_id' => '4',
836+
'experiment_id' => '122230',
837+
'variation_id' => '122234'
838+
];
839+
840+
$this->expectedEventParams['visitors'][0]['snapshots'][0]['events'][0] = [
841+
'entity_id' => '7718020065',
842+
'timestamp' => $this->timestamp,
843+
'uuid' => $this->uuid,
844+
'key' => 'multi_exp_event',
845+
'revenue' => 4200,
846+
'value' => 1.234,
847+
'tags' => $eventTags,
848+
];
849+
850+
array_unshift($this->expectedEventParams['visitors'][0]['attributes'],
851+
[
852+
'entity_id' => '7723280020',
853+
'key' => 'device_type',
854+
'type' => 'custom',
855+
'value' => 'iPhone'
856+
],[
857+
'entity_id' => '7723340004',
858+
'key' => 'location',
859+
'type' => 'custom',
860+
'value' => 'SF'
861+
]);
862+
863+
$decisions = [
864+
'7716830082' => '7721010009',
865+
'122230' => '122234'
866+
];
867+
$userAttributes = [
868+
'device_type' => 'iPhone',
869+
'location' => 'SF'
870+
];
871+
872+
$logEvent = $this->eventBuilder->createConversionEvent(
873+
$this->config,
874+
'multi_exp_event',
875+
$decisions,
876+
$this->testUserId,
877+
$userAttributes,
878+
$eventTags
879+
);
880+
$expectedLogEvent = new LogEvent(
881+
$this->expectedEventUrl,
882+
$this->expectedEventParams,
883+
$this->expectedEventHttpVerb,
884+
$this->expectedEventHeaders
885+
);
886+
887+
$logEvent = $this->fakeParamsToReconcile($logEvent);
888+
$result = $this->areLogEventsEqual($expectedLogEvent, $logEvent);
889+
$this->assertTrue($result[0], $result[1]);
890+
891+
}
826892
}

Diff for: tests/ProjectConfigTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public function testInit()
140140
$this->assertEquals(
141141
[
142142
'purchase' => $this->config->getEvent('purchase'),
143-
'unlinked_event' => $this->config->getEvent('unlinked_event')
143+
'unlinked_event' => $this->config->getEvent('unlinked_event'),
144+
'multi_exp_event' => $this->config->getEvent('multi_exp_event')
144145
],
145146
$eventKeyMap->getValue($this->config)
146147
);

Diff for: tests/TestData.php

+8
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,14 @@
483483
"experimentIds": [],
484484
"id": "7718020064",
485485
"key": "unlinked_event"
486+
},
487+
{
488+
"experimentIds":[
489+
"7716830082",
490+
"122230"
491+
],
492+
"id": "7718020065",
493+
"key": "multi_exp_event"
486494
}
487495
],
488496
"anonymizeIP": false,

0 commit comments

Comments
 (0)