forked from innocraft/php-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_filter.php
More file actions
34 lines (27 loc) · 1.2 KB
/
custom_filter.php
File metadata and controls
34 lines (27 loc) · 1.2 KB
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
<?php
/**
* Example for an A/B test where we have the original version (added automatically),
* a green variation and a blue variation. The `customFilter` restricts which users take part in the experiment
* or not. It is possible to pass a callable for `customFilter` or an object that is an instance of a FilterInterface.
*/
date_default_timezone_set('utc');
use InnoCraft\Experiments\Experiment;
include_once '../vendor/autoload.php';
$variations = [['name' => 'green'], ['name' => 'blue']];
$config = [
// optional, a callable to further restrict with custom logic for whom the experiment will be activated
'customFilter' => function () {
return true; // eg return UserCountry::GetCountry() === 'de';
}
];
$experiment = new Experiment('experimentName', $variations);
// shouldTrigger() may return false depending on the result of the custom filter
if ($experiment->shouldTrigger()) {
// to get a randomly activated variation or a previously forced variation call
$activated = $experiment->getActivatedVariation();
if ($activated->getName() === 'green') {
// ...
}
} else {
echo 'experiment should not be activated, usually means the original version will be shown';
}