Completed
Push — master ( 2cec7d...b8452f )
by Walter
8s
created

GoogleClassicAnalytics::getScript()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 22
ccs 13
cts 13
cp 1
rs 8.9197
cc 4
eloc 11
nc 5
nop 0
crap 4
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 Classic Analytics Experiments ran as External
14
 *
15
 * @package PhpAb
16
 * @see https://developers.google.com/analytics/devguides/collection/gajs/experiments#cxjs-setchosen
17
 */
18
class GoogleClassicAnalytics 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[] = "cxApi.setChosenVariation(" . (int) $variationIndex . ", '" . (string) $testIdentifier . "')";
56 2
        }
57
58 2
        $script[] = '</script>';
59
60 2
        return implode(PHP_EOL, $script);
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 1
    public function getParticipations()
67
    {
68
69 1
        return $this->participations;
70
    }
71
}
72