Skip to content

Commit c687e53

Browse files
authored
feat(Decide): Add Decide API (#220)
Added new APIs to support the decide feature. Introduced a new OptimizelyUserContext class through createUserContext class api. This creates an optimizely instance with memoized user context and exposes the following APIs 1. setAttribute 2. decide 3. decideAll 4. decideForKeys 5. trackEvent
1 parent 5f73a01 commit c687e53

19 files changed

+3078
-399
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ php:
66
- '7.0'
77
- '7.1'
88
- '7.2'
9-
- '7.3'
9+
- '7.3.24' # revert it back to 7.3 once 7.3.25 latest build gets fixed.
1010
install: "composer install"
1111
script:
1212
- mkdir -p build/logs

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^4.8|^5.0",
26-
"php-coveralls/php-coveralls": "v2.0.0",
26+
"php-coveralls/php-coveralls": "v2.3.0",
2727
"squizlabs/php_codesniffer": "3.*",
2828
"icecave/parity": "^1.0 || ^2.0"
2929
},

src/Optimizely/Bucketer.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2020, Optimizely
3+
* Copyright 2016-2021, 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.
@@ -110,23 +110,26 @@ protected function generateBucketValue($bucketingKey)
110110
* @param $parentId mixed ID representing Experiment or Group.
111111
* @param $trafficAllocations array Traffic allocations for variation or experiment.
112112
*
113-
* @return string ID representing experiment or variation.
113+
* @return [ string, array ] ID representing experiment or variation and array of log messages representing decision making.
114114
*/
115115
private function findBucket($bucketingId, $userId, $parentId, $trafficAllocations)
116116
{
117+
$decideReasons = [];
117118
// Generate the bucketing key based on combination of user ID and experiment ID or group ID.
118119
$bucketingKey = $bucketingId.$parentId;
119120
$bucketingNumber = $this->generateBucketValue($bucketingKey);
120-
$this->_logger->log(Logger::DEBUG, sprintf('Assigned bucket %s to user "%s" with bucketing ID "%s".', $bucketingNumber, $userId, $bucketingId));
121+
$message = sprintf('Assigned bucket %s to user "%s" with bucketing ID "%s".', $bucketingNumber, $userId, $bucketingId);
122+
$this->_logger->log(Logger::DEBUG, $message);
123+
$decideReasons[] = $message;
121124

122125
foreach ($trafficAllocations as $trafficAllocation) {
123126
$currentEnd = $trafficAllocation->getEndOfRange();
124127
if ($bucketingNumber < $currentEnd) {
125-
return $trafficAllocation->getEntityId();
128+
return [$trafficAllocation->getEntityId(), $decideReasons];
126129
}
127130
}
128131

129-
return null;
132+
return [null, $decideReasons];
130133
}
131134

