1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpab/phpab. (https://github.com/phpab/phpab) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/phpab/phpab for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/) |
7
|
|
|
* @license https://raw.githubusercontent.com/phpab/phpab/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PhpAb\Analytics\DataCollector; |
11
|
|
|
|
12
|
|
|
use PhpAb\Event\SubscriberInterface; |
13
|
|
|
use PhpAb\Test\TestInterface; |
14
|
|
|
use PhpAb\Variant\VariantInterface; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A data collector that holds information about which tests have been executed in a format for Google. |
19
|
|
|
* |
20
|
|
|
* @package PhpAb |
21
|
|
|
*/ |
22
|
|
|
class Google implements SubscriberInterface |
23
|
|
|
{ |
24
|
|
|
const EXPERIMENT_ID = 'experimentId'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array Test identifiers and variation indexes |
28
|
|
|
*/ |
29
|
|
|
private $participations = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
public function getSubscribedEvents() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
8 |
|
'phpab.participation.variant_run' => function ($options) { |
38
|
8 |
|
Assert::notEmpty($options, 'Array passed to closure cannot be empty.'); |
39
|
|
|
|
40
|
7 |
|
Assert::keyExists($options, 1, 'Second parameter passed to closure must be instance of Bag.'); |
41
|
|
|
|
42
|
6 |
|
Assert::isInstanceOf( |
43
|
6 |
|
$options[1], |
44
|
6 |
|
'PhpAb\Test\Bag', |
45
|
|
|
'Second parameter passed to closure must be instance of Bag.' |
46
|
6 |
|
); |
47
|
|
|
|
48
|
5 |
|
Assert::keyExists( |
49
|
5 |
|
$options, |
50
|
5 |
|
2, |
51
|
|
|
'Third parameter passed to closure must be instance of VariantInterface.' |
52
|
5 |
|
); |
53
|
|
|
|
54
|
4 |
|
Assert::isInstanceOf( |
55
|
4 |
|
$options[2], |
56
|
4 |
|
'PhpAb\Variant\VariantInterface', |
57
|
|
|
'Third parameter passed to closure must be instance of VariantInterface.' |
58
|
4 |
|
); |
59
|
|
|
|
60
|
|
|
/** @var TestInterface $test */ |
61
|
3 |
|
$test = $options[1]->getTest(); |
62
|
|
|
|
63
|
3 |
|
Assert::keyExists( |
64
|
3 |
|
$test->getOptions(), |
65
|
3 |
|
static::EXPERIMENT_ID, |
66
|
|
|
'A Google Analytics Experiment Id must be set as options.' |
67
|
3 |
|
); |
68
|
|
|
|
69
|
2 |
|
$experimentId = $test->getOptions()[static::EXPERIMENT_ID]; |
70
|
|
|
|
71
|
|
|
/** @var VariantInterface $chosenVariant */ |
72
|
2 |
|
$chosenVariant = $options[2]; |
73
|
|
|
|
74
|
2 |
|
$variants = $test->getVariants(); |
75
|
|
|
|
76
|
|
|
// Get the index number of the element |
77
|
2 |
|
$chosenIndex = array_search($chosenVariant->getIdentifier(), array_keys($variants)); |
78
|
|
|
|
79
|
|
|
// Call the add method |
80
|
2 |
|
$this->addParticipation($experimentId, $chosenIndex); |
81
|
2 |
|
} |
82
|
9 |
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds a participation to the data collector. |
87
|
|
|
* |
88
|
|
|
* @param string $testIdentifier It will look like "Qp0gahJ3RAO3DJ18b0XoUQ" |
89
|
|
|
* @param int $variationIndex |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
*/ |
92
|
3 |
|
public function addParticipation($testIdentifier, $variationIndex) |
93
|
|
|
{ |
94
|
3 |
|
Assert::string($testIdentifier, 'Test identifier must be a string'); |
95
|
3 |
|
Assert::integer($variationIndex, 'Variation index must be integer'); |
96
|
3 |
|
Assert::greaterThan($variationIndex, -1, 'Variation index must be integer >= 0'); |
97
|
|
|
|
98
|
3 |
|
$this->participations[$testIdentifier] = $variationIndex; |
99
|
3 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the test data that has been collected. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
3 |
|
public function getTestsData() |
107
|
|
|
{ |
108
|
3 |
|
return $this->participations; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|