Skip to content

Commit

Permalink
Merge fee6525 into 513039d
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Jan 17, 2019
2 parents 513039d + fee6525 commit 05eb04b
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 743 deletions.
37 changes: 10 additions & 27 deletions src/Optimizely/Event/Builder/EventBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,7 +96,8 @@ private function getCommonParams($config, $userId, $attributes)
REVISION => $config->getRevision(),
CLIENT_ENGINE => self::SDK_TYPE,
CLIENT_VERSION => self::SDK_VERSION,
ANONYMIZE_IP => $config->getAnonymizeIP()
ANONYMIZE_IP => $config->getAnonymizeIP(),
ENRICH_DECISIONS => true
];

if(!is_null($attributes)) {
Expand Down Expand Up @@ -170,38 +171,21 @@ private function getImpressionParams(Experiment $experiment, $variationId)
/**
* Helper function to get parameters specific to conversion event.
*
* @param $config ProjectConfig Configuration for the project.
* @param $eventKey string Key representing the event.
* @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into.
* @param $eventEntity Event representing event entity.
* @param $eventTags array Hash representing metadata associated with the event.
*
* @return array Hash representing parameters particular to conversion event.
*/
private function getConversionParams($config, $eventKey, $experimentVariationMap, $eventTags)
private function getConversionParams($eventEntity, $eventTags)
{
$conversionParams = [];
$singleSnapshot = [];
$decisions = [];

foreach ($experimentVariationMap as $experimentId => $variationId) {


$experiment = $config->getExperimentFromId($experimentId);
$eventEntity = $config->getEvent($eventKey);

$decision = [
CAMPAIGN_ID => $experiment->getLayerId(),
EXPERIMENT_ID => $experiment->getId(),
VARIATION_ID => $variationId
];
$decisions [] = $decision;
}

$eventDict = [
ENTITY_ID => $eventEntity->getId(),
TIMESTAMP => time()*1000,
UUID => GeneratorUtils::getRandomUuid(),
KEY => $eventKey
KEY => $eventEntity->getKey()
];

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

if(count($eventTags) > 0) {
$eventDict['tags'] = $eventTags;
}
}
}

$singleSnapshot[DECISIONS] = $decisions;
$singleSnapshot[EVENTS] [] = $eventDict;
$conversionParams [] = $singleSnapshot;

