-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimizely.php
718 lines (626 loc) · 19.8 KB
/
optimizely.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
/*
* Version 0.2
*/
class Optimizely {
/**
* contains info from the all the HTTP requests
*/
public $request_info = array();
/**
* contains latest HTTP status code
*/
public $request_http_code;
/**
* contains latest requested URL
*/
public $request_url;
/**
* contains latest raw response
*/
public $request_response;
/**
* user agent to provide on requests
*/
protected $useragent = 'PHP Optimizely v0.2';
/**
* default timeout
*/
protected $timeout = 30;
/**
* default connect timeout
*/
protected $connecttimeout = 30;
/**
* Verify peer SSL Certificate?
*/
protected $ssl_verifypeer = FALSE;
/**
* Optimizely API token
*/
protected $api_token;
/**
* Optimizely API token type
*/
protected $token_type = 'default';
/**
* base url for API
*/
protected $api_url = 'https://www.optimizelyapis.com/experiment/v1/';
/**
* preferred date format for dates
*/
public $date_format = 'Y-m-d\TH:i:s\Z';
/**
* Setup the object
* @param string $api_token API token provided by Optimizely
* @param string $token_type Type of token to use
*/
public function __construct( $api_token, $token_type = 'default' ) {
$this->api_token = $api_token;
$this->token_type = $token_type;
}// end __construct
/**
* Allow late setting of API token
* @param string $api_token API token provided by Optimizely
*/
public function set_api_token( $api_token ) {
$this->api_token = $api_token;
}// end set_api_token
/**
* Uses curl to hit hit the API, override this function to use a different method
* @param array $options Options for request
*/
protected function request( $options ) {
if ( ! $this->api_token ) {
return FALSE;
}//end if
$c = curl_init();
/* Curl settings */
curl_setopt( $c, CURLOPT_USERAGENT, $this->useragent );
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout );
curl_setopt( $c, CURLOPT_TIMEOUT, $this->timeout );
curl_setopt( $c, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer );
curl_setopt( $c, CURLOPT_HEADER, FALSE );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, TRUE );
if ( $this->token_type === 'oauth' ) {
curl_setopt( $c, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $this->api_token,
'Content-Type: application/json'
) );
} else {
curl_setopt( $c, CURLOPT_HTTPHEADER, array(
'Token: ' . $this->api_token,
'Content-Type: application/json'
) );
}
$url = $this->api_url . $options['function'];
switch ( $options['method'] )
{
case 'POST':
curl_setopt( $c, CURLOPT_POST, TRUE );
curl_setopt( $c, CURLOPT_POSTFIELDS, json_encode( $options['data'] ) );
break;
case 'DELETE':
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'DELETE' );
break;
case 'PUT':
curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $c, CURLOPT_POSTFIELDS, json_encode( $options['data'] ) );
break;
case 'GET':
curl_setopt( $c, CURLOPT_HTTPGET, TRUE );
break;
}//END switch
curl_setopt( $c, CURLOPT_URL, $url );
$response = curl_exec( $c );
// the following variables are primarily for debugging purposes
$this->request_http_code = curl_getinfo( $c, CURLINFO_HTTP_CODE );
$this->request_info = curl_getinfo( $c );
$this->request_url = $url;
$this->request_response = $response;
curl_close( $c );
return json_decode( $response );
}// end request
/**
* Get a list of all the projects in your account, with associated metadata.
*/
public function get_projects() {
return $this->request( array(
'function' => 'projects',
'method' => 'GET',
) );
}// end get_projects
/**
* Get metadata for a single project.
* @param string|integer $project_id ID of project to get
*/
public function get_project( $project_id ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/',
'method' => 'GET',
) );
}// end get_project
/**
* Create a new project in your account. The project_name is required in the options.
* The other editable arguments are all optional.
* @param array $options Options for project
*/
public function create_project( $options ) {
if ( ! isset( $options['project_name'] ) ) {
return FALSE;
}//end if
$defaults = array(
'project_status' => 'Active',
'include_jquery' => FALSE,
'project_javascript' => null,
'enable_force_variation' => FALSE,
'exclude_disabled_experiments' => FALSE,
'exclude_names' => null,
'ip_anonymization' => FALSE,
'ip_filter' => null,
);
$options = array_replace( $defaults, $options );
return $this->request( array(
'function' => 'projects/',
'method' => 'POST',
'data' => $options,
) );
}// end create_project
/**
* update a project
* @param string|integer $project_id ID of project to get
* @param array $options Options for experiment
*/
public function update_project( $project_id, $options ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}//end update_project
/**
* Get a list of all the experiments in a project.
* @param string|integer $project_id ID of project to get the experiments from
*/
public function get_experiments( $project_id ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/experiments/',
'method' => 'GET',
) );
}// end get_experiments
/**
* Get metadata for a single experiment.
* @param integer $experiment_id ID of experiment to get
*/
public function get_experiment( $experiment_id ) {
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/',
'method' => 'GET',
) );
}// end get_experiment
/**
* Get the top-level results of an experiment, including number of visitors,
* number of conversions, and chance to beat baseline for each variation.
* @param string|integer $experiment_id ID of experiment to get the results for
* @param array $options Options for experiment
*/
public function get_experiment_results( $experiment_id, $options = array() ) {
// @TODO: support options
// @TODO: check for 503 in case this endpoint is overloaded (from docs)
$extra = '';
if ( $options ) {
$extra = '?' . http_build_query( $options );
}//end if
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/results' . $extra,
'method' => 'GET',
) );
}// end get_experiment_results
/**
* Get the top-level results (stats) of an experiment, including number of visitors,
* number of conversions, and chance to beat baseline for each variation.
* @param string|integer $experiment_id ID of experiment to get the results for
* @param array $options Options for experiment
*/
public function get_experiment_stats( $experiment_id, $options = array() ) {
// @TODO: support options
// @TODO: check for 503 in case this endpoint is overloaded (from docs)
$extra = '';
if ( $options ) {
$extra = '?' . http_build_query( $options );
}//end if
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/stats' . $extra,
'method' => 'GET',
) );
}// end get_experiment_stats
/**
* Creates a new experiment
* @param string|integer $project_id ID of project for the new experiment
* @param array $options Options for experiment
*/
public function create_experiment( $project_id, $options ) {
if ( ! isset( $options['description'] )
|| ! isset( $options['edit_url'] ) ) {
return FALSE;
}//end if
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/experiments/',
'method' => 'POST',
'data' => $options,
) );
}// end create_experiment
/**
* Update an experiment
* @param string|integer $experiment_id ID of experiment to update
* @param array $options Options for experiment
*/
public function update_experiment( $experiment_id, $options ) {
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_experiment
/**
* Delete an experiment
* @param string|integer $experiment_id ID of experiment to update
* @param boolean $archive Whether to archive or not
*/
public function delete_experiment( $experiment_id, $archive = TRUE ) {
if ( $archive ) {
return $this->update_experiment( $experiment_id, array( 'status' => 'Archived' ) );
}//end if
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ),
'method' => 'DELETE',
) );
}// end delete_experiment
/**
* See a list containing the current schedule for an experiment as well as any previously
* created schedules. The current schedule will be marked ACTIVE and any previously created schedules will be marked INACTIVE.
* @param string|integer $experiment_id ID of experiment to get schedules for
*/
public function get_schedules( $experiment_id ) {
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/schedules',
'method' => 'GET',
) );
}// end get_schedules
/**
* Get data about a particular schedule, including the start time and stop time of the associated experiment.
* @param string|integer $schedule_id ID of schedule to get
*/
public function get_schedule( $schedule_id ) {
return $this->request( array(
'function' => 'schedules/' . abs( intval( $schedule_id ) ),
'method' => 'GET',
) );
}// end get_schedule
/**
* Create a schedule for an experiment.
* @param string|integer $experiment_id ID of experiment to create schedule for
* @param array $options Options for schedule
*/
public function create_schedule( $experiment_id, $options ) {
// requires either start_time or end_time
if ( ! isset( $options['start_time'] )
&& ! isset( $options['stop_time'] ) ) {
return FALSE;
}//end if
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/schedules',
'method' => 'POST',
'data' => $options,
) );
}// end create_schedule
/**
* Update a schedule.
* @param string|integer $schedule_id ID of schedule to update
* @param array $options Options for schedule
*/
public function update_schedule( $schedule_id, $options ) {
// requires either start_time or end_time
if ( ! isset( $options['start_time'] )
&& ! isset( $options['stop_time'] ) ) {
return FALSE;
}//end if
return $this->request( array(
'function' => 'schedules/' . abs( intval( $schedule_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_schedule
/**
* Delete a schedule.
* @param string|integer $schedule_id ID of schedule to delete
*/
public function delete_schedule( $schedule_id ) {
return $this->request( array(
'function' => 'schedules/' . abs( intval( $schedule_id ) ),
'method' => 'DELETE',
) );
}// end update_schedule
/**
* List all variations associated with the experiment.
* @param string|integer $experiment_id ID of experiment to get variations for
*/
public function get_variations( $experiment_id ) {
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/variations/',
'method' => 'GET',
) );
}// end get_variations
/**
* Get metadata for a single variation.
* @param string|integer $variation_id ID of variation to get
*/
public function get_variation( $variation_id ) {
return $this->request( array(
'function' => 'variations/' . abs( intval( $variation_id ) ),
'method' => 'GET',
) );
}// end get_variation
/**
* Create a variation
* @param string|integer $experiment_id ID of experiment to create variation for
* @param array $options Options for variation
*/
public function create_variation( $experiment_id, $options ) {
if ( ! isset( $options['description'] ) ) {
return FALSE;
}//end if
return $this->request( array(
'function' => 'experiments/' . abs( intval( $experiment_id ) ) . '/variations',
'method' => 'POST',
'data' => $options,
) );
}// end create_variation
/**
* Update a variation
* @param string|integer $variation_id ID of variation to update
* @param array $options Options for variation
*/
public function update_variation( $variation_id, $options ) {
return $this->request( array(
'function' => 'variations/' . abs( intval( $variation_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_variation
/**
* Delete a variation
* @param string|integer $variation_id ID of variation to delete
*/
public function delete_variation( $variation_id ) {
return $this->request( array(
'function' => 'variations/' . abs( intval( $variation_id ) ),
'method' => 'DELETE',
) );
}// end delete_variation
/**
* List all goals associated with the project.
* @param string|integer $project_id ID of project to get the goals for
*/
public function get_goals( $project_id ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/goals/',
'method' => 'GET',
) );
}// end get_goals
/**
* Get metadata for a single goal.
* @param string|integer $goal_id ID of goal to get
*/
public function get_goal( $goal_id ) {
return $this->request( array(
'function' => 'goals/' . abs( intval( $goal_id ) ),
'method' => 'GET',
) );
}// end get_goal
/**
* Create a goal
* @param string|integer $project_id ID of project to create the goal for
* @param array $options Options for goal
*/
public function create_goal( $project_id, $options = array() ) {
if ( ! isset( $options['title'] )
|| ! isset( $options['goal_type'] ) ) {
return FALSE;
}//end if
// check for additional conditional required fields
switch ( $options['goal_type'] ) {
case 0: // Click
if ( ! isset( $options['selector'] )
|| ! isset( $options['target_to_experiments'] ) ) {
return FALSE;
}//end if
if ( FALSE === $options['target_to_experiments'] ) {
if ( ! isset( $options['target_urls'] )
|| ! isset( $options['target_url_match_types'] ) ) {
return FALSE;
}//end if
}//end if
break;
case 1: // Custom event
if ( ! isset( $options['event'] ) ) {
return FALSE;
}//end if
break;
case 2: // Engagement
// no additional required fields
break;
case 3: // Pageviews
if ( ! isset( $options['urls'] )
|| ! isset( $options['url_match_types'] ) ) {
return FALSE;
}//end if
break;
case 4: // Revenue
// no additional required fields
break;
default:
return FALSE;
}// end switch
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/goals/',
'method' => 'POST',
'data' => $options,
) );
}// end create_goal
/**
* Update a goal
* @param string|integer $goal_id ID of goal to update
* @param array $options Options for goal
*/
public function update_goal( $goal_id, $options ) {
return $this->request( array(
'function' => 'goals/' . abs( intval( $goal_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_goal
/**
* Delete a goal (in general, you don't want to do this)
* See remove_goal function for preferred approach
* @param string|integer $goal_id ID of goal to delete
*/
public function delete_goal( $goal_id ) {
return $this->request( array(
'function' => 'goals/' . abs( intval( $goal_id ) ),
'method' => 'DELETE',
) );
}// end delete_goal
/**
* add a goal to an experiment
* @param string|integer $experiment_id ID of experiment to add goal to
* @param string|integer $goal_id ID of goal to add to experiment
*/
public function add_goal( $experiment_id, $goal_id ) {
$goal = $this->get_goal( $goal_id );
if ( ! isset( $goal->experiment_ids ) ) {
return FALSE;
}//end if
$goal->experiment_ids[] = $experiment_id;
$goal->experiment_ids = array_unique( $goal->experiment_ids );
return $this->update_goal( $goal_id, array( 'experiment_ids' => $goal->experiment_ids ) );
}//end add_goal
/**
* remove a goal from an experiment
* @param string|integer $experiment_id ID of experiment to remove goal from
* @param string|integer $goal_id ID of goal to remove from experiment
*/
public function remove_goal( $experiment_id, $goal_id ) {
$goal = $this->get_goal( $goal_id );
if ( ! isset( $goal->experiment_ids ) ) {
return FALSE;
}//end if
$goal->experiment_ids = array_diff( $goal->experiment_ids, array( $experiment_id ) );
return $this->update_goal( $goal_id, array( 'experiment_ids' => $goal->experiment_ids ) );
}//end remove_goal
/**
* List all audiences associated with the project.
* @param string|integer $project_id ID of project to get the audiences for
*/
public function get_audiences( $project_id ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/audiences/',
'method' => 'GET',
) );
}// end get_audiences
/**
* Get metadata for a single audience.
* @param string|integer $audience_id ID of audience to get
*/
public function get_audience( $audience_id ) {
return $this->request( array(
'function' => 'audiences/' . abs( intval( $audience_id ) ),
'method' => 'GET',
) );
}// end get_audience
/**
* Create an audience
* @param string|integer $project_id ID of project to create the audience for
* @param array $options Options for audience
*/
public function create_audience( $project_id, $options ) {
if ( ! isset( $options['name'] ) ) {
return FALSE;
}// end if
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/audiences/',
'method' => 'POST',
'data' => $options,
) );
}// end create_audience
/**
* Update an audience
* @param string|integer $audience_id ID of audience to update
* @param array $options Options for audience
*/
public function update_audience( $audience_id, $options ) {
return $this->request( array(
'function' => 'audiences/' . abs( intval( $audience_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_audience
/**
* List all dimensions associated with the project.
* @param string|integer $project_id ID of project to get the dimensions for
*/
public function get_dimensions( $project_id ) {
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/dimensions/',
'method' => 'GET',
) );
}// end get_dimensions
/**
* Get metadata for a single dimension.
* @param string|integer $dimension_id ID of dimension to get
*/
public function get_dimension( $dimension_id ) {
return $this->request( array(
'function' => 'dimensions/' . abs( intval( $dimension_id ) ),
'method' => 'GET',
) );
}// end get_dimension
/**
* Create a dimension
* @param string|integer $project_id ID of project to create the dimensions for
* @param array $options Options for dimension
*/
public function create_dimension( $project_id, $options ) {
if ( ! isset( $options['name'] ) ) {
return FALSE;
}// end if
return $this->request( array(
'function' => 'projects/' . abs( intval( $project_id ) ) . '/dimensions/',
'method' => 'POST',
'data' => $options,
) );
}// end create_dimension
/**
* Update a dimension
* @param string|integer $dimension_id ID of dimension to update
* @param array $options Options for dimension
*/
public function update_dimension( $dimension_id, $options ) {
return $this->request( array(
'function' => 'dimensions/' . abs( intval( $dimension_id ) ),
'method' => 'PUT',
'data' => $options,
) );
}// end update_dimension
/**
* Delete a dimension, not generally recommended
* @param string|integer $dimension_id ID of dimension to delete
*/
public function delete_dimension( $dimension_id ) {
return $this->request( array(
'function' => 'dimensions/' . abs( intval( $dimension_id ) ),
'method' => 'DELETE',
) );
}// end delete_dimension
}// end Optimizely