132135
/**
@@ -137,12 +140,14 @@ private function findBucket($bucketingId, $userId, $parentId, $trafficAllocation
137140
* @param $bucketingId string A customer-assigned value used to create the key for the murmur hash.
138141
* @param $userId string User identifier.
139142
*
140-
* @return Variation Variation which will be shown to the user.
143+
* @return [ Variation, array ] Variation which will be shown to the user and array of log messages representing decision making.
141144
*/
142145
public function bucket(ProjectConfigInterface $config, Experiment $experiment, $bucketingId, $userId)
143146
{
147+
$decideReasons = [];
148+
144149
if (is_null($experiment->getKey())) {
145-
return null;
150+
return [ null, $decideReasons ];
146151
}
147152

148153
// Determine if experiment is in a mutually exclusive group.
@@ -151,47 +156,52 @@ public function bucket(ProjectConfigInterface $config, Experiment $experiment, $
151156
$group = $config->getGroup($experiment->getGroupId());
152157

153158
if (is_null($group->getId())) {
154-
return null;
159+
return [ null, $decideReasons ];
155160
}
156161

157-
$userExperimentId = $this->findBucket($bucketingId, $userId, $group->getId(), $group->getTrafficAllocation());
162+
list($userExperimentId, $reasons) = $this->findBucket($bucketingId, $userId, $group->getId(), $group->getTrafficAllocation());
163+
$decideReasons = array_merge($decideReasons, $reasons);
164+
158165
if (empty($userExperimentId)) {
159-
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no experiment.', $userId));
160-
return null;
166+
$message = sprintf('User "%s" is in no experiment.', $userId);
167+
$this->_logger->log(Logger::INFO, $message);
168+
$decideReasons[] = $message;
169+
return [ null, $decideReasons ];
161170
}
162171

163172
if ($userExperimentId != $experiment->getId()) {
164-
$this->_logger->log(
165-
Logger::INFO,
166-
sprintf(
167-
'User "%s" is not in experiment %s of group %s.',
168-
$userId,
169-
$experiment->getKey(),
170-
$experiment->getGroupId()
171-
)
172-
);
173-
return null;
174-
}
175-
176-
$this->_logger->log(
177-
Logger::INFO,
178-
sprintf(
179-
'User "%s" is in experiment %s of group %s.',
173+
$message = sprintf(
174+
'User "%s" is not in experiment %s of group %s.',
180175
$userId,
181176
$experiment->getKey(),
182177
$experiment->getGroupId()
183-
)
178+
);
179+
180+
$this->_logger->log(Logger::INFO, $message);
181+
$decideReasons[] = $message;
182+
return [ null, $decideReasons ];
183+
}
184+
185+
$message = sprintf(
186+
'User "%s" is in experiment %s of group %s.',
187+
$userId,
188+
$experiment->getKey(),
189+
$experiment->getGroupId()
184190
);
191+
192+
$this->_logger->log(Logger::INFO, $message);
193+
$decideReasons[] = $message;
185194
}
186195

187196
// Bucket user if not in whitelist and in group (if any).
188-
$variationId = $this->findBucket($bucketingId, $userId, $experiment->getId(), $experiment->getTrafficAllocation());
197+
list($variationId, $reasons) = $this->findBucket($bucketingId, $userId, $experiment->getId(), $experiment->getTrafficAllocation());
198+
$decideReasons = array_merge($decideReasons, $reasons);
189199
if (!empty($variationId)) {
190200
$variation = $config->getVariationFromId($experiment->getKey(), $variationId);
191201

192-
return $variation;
202+
return [ $variation, $decideReasons ];
193203
}
194204

195-
return null;
205+
return [ null, $decideReasons ];
196206
}
197207
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright 2021, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Optimizely\Decide;
19+
20+
class OptimizelyDecideOption
21+
{
22+
const DISABLE_DECISION_EVENT = 'DISABLE_DECISION_EVENT';
23+
const ENABLED_FLAGS_ONLY = 'ENABLED_FLAGS_ONLY';
24+
const IGNORE_USER_PROFILE_SERVICE = 'IGNORE_USER_PROFILE_SERVICE';
25+
const INCLUDE_REASONS = 'INCLUDE_REASONS';
26+
const EXCLUDE_VARIABLES = 'EXCLUDE_VARIABLES';
27+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright 2021, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Optimizely\Decide;
19+
20+
class OptimizelyDecision implements \JsonSerializable
21+
{
22+
private $variationKey;
23+
private $enabled;
24+
private $variables;
25+
private $ruleKey;
26+
private $flagKey;
27+
private $userContext;
28+
private $reasons;
29+
30+
31+
public function __construct(
32+
$variationKey = null,
33+
$enabled = null,
34+
$variables = null,
35+
$ruleKey = null,
36+
$flagKey = null,
37+
$userContext = null,
38+
$reasons = null
39+
) {
40+
$this->variationKey = $variationKey;
41+
$this->enabled = $enabled === null ? false : $enabled;
42+
$this->variables = $variables === null ? [] : $variables;
43+
$this->ruleKey = $ruleKey;
44+
$this->flagKey = $flagKey;
45+
$this->userContext = $userContext;
46+
$this->reasons = $reasons === null ? [] : $reasons;
47+
}
48+
49+
public function getVariationKey()
50+
{
51+
return $this->variationKey;
52+
}
53+
54+
public function getEnabled()
55+
{
56+
return $this->enabled;
57+
}
58+
59+
public function getVariables()
60+
{
61+
return $this->variables;
62+
}
63+
64+
public function getRuleKey()
65+
{
66+
return $this->ruleKey;
67+
}
68+
69+
public function getFlagKey()
70+
{
71+
return $this->flagKey;
72+
}
73+
74+
public function getUserContext()
75+
{
76+
return $this->userContext;
77+
}
78+
79+
public function getReasons()
80+
{
81+
return $this->reasons;
82+
}
83+
84+
public function jsonSerialize()
85+
{
86+
return get_object_vars($this);
87+
}
88+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright 2021, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Optimizely\Decide;
19+
20+
class OptimizelyDecisionMessage
21+
{
22+
const SDK_NOT_READY = 'Optimizely SDK not configured properly yet.';
23+
const FLAG_KEY_INVALID = 'No flag was found for key "%s".';
24+
const VARIABLE_VALUE_INVALID = 'Variable value for key "%s" is invalid or wrong type.';
25+
}

0 commit comments

Comments
 (0)