Expand Down Expand Up @@ -256,17 +239,17 @@ public function createImpressionEvent($config, $experimentKey, $variationKey, $u
*
* @param $config ProjectConfig Configuration for the project.
* @param $eventKey string Key representing the event.
* @param $experimentVariationMap array Map of experiment ID to the ID of the variation that the user is bucketed into.
* @param $userId string ID of user.
* @param $attributes array Attributes of the user.
* @param $eventTags array Hash representing metadata associated with the event.
*
* @return LogEvent Event object to be sent to dispatcher.
*/
public function createConversionEvent($config, $eventKey, $experimentVariationMap, $userId, $attributes, $eventTags)
public function createConversionEvent($config, $eventKey, $userId, $attributes, $eventTags)
{
$eventParams = $this->getCommonParams($config, $userId, $attributes);
$conversionParams = $this->getConversionParams($config, $eventKey, $experimentVariationMap, $eventTags);
$eventEntity = $config->getEvent($eventKey);
$conversionParams = $this->getConversionParams($eventEntity, $eventTags);

$eventParams[VISITORS][0][SNAPSHOTS] = $conversionParams;
return new LogEvent(self::$ENDPOINT, $eventParams, self::$HTTP_VERB, self::$HTTP_HEADERS);
Expand Down
3 changes: 2 additions & 1 deletion src/Optimizely/Event/Builder/Params.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
define('CLIENT_VERSION', 'client_version');
define('CUSTOM_ATTRIBUTE_FEATURE_TYPE', 'custom');
define('DECISIONS', 'decisions');
define('ENRICH_DECISIONS', 'enrich_decisions');
define('ENTITY_ID', 'entity_id');
define('EVENTS', 'events');;
define('EXPERIMENT_ID', 'experiment_id');
Expand Down
144 changes: 49 additions & 95 deletions src/Optimizely/Optimizely.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2018, Optimizely
* Copyright 2016-2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -192,43 +192,6 @@ private function validateUserInputs($attributes, $eventTags = null)
return true;
}

/**
* Get the experiments that we should be tracking for the given event. A valid experiment
* is one that is in "Running" state and into which the user has been bucketed.
*
* @param $event string Event key representing the event which needs to be recorded.
* @param $user string ID for user.
* @param $attributes array Attributes of the user.
*
* @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.
*/
private function getValidExperimentsForEvent($event, $userId, $attributes = null)
{
$validExperiments = [];
foreach ($event->getExperimentIds() as $experimentId) {
$experiment = $this->_config->getExperimentFromId($experimentId);
$experimentKey = $experiment->getKey();
$variationKey = $this->getVariation($experimentKey, $userId, $attributes);

if (is_null($variationKey)) {
$this->_logger->log(
Logger::INFO,
sprintf(
'Not tracking user "%s" for experiment "%s".',
$userId,
$experimentKey
)
);
continue;
}

$variation = $this->_config->getVariationFromKey($experimentKey, $variationKey);
$validExperiments[$experimentId] = $variation->getId();
}

return $validExperiments;
}

/**
* @param string Experiment key
* @param string Variation key
Expand Down Expand Up @@ -350,68 +313,59 @@ public function track($eventKey, $userId, $attributes = null, $eventTags = null)
$event = $this->_config->getEvent($eventKey);

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

// Filter out experiments that are not running or when user(s) do not meet conditions.
$experimentVariationMap = $this->getValidExperimentsForEvent($event, $userId, $attributes);
if (!empty($experimentVariationMap)) {
$conversionEvent = $this->_eventBuilder
->createConversionEvent(
$this->_config,
$eventKey,
$experimentVariationMap,
$userId,
$attributes,
$eventTags
);
$this->_logger->log(Logger::INFO, sprintf('Tracking event "%s" for user "%s".', $eventKey, $userId));
$this->_logger->log(
Logger::DEBUG,
sprintf(
'Dispatching conversion event to URL %s with params %s.',
$conversionEvent->getUrl(),
json_encode($conversionEvent->getParams())
)
$conversionEvent = $this->_eventBuilder
->createConversionEvent(
$this->_config,
$eventKey,
$userId,
$attributes,
$eventTags
);

$this->_logger->log(Logger::INFO, sprintf('Tracking event "%s" for user "%s".', $eventKey, $userId));
$this->_logger->log(
Logger::DEBUG,
sprintf(
'Dispatching conversion event to URL %s with params %s.',
$conversionEvent->getUrl(),
json_encode($conversionEvent->getParams())
)
);

try {
$this->_eventDispatcher->dispatchEvent($conversionEvent);
} catch (Throwable $exception) {
$this->_logger->log(
Logger::ERROR,
sprintf(
'Unable to dispatch conversion event. Error %s',
$exception->getMessage()
)
);
} catch (Exception $exception) {
$this->_logger->log(
Logger::ERROR,
sprintf(
'Unable to dispatch conversion event. Error %s',
$exception->getMessage()
)
);
}

$this->notificationCenter->sendNotifications(
NotificationType::TRACK,
array(
$eventKey,
$userId,
$attributes,
$eventTags,
$conversionEvent
)
);
} else {
$this->_logger->log(
Logger::INFO,
sprintf('There are no valid experiments for event "%s" to track.', $eventKey)
);
}
try {
$this->_eventDispatcher->dispatchEvent($conversionEvent);
} catch (Throwable $exception) {
$this->_logger->log(
Logger::ERROR,
sprintf(
'Unable to dispatch conversion event. Error %s',
$exception->getMessage()
)
);
} catch (Exception $exception) {
$this->_logger->log(
Logger::ERROR,
sprintf(
'Unable to dispatch conversion event. Error %s',
$exception->getMessage()
)
);
}

$this->notificationCenter->sendNotifications(
NotificationType::TRACK,
array(
$eventKey,
$userId,
$attributes,
$eventTags,
$conversionEvent
)
);
}

/**
Expand Down

0 comments on commit 05eb04b

Please sign in to comment.