Skip to content

Commit e3eef7c

Browse files
Release 1.1.0
2 parents d90fddc + f028b5d commit e3eef7c

19 files changed

+605
-68
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.1.0
2+
- Updated to send datafile revision information in log events.
3+
- Gracefully handle empty entity IDs.
4+
- Added event tags to track API to allow users to pass in event metadata.
5+
- Deprecated the `eventValue` parameter from the track method. Should use `eventTags` to pass in event value instead.
6+
- Relaxed restriction on monolog package.
7+
18
## 1.0.1
29
- Updated to support more versions of json-schema package.
310

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#Contributing to the Optimizely PHP SDK
1+
# Contributing to the Optimizely PHP SDK
22
We welcome contributions and feedback! All contributors must sign our [Contributor License Agreement (CLA)](https://docs.google.com/a/optimizely.com/forms/d/e/1FAIpQLSf9cbouWptIpMgukAKZZOIAhafvjFCV8hS00XJLWQnWDFtwtA/viewform) to be eligible to contribute. Please read the [README](README.md) to set up your development environment, then read the guidelines below for information on submitting your code.
33

4-
##Development process
4+
## Development process
55

66
1. Create a branch off of `devel`: `git checkout -b YOUR_NAME/branch_name`.
77
2. Commit your changes. Make sure to add tests!
@@ -10,12 +10,12 @@ We welcome contributions and feedback! All contributors must sign our [Contribut
1010
5. Open a pull request from `YOUR_NAME/branch_name` to `devel`.
1111
6. A repository maintainer will review your pull request and, if all goes well, merge it!
1212

13-
##Pull request acceptance criteria
13+
## Pull request acceptance criteria
1414

1515
* **All code must have test coverage.** We use PHPUnit. Changes in functionality should have accompanying unit tests. Bug fixes should have accompanying regression tests.
1616
* Tests are located in `tests` with one file per class.
1717

18-
##License
18+
## License
1919

2020
All contributions are under the CLA mentioned above. For this project, Optimizely uses the Apache 2.0 license, and so asks that by contributing your code, you agree to license your contribution under the terms of the [Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0). Your contributions should also include the following header:
2121

@@ -39,5 +39,5 @@ All contributions are under the CLA mentioned above. For this project, Optimizel
3939

4040
The YEAR above should be the year of the contribution. If work on the file has been done over multiple years, list each year in the section above. Example: Optimizely writes the file and releases it in 2014. No changes are made in 2015. Change made in 2016. YEAR should be “2014, 2016”.
4141

42-
##Contact
42+
## Contact
4343
If you have questions, please contact [email protected].

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
#Optimizely PHP SDK
1+
# Optimizely PHP SDK
22
[![Build Status](https://travis-ci.org/optimizely/php-sdk.svg?branch=master)](https://travis-ci.org/optimizely/php-sdk)
33
[![Coverage Status](https://coveralls.io/repos/github/optimizely/php-sdk/badge.svg?branch=master)](https://coveralls.io/github/optimizely/php-sdk?branch=master)
44
[![Total Downloads](https://poser.pugx.org/optimizely/optimizely-sdk/downloads)](https://packagist.org/packages/optimizely/optimizely-sdk)
55
[![Apache 2.0](https://img.shields.io/github/license/nebula-plugins/gradle-extra-configurations-plugin.svg)](http://www.apache.org/licenses/LICENSE-2.0)
66

77
This repository houses the PHP SDK for Optimizely Full Stack.
88

9-
##Getting Started
9+
## Getting Started
1010

11-
###Installing the SDK
11+
### Installing the SDK
1212

1313
The Optimizely PHP SDK can be installed through [Composer](https://getcomposer.org/). Please use the following command:
1414

1515
```
1616
php composer.phar require optimizely/optimizely-sdk
1717
```
1818

19-
###Using the SDK
19+
### Using the SDK
2020
See the Optimizely Full Stack [developer documentation](https://developers.optimizely.com/x/solutions/sdks/reference/?language=php) to learn how to set up your first Full Stack project and use the SDK.
2121

22-
##Development
22+
## Development
2323

24-
###Unit tests
24+
### Unit tests
2525

26-
#####Running all tests
26+
##### Running all tests
2727
You can run all unit tests with:
2828

2929
```
3030
./vendor/bin/phpunit
3131
```
3232

33-
###Contributing
33+
### Contributing
3434

3535
Please see [CONTRIBUTING](CONTRIBUTING.md).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"justinrainbow/json-schema": "^1.6 || ^2.0 || ^4.0",
1515
"lastguest/murmurhash": "1.3.0",
1616
"guzzlehttp/guzzle": "~5.3|~6.2",
17-
"monolog/monolog": "1.21.0"
17+
"monolog/monolog": "~1.21"
1818
},
1919
"require-dev": {
2020
"phpunit/phpunit": "~4.8|~5.0",

src/Optimizely/Bucketer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, Optimizely
3+
* Copyright 2016-2017, 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.
@@ -92,7 +92,7 @@ protected function generateBucketValue($bucketingId)
9292
* @param $parentId mixed ID representing Experiment or Group.
9393
* @param $trafficAllocations array Traffic allocations for variation or experiment.
9494
*
95-
* @returns string ID representing experiment or variation.
95+
* @return string ID representing experiment or variation.
9696
*/
9797
private function findBucket($userId, $parentId, $trafficAllocations)
9898
{
@@ -150,7 +150,7 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
150150
}
151151

152152
$userExperimentId = $this->findBucket($userId, $group->getId(), $group->getTrafficAllocation());
153-
if (is_null($userExperimentId)) {
153+
if (empty($userExperimentId)) {
154154
$this->_logger->log(Logger::INFO, sprintf('User "%s" is in no experiment.', $userId));
155155
return new Variation();
156156
}
@@ -172,7 +172,7 @@ public function bucket(ProjectConfig $config, Experiment $experiment, $userId)
172172

173173
// Bucket user if not in whitelist and in group (if any).
174174
$variationId = $this->findBucket($userId, $experiment->getId(), $experiment->getTrafficAllocation());
175-
if (!is_null($variationId)) {
175+
if (!empty($variationId)) {
176176
$variation = $config->getVariationFromId($experiment->getKey(), $variationId);
177177
$this->_logger->log(Logger::INFO,
178178
sprintf('User "%s" is in variation %s of experiment %s.',

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, Optimizely
3+
* Copyright 2016-2017, 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.
@@ -22,6 +22,7 @@
2222
use Optimizely\Entity\Experiment;
2323
use Optimizely\Event\LogEvent;
2424
use Optimizely\ProjectConfig;
25+
use Optimizely\Utils\EventTagUtils;
2526

2627
class EventBuilder
2728
{
@@ -33,7 +34,7 @@ class EventBuilder
3334
/**
3435
* @const string Version of the Optimizely PHP SDK.
3536
*/
36-
const SDK_VERSION = '1.0.1';
37+
const SDK_VERSION = '1.1.0';
3738

3839
/**
3940
* @var string URL to send impression event to.
@@ -104,6 +105,7 @@ private function setCommonParams($config, $userId, $attributes)
104105
{
105106
$this->_eventParams[PROJECT_ID] = $config->getProjectId();
106107
$this->_eventParams[ACCOUNT_ID] = $config->getAccountId();
108+
$this->_eventParams[REVISION] = $config->getRevision();
107109
$this->_eventParams[VISITOR_ID] = $userId;
108110
$this->_eventParams[CLIENT_ENGINE] = self::SDK_TYPE;
109111
$this->_eventParams[CLIENT_VERSION] = self::SDK_VERSION;
@@ -153,20 +155,34 @@ private function setImpressionParams(Experiment $experiment, $variationId)
153155
* @param $eventKey string Key representing the event.
154156
* @param $experiments array Experiments for which conversion event needs to be recorded.
155157
* @param $userId string ID of user.
156-
* @param $eventValue integer Value associated with the event.
158+
* @param $eventTags array Hash representing metadata associated with the event.
157159
*/
158-
private function setConversionParams($config, $eventKey, $experiments, $userId, $eventValue)
160+
private function setConversionParams($config, $eventKey, $experiments, $userId, $eventTags)
159161
{
160162
$this->_eventParams[EVENT_FEATURES] = [];
161163
$this->_eventParams[EVENT_METRICS] = [];
162164

163-
if (!is_null($eventValue)) {
164-
$this->_eventParams[EVENT_METRICS] = [
165-
[
166-
'name' => 'revenue',
167-
'value' => $eventValue
168-
]
169-
];
165+
if (!is_null($eventTags)) {
166+
forEach ($eventTags as $eventTagId => $eventTagValue) {
167+
if (is_null($eventTagValue)) {
168+
continue;
169+
}
170+
$eventFeature = array(
171+
'name' => $eventTagId,
172+
'type' => 'custom',
173+
'value' => $eventTagValue,
174+
'shouldIndex' => false,
175+
);
176+
array_push($this->_eventParams[EVENT_FEATURES], $eventFeature);
177+
}
178+
$eventValue = EventTagUtils::getRevenueValue($eventTags);
179+
if ($eventValue) {
180+
$eventMetric = array(
181+
'name' => EventTagUtils::REVENUE_EVENT_METRIC_NAME,
182+
'value' => $eventValue,
183+
);
184+
array_push($this->_eventParams[EVENT_METRICS], $eventMetric);
185+
}
170186
}
171187

172188
$eventEntity = $config->getEvent($eventKey);
@@ -180,6 +196,7 @@ private function setConversionParams($config, $eventKey, $experiments, $userId,
180196
array_push($this->_eventParams[LAYER_STATES], [
181197
LAYER_ID => $experiment->getLayerId(),
182198
ACTION_TRIGGERED => true,
199+
REVISION => $config->getRevision(),
183200
DECISION => [
184201
EXPERIMENT_ID => $experiment->getId(),
185202
VARIATION_ID => $variation->getId(),
@@ -218,15 +235,15 @@ public function createImpressionEvent($config, Experiment $experiment, $variatio
218235
* @param $experiments array Experiments for which conversion event needs to be recorded.
219236
* @param $userId string ID of user.
220237
* @param $attributes array Attributes of the user.
221-
* @param $eventValue integer Value associated with the event.
238+
* @param $eventTags array Hash representing metadata associated with the event.
222239
*
223240
* @return LogEvent Event object to be sent to dispatcher.
224241
*/
225-
public function createConversionEvent($config, $eventKey, $experiments, $userId, $attributes, $eventValue)
242+
public function createConversionEvent($config, $eventKey, $experiments, $userId, $attributes, $eventTags)
226243
{
227244
$this->resetParams();
228245
$this->setCommonParams($config, $userId, $attributes);
229-
$this->setConversionParams($config, $eventKey, $experiments, $userId, $eventValue);
246+
$this->setConversionParams($config, $eventKey, $experiments, $userId, $eventTags);
230247

231248
return new LogEvent(self::$CONVERSION_ENDPOINT, $this->getParams(), self::$HTTP_VERB, self::$HTTP_HEADERS);
232249
}

src/Optimizely/Event/Builder/Params.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
define('ACCOUNT_ID', 'accountId');
1919
define('PROJECT_ID', 'projectId');
20+
define('REVISION', 'revision');
2021
define('LAYER_ID', 'layerId');
2122
define('EXPERIMENT_ID', 'experimentId');
2223
define('VARIATION_ID', 'variationId');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright 2017, 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\Exceptions;
19+
20+
21+
class InvalidEventTagException extends OptimizelyException
22+
{
23+
}

src/Optimizely/Optimizely.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, Optimizely
3+
* Copyright 2016-2017, 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.
@@ -18,6 +18,7 @@
1818

1919
use Exception;
2020
use Optimizely\Exceptions\InvalidAttributeException;
21+
use Optimizely\Exceptions\InvalidEventTagException;
2122
use Throwable;
2223
use Monolog\Logger;
2324
use Optimizely\Entity\Experiment;
@@ -29,6 +30,7 @@
2930
use Optimizely\Event\Dispatcher\EventDispatcherInterface;
3031
use Optimizely\Logger\LoggerInterface;
3132
use Optimizely\Logger\NoOpLogger;
33+
use Optimizely\Utils\EventTagUtils;
3234
use Optimizely\Utils\Validator;
3335

3436
/**
@@ -240,9 +242,9 @@ public function activate($experimentKey, $userId, $attributes = null)
240242
* @param $eventKey string Event key representing the event which needs to be recorded.
241243
* @param $userId string ID for user.
242244
* @param $attributes array Attributes of the user.
243-
* @param $eventValue integer Value associated with event.
245+
* @param $eventTags array Hash representing metadata associated with the event.
244246
*/
245-
public function track($eventKey, $userId, $attributes = null, $eventValue = null)
247+
public function track($eventKey, $userId, $attributes = null, $eventTags = null)
246248
{
247249
if (!$this->_isValid) {
248250
$this->_logger->log(Logger::ERROR, 'Datafile has invalid format. Failing "track".');
@@ -257,6 +259,24 @@ public function track($eventKey, $userId, $attributes = null, $eventValue = null
257259
return;
258260
}
259261

262+
if (!is_null($eventTags)) {
263+
if (is_numeric($eventTags) && !is_string($eventTags)) {
264+
$eventTags = array(
265+
EventTagUtils::REVENUE_EVENT_METRIC_NAME => $eventTags,
266+
);
267+
$this->_logger->log(
268+
Logger::WARNING,
269+
'Event value is deprecated in track call. Use event tags to pass in revenue value instead.'
270+
);
271+
}
272+
if (!Validator::areEventTagsValid($eventTags)) {
273+
$this->_logger->log(Logger::ERROR, 'Provided event tags are in an invalid format.');
274+
$this->_errorHandler->handleError(
275+
new InvalidEventTagException('Provided event tags are in an invalid format.')
276+
);
277+
}
278+
}
279+
260280
$event = $this->_config->getEvent($eventKey);
261281

262282
if (is_null($event->getKey())) {
@@ -284,7 +304,7 @@ public function track($eventKey, $userId, $attributes = null, $eventValue = null
284304
$validExperiments,
285305
$userId,
286306
$attributes,
287-
$eventValue
307+
$eventTags
288308
);
289309
$this->_logger->log(Logger::INFO, sprintf('Tracking event "%s" for user "%s".', $eventKey, $userId));
290310
$this->_logger->log(

src/Optimizely/ProjectConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ public function getProjectId()
184184
return $this->_projectId;
185185
}
186186

187+
/**
188+
* @return string String representing revision of the datafile.
189+
*/
190+
public function getRevision()
191+
{
192+
return $this->_revision;
193+
}
194+
187195
/**
188196
* @param $groupId string ID of the group.
189197
*

0 commit comments

Comments
 (0)