Skip to content

Commit 42445df

Browse files
oakbanialiabbasrizvi
authored andcommitted
feat(track): Introducing easier event tracking (#156)
1 parent 513039d commit 42445df

File tree

5 files changed

+288
-738
lines changed

5 files changed

+288
-738
lines changed

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -96,7 +96,8 @@ private function getCommonParams($config, $userId, $attributes)
9696
REVISION => $config->getRevision(),
9797
CLIENT_ENGINE => self::SDK_TYPE,
9898
CLIENT_VERSION => self::SDK_VERSION,
99-
ANONYMIZE_IP => $config->getAnonymizeIP()
99+
ANONYMIZE_IP => $config->getAnonymizeIP(),
100+
ENRICH_DECISIONS => true
100101
];
101102

102103
if(!is_null($attributes)) {
@@ -170,38 +171,21 @@ private function getImpressionParams(Experiment $experiment, $variationId)
170171
/**
171172
* Helper function to get parameters specific to conversion event.
172173
*
173-
* @param $config ProjectConfig Configuration for the project.
174-
* @param $eventKey string Key representing the event.
175-
* @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into.
174+
* @param $eventEntity Event representing event entity.
176175
* @param $eventTags array Hash representing metadata associated with the event.
177176
*
178177
* @return array Hash representing parameters particular to conversion event.
179178
*/
180-
private function getConversionParams($config, $eventKey, $experimentVariationMap, $eventTags)
179+
private function getConversionParams($eventEntity, $eventTags)
181180
{
182181
$conversionParams = [];
183182
$singleSnapshot = [];
184-
$decisions = [];
185-
186-
foreach ($experimentVariationMap as $experimentId => $variationId) {
187-
188-
189-
$experiment = $config->getExperimentFromId($experimentId);
190-
$eventEntity = $config->getEvent($eventKey);
191-
192-
$decision = [
193-
CAMPAIGN_ID => $experiment->getLayerId(),
194-
EXPERIMENT_ID => $experiment->getId(),
195-
VARIATION_ID => $variationId
196-
];
197-
$decisions [] = $decision;
198-
}
199183

200184
$eventDict = [
201185
ENTITY_ID => $eventEntity->getId(),
202186
TIMESTAMP => time()*1000,
203187
UUID => GeneratorUtils::getRandomUuid(),
204-
KEY => $eventKey
188+
KEY => $eventEntity->getKey()
205189
];
206190

207191
if (!is_null($eventTags)) {
@@ -217,10 +201,9 @@ private function getConversionParams($config, $eventKey, $experimentVariationMap
217201

218202
if(count($eventTags) > 0) {
219203
$eventDict['tags'] = $eventTags;
220-
}
204+
}
221205
}
222206

223-
$singleSnapshot[DECISIONS] = $decisions;
224207
$singleSnapshot[EVENTS] [] = $eventDict;
225208
$conversionParams [] = $singleSnapshot;
226209

@@ -256,17 +239,17 @@ public function createImpressionEvent($config, $experimentKey, $variationKey, $u
256239
*
257240
* @param $config ProjectConfig Configuration for the project.
258241
* @param $eventKey string Key representing the event.
259-
* @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into.
260242
* @param $userId string ID of user.
261243
* @param $attributes array Attributes of the user.
262244
* @param $eventTags array Hash representing metadata associated with the event.
263245
*
264246
* @return LogEvent Event object to be sent to dispatcher.
265247
*/
266-
public function createConversionEvent($config, $eventKey, $experimentVariationMap, $userId, $attributes, $eventTags)
248+
public function createConversionEvent($config, $eventKey, $userId, $attributes, $eventTags)
267249
{
268250
$eventParams = $this->getCommonParams($config, $userId, $attributes);
269-
$conversionParams = $this->getConversionParams($config, $eventKey, $experimentVariationMap, $eventTags);
251+
$eventEntity = $config->getEvent($eventKey);
252+
$conversionParams = $this->getConversionParams($eventEntity, $eventTags);
270253

271254
$eventParams[VISITORS][0][SNAPSHOTS] = $conversionParams;
272255
return new LogEvent(self::$ENDPOINT, $eventParams, self::$HTTP_VERB, self::$HTTP_HEADERS);

src/Optimizely/Event/Builder/Params.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
define('CLIENT_VERSION', 'client_version');
2525
define('CUSTOM_ATTRIBUTE_FEATURE_TYPE', 'custom');
2626
define('DECISIONS', 'decisions');
27+
define('ENRICH_DECISIONS', 'enrich_decisions');
2728
define('ENTITY_ID', 'entity_id');
2829
define('EVENTS', 'events');;
2930
define('EXPERIMENT_ID', 'experiment_id');

src/Optimizely/Optimizely.php

Lines changed: 44 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -192,43 +192,6 @@ private function validateUserInputs($attributes, $eventTags = null)
192192
return true;
193193
}
194194

195-
/**
196-
* Get the experiments that we should be tracking for the given event. A valid experiment
197-
* is one that is in "Running" state and into which the user has been bucketed.
198-
*
199-
* @param $event string Event key representing the event which needs to be recorded.
200-
* @param $user string ID for user.
201-
* @param $attributes array Attributes of the user.
202-
*
203-
* @return Array Of objects where each object contains the ID of the experiment to track and the ID of the variation the user is bucketed into.
204-
*/
205-
private function getValidExperimentsForEvent($event, $userId, $attributes = null)
206-
{
207-
$validExperiments = [];
208-
foreach ($event->getExperimentIds() as $experimentId) {
209-
$experiment = $this->_config->getExperimentFromId($experimentId);
210-
$experimentKey = $experiment->getKey();
211-
$variationKey = $this->getVariation($experimentKey, $userId, $attributes);
212-
213-
if (is_null($variationKey)) {
214-
$this->_logger->log(
215-
Logger::INFO,
216-
sprintf(
217-
'Not tracking user "%s" for experiment "%s".',
218-
$userId,
219-
$experimentKey
220-
)
221-
);
222-
continue;
223-
}
224-
225-
$variation = $this->_config->getVariationFromKey($experimentKey, $variationKey);
226-
$validExperiments[$experimentId] = $variation->getId();
227-
}
228-
229-
return $validExperiments;
230-
}
231-
232195
/**
233196
* @param string Experiment key
234197
* @param string Variation key
@@ -350,68 +313,59 @@ public function track($eventKey, $userId, $attributes = null, $eventTags = null)
350313
$event = $this->_config->getEvent($eventKey);
351314

352315
if (is_null($event->getKey())) {
353-
$this->_logger->log(Logger::ERROR, sprintf('Not tracking user "%s" for event "%s".', $userId, $eventKey));
316+
$this->_logger->log(Logger::INFO, sprintf('Not tracking user "%s" for event "%s".', $userId, $eventKey));
354317
return;
355318
}
356319

357-
// Filter out experiments that are not running or when user(s) do not meet conditions.
358-
$experimentVariationMap = $this->getValidExperimentsForEvent($event, $userId, $attributes);
359-
if (!empty($experimentVariationMap)) {
360-
$conversionEvent = $this->_eventBuilder
361-
->createConversionEvent(
362-
$this->_config,
363-
$eventKey,
364-
$experimentVariationMap,
365-
$userId,
366-
$attributes,
367-
$eventTags
368-
);
369-
$this->_logger->log(Logger::INFO, sprintf('Tracking event "%s" for user "%s".', $eventKey, $userId));
370-
$this->_logger->log(
371-
Logger::DEBUG,
372-
sprintf(
373-
'Dispatching conversion event to URL %s with params %s.',
374-
$conversionEvent->getUrl(),
375-
json_encode($conversionEvent->getParams())
376-
)
320+
$conversionEvent = $this->_eventBuilder
321+
->createConversionEvent(
322+
$this->_config,
323+
$eventKey,
324+
$userId,
325+
$attributes,
326+
$eventTags
377327
);
328+
329+
$this->_logger->log(Logger::INFO, sprintf('Tracking event "%s" for user "%s".', $eventKey, $userId));
330+
$this->_logger->log(
331+
Logger::DEBUG,
332+
sprintf(
333+
'Dispatching conversion event to URL %s with params %s.',
334+
$conversionEvent->getUrl(),
335+
json_encode($conversionEvent->getParams())
336+
)
337+
);
378338

379-
try {
380-
$this->_eventDispatcher->dispatchEvent($conversionEvent);
381-
} catch (Throwable $exception) {
382-
$this->_logger->log(
383-
Logger::ERROR,
384-
sprintf(
385-
'Unable to dispatch conversion event. Error %s',
386-
$exception->getMessage()
387-
)
388-
);
389-
} catch (Exception $exception) {
390-
$this->_logger->log(
391-
Logger::ERROR,
392-
sprintf(
393-
'Unable to dispatch conversion event. Error %s',
394-
$exception->getMessage()
395-
)
396-
);
397-
}
398-
399-
$this->notificationCenter->sendNotifications(
400-
NotificationType::TRACK,
401-
array(
402-
$eventKey,
403-
$userId,
404-
$attributes,
405-
$eventTags,
406-
$conversionEvent
339+
try {
340+
$this->_eventDispatcher->dispatchEvent($conversionEvent);
341+
} catch (Throwable $exception) {
342+
$this->_logger->log(
343+
Logger::ERROR,
344+
sprintf(
345+
'Unable to dispatch conversion event. Error %s',
346+
$exception->getMessage()
407347
)
408348
);
409-
} else {
349+
} catch (Exception $exception) {
410350
$this->_logger->log(
411-
Logger::INFO,
412-
sprintf('There are no valid experiments for event "%s" to track.', $eventKey)
351+
Logger::ERROR,
352+
sprintf(
353+
'Unable to dispatch conversion event. Error %s',
354+
$exception->getMessage()
355+
)
413356
);
414357
}
358+
359+
$this->notificationCenter->sendNotifications(
360+
NotificationType::TRACK,
361+
array(
362+
$eventKey,
363+
$userId,
364+
$attributes,
365+
$eventTags,
366+
$conversionEvent
367+
)
368+
);
415369
}
416370

417371
/**

0 commit comments

Comments
 (0)