Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
GoogleAnalytics
93.75% covered (success)
93.75%
15 / 16
66.67% covered (warning)
66.67%
2 / 3
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getRawJavascript
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * GoogleAnalytics view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Laminas\View\Helper\HeadScript;
33
34use function is_array;
35
36/**
37 * GoogleAnalytics view helper
38 *
39 * @category VuFind
40 * @package  View_Helpers
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org Main Site
44 */
45class GoogleAnalytics extends \Laminas\View\Helper\AbstractHelper
46{
47    /**
48     * API key (false if disabled)
49     *
50     * @var string|bool
51     */
52    protected $key;
53
54    /**
55     * Options to pass to the ga() create command.
56     *
57     * @var string
58     */
59    protected $createOptions;
60
61    /**
62     * Constructor
63     *
64     * @param string|bool $key     API key (false if disabled)
65     * @param bool|array  $options Configuration options (supported options:
66     * 'universal' and 'create_options_js'). If a boolean is provided instead of
67     * an array, that value is used as the 'universal' setting and no other options
68     * are set (for backward compatibility).
69     */
70    public function __construct($key, $options = [])
71    {
72        // The second constructor parameter used to be a boolean representing
73        // the "universal" setting, so convert to an array for back-compatibility:
74        if (!is_array($options)) {
75            $options = ['universal' => (bool)$options];
76        }
77        $this->key = $key;
78        $this->createOptions = $options['create_options_js'] ?? "'auto'";
79    }
80
81    /**
82     * Returns GA Javascript code.
83     *
84     * @param string $customUrl override URL to send to Google Analytics
85     *
86     * @return string
87     */
88    protected function getRawJavascript($customUrl = false)
89    {
90        return <<<JS
91            window.dataLayer = window.dataLayer || [];
92            function gtag(){dataLayer.push(arguments);}
93            gtag('js', new Date());
94            gtag('config', '{$this->key}', {$this->createOptions});
95            JS;
96    }
97
98    /**
99     * Returns GA code (if active) or empty string if not.
100     *
101     * @param string $customUrl override URL to send to Google Analytics
102     *
103     * @return string
104     */
105    public function __invoke($customUrl = false)
106    {
107        if (!$this->key) {
108            return '';
109        }
110        $inlineScript = $this->getView()->plugin('inlinescript');
111        $url = 'https://www.googletagmanager.com/gtag/js?id=' . urlencode($this->key);
112        $code = $this->getRawJavascript($customUrl);
113        return
114            $inlineScript(HeadScript::FILE, $url, 'SET', ['async' => true]) . "\n"
115            . $inlineScript(HeadScript::SCRIPT, $code, 'SET');
116    }
117}