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\Renderer\Google; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class will only work for Universal Analytics Experiments ran as External |
14
|
|
|
* |
15
|
|
|
* @package PhpAb |
16
|
|
|
* @see https://developers.google.com/analytics/devguides/collection/analyticsjs/experiments#pro-server |
17
|
|
|
*/ |
18
|
|
|
class GoogleUniversalAnalytics extends AbstractGoogleAnalytics |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The map with test identifiers and variation indexes. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $participations = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initializes a new instance of this class. |
29
|
|
|
* |
30
|
|
|
* @param array $participations |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct(array $participations) |
33
|
|
|
{ |
34
|
4 |
|
$this->participations = $participations; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
3 |
|
public function getScript() |
41
|
|
|
{ |
42
|
3 |
|
if (empty($this->participations)) { |
43
|
1 |
|
return ''; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
$script = []; |
47
|
|
|
|
48
|
2 |
|
if (true === $this->getApiCLientInclusion()) { |
49
|
1 |
|
$script[] = '<script src="//www.google-analytics.com/cx/api.js"></script>'; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
2 |
|
$script[] = '<script>'; |
53
|
|
|
|
54
|
2 |
|
foreach ($this->participations as $testIdentifier => $variationIndex) { |
55
|
2 |
|
$script[] = "(function(){ |
56
|
|
|
ga(function(tracker) { |
57
|
2 |
|
cxApi.setChosenVariation(".$variationIndex.", '".$testIdentifier."'); |
58
|
2 |
|
tracker.send('event', 'PhpAb', '".$testIdentifier."', {'nonInteraction': 1}); |
59
|
|
|
}); |
60
|
2 |
|
})();"; |
61
|
2 |
|
} |
62
|
|
|
|
63
|
2 |
|
$script[] = '</script>'; |
64
|
|
|
|
65
|
2 |
|
return implode(PHP_EOL, $script); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
1 |
|
public function getParticipations() |
72
|
|
|
{ |
73
|
|
|
|
74
|
1 |
|
return $this->participations; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|