|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Oleg Krivtsov <[email protected]> |
|
4
|
|
|
* @date 03 October 2016 |
|
5
|
|
|
* @copyright (c) 2016, Web Marketing ROI |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace WebMarketingROI\OptimizelyPHP\Service\v2; |
|
8
|
|
|
|
|
9
|
|
|
use WebMarketingROI\OptimizelyPHP\Exception; |
|
10
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\Experiment; |
|
11
|
|
|
use WebMarketingROI\OptimizelyPHP\Resource\v2\ExperimentResults; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides methods for working with Optimizely experiments. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Experiments |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Optimizely API Client. |
|
20
|
|
|
* @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
|
21
|
|
|
*/ |
|
22
|
|
|
private $client; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
*/ |
|
27
|
12 |
|
public function __construct($client) |
|
28
|
|
|
{ |
|
29
|
12 |
|
$this->client = $client; |
|
30
|
12 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get a list of all the experiments by Project or Campaign |
|
34
|
|
|
* @param integer $projectId |
|
35
|
|
|
* @param integer $campaignId |
|
36
|
|
|
* @param boolean $includeClassic |
|
37
|
|
|
* @param integer $page |
|
38
|
|
|
* @param integer $perPage |
|
39
|
|
|
* @return Result |
|
40
|
|
|
* @throws Exception |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function listAll($projectId, $campaignId=null, $includeClassic=false, $page=1, $perPage=25) |
|
43
|
|
|
{ |
|
44
|
4 |
|
if ($projectId==null && $campaignId==null) { |
|
|
|
|
|
|
45
|
1 |
|
throw new Exception('Project ID or Campaign ID must be non-null'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
if ($page<0) { |
|
49
|
1 |
|
throw new Exception('Invalid page number passed'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
if ($perPage<0 || $perPage>100) { |
|
53
|
1 |
|
throw new Exception('Invalid page size passed'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
$result = $this->client->sendApiRequest('/experiments', |
|
57
|
|
|
array( |
|
58
|
1 |
|
'project_id'=>$projectId, |
|
59
|
1 |
|
'campaign_id'=>$campaignId, |
|
60
|
1 |
|
'include_classic'=>$includeClassic, |
|
61
|
1 |
|
'page'=>$page, |
|
62
|
1 |
|
'per_page'=>$perPage |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
1 |
|
$experiments = array(); |
|
66
|
1 |
|
foreach ($result->getDecodedJsonData() as $experimentInfo) { |
|
67
|
1 |
|
$experiment = new Experiment($experimentInfo); |
|
68
|
1 |
|
$experiments[] = $experiment; |
|
69
|
|
|
} |
|
70
|
1 |
|
$result->setPayload($experiments); |
|
71
|
|
|
|
|
72
|
1 |
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get metadata for a single Experiment. |
|
77
|
|
|
* @param integer $experimentId |
|
78
|
|
|
* @return Result |
|
79
|
|
|
* @throws Exception |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function get($experimentId) |
|
82
|
|
|
{ |
|
83
|
3 |
|
if (!is_int($experimentId)) { |
|
84
|
1 |
|
throw new Exception("Integer experiment ID expected, while got '$experimentId'", |
|
85
|
1 |
|
Exception::CODE_INVALID_ARG); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
if ($experimentId<0) { |
|
89
|
1 |
|
throw new Exception("A positive experiment ID expected"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$result = $this->client->sendApiRequest("/experiments/$experimentId"); |
|
93
|
|
|
|
|
94
|
1 |
|
$experiment = new Experiment($result->getDecodedJsonData()); |
|
95
|
1 |
|
$result->setPayload($experiment); |
|
96
|
|
|
|
|
97
|
1 |
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get results for a single experiment |
|
102
|
|
|
* @param integer $experimentId The id for the experiment you want results for |
|
103
|
|
|
* @param integer $baselineVariationId The id of the variation to use as the baseline to compare against other variations. Defaults to the first variation if not provided. |
|
104
|
|
|
* @param string $startTime The earliest time to count events in results. Defaults to the time that the experiment was first activated. |
|
105
|
|
|
* @param string $endTime The latest time to count events in results. Defaults to the time the experiment was last active or the current time if the experiment is still running. |
|
106
|
|
|
* @return Result |
|
107
|
|
|
* @throws Exception |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function getResults($experimentId, $baselineVariationId = null, $startTime = null, $endTime = null) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$result = $this->client->sendApiRequest("/experiments/$experimentId/results", |
|
112
|
|
|
array( |
|
113
|
1 |
|
'baseline_variation_id' => $baselineVariationId, |
|
114
|
1 |
|
'start_time' => $startTime, |
|
115
|
1 |
|
'end_time' => $endTime |
|
116
|
|
|
)); |
|
117
|
|
|
|
|
118
|
1 |
|
$experimentResults = new ExperimentResults($result->getDecodedJsonData()); |
|
119
|
1 |
|
$result->setPayload($experimentResults); |
|
120
|
|
|
|
|
121
|
1 |
|
return $result; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Create an experiment in a Project. |
|
126
|
|
|
* @param Experiment $experiment |
|
127
|
|
|
* @param string $action Action to change the state of the Experiment. |
|
128
|
|
|
* @return Result |
|
129
|
|
|
* @throw Exception |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function create($experiment, $action = null) |
|
132
|
|
|
{ |
|
133
|
1 |
|
if (!($experiment instanceOf Experiment)) { |
|
134
|
|
|
throw new Exception("Expected argument of type Experiment", |
|
135
|
|
|
Exception::CODE_INVALID_ARG); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
if ($action!=null && !is_string($action)) { |
|
|
|
|
|
|
139
|
|
|
throw new Exception("Expected string or null action argument", |
|
140
|
|
|
Exception::CODE_INVALID_ARG); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
$queryParams = array(); |
|
144
|
1 |
|
if ($action!=null) $queryParams['action'] = $action; |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
1 |
|
$postData = $experiment->toArray(); |
|
147
|
|
|
|
|
148
|
1 |
|
$result = $this->client->sendApiRequest("/experiments", $queryParams, 'POST', |
|
149
|
1 |
|
$postData); |
|
150
|
|
|
|
|
151
|
1 |
|
$experiment = new Experiment($result->getDecodedJsonData()); |
|
152
|
1 |
|
$result->setPayload($experiment); |
|
153
|
|
|
|
|
154
|
1 |
|
return $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Update an Experiment by ID |
|
159
|
|
|
* @param integer $experimentId |
|
160
|
|
|
* @param Experiment $experiment |
|
161
|
|
|
* @param string $action Action to change the state of the Experiment. |
|
162
|
|
|
* @return Result |
|
163
|
|
|
* @throws Exception |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function update($experimentId, $experiment, $action) |
|
166
|
|
|
{ |
|
167
|
1 |
|
if ($experimentId<0) { |
|
168
|
|
|
throw new Exception("Expected positive experiment ID argument"); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
if (!($experiment instanceOf Experiment)) { |
|
172
|
|
|
throw new Exception("Expected argument of type Experiment"); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$queryParams = array( |
|
176
|
1 |
|
'action' => $action |
|
177
|
|
|
); |
|
178
|
|
|
|
|
179
|
1 |
|
$postData = $experiment->toArray(); |
|
180
|
|
|
|
|
181
|
1 |
|
$result = $this->client->sendApiRequest("/experiments/$experimentId", $queryParams, 'PATCH', |
|
182
|
1 |
|
$postData); |
|
183
|
|
|
|
|
184
|
1 |
|
$experiment = new Experiment($result->getDecodedJsonData()); |
|
185
|
1 |
|
$result->setPayload($experiment); |
|
186
|
|
|
|
|
187
|
1 |
|
return $result; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Delete Experiment by ID |
|
192
|
|
|
* @param integer $experimentId |
|
193
|
|
|
* @return Result |
|
194
|
|
|
* @throws Exception |
|
195
|
|
|
*/ |
|
196
|
1 |
|
public function delete($experimentId) |
|
197
|
|
|
{ |
|
198
|
1 |
|
$result = $this->client->sendApiRequest("/experiments/$experimentId", array(), 'DELETE', |
|
199
|
1 |
|
array()); |
|
200
|
|
|
|
|
201
|
1 |
|
return $result; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
|