Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GoogleTagManager
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeadCode
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getBodyCode
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * GoogleTagManager view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2022.
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   Maccabee Levine <msl321@lehigh.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
32/**
33 * GoogleTagManager view helper
34 *
35 * @category VuFind
36 * @package  View_Helpers
37 * @author   Maccabee Levine <msl321@lehigh.edu>
38 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
39 * @link     https://vufind.org Main Site
40 */
41class GoogleTagManager extends \Laminas\View\Helper\AbstractHelper
42{
43    /**
44     * GTM Container ID (false if disabled)
45     *
46     * @var string|bool
47     */
48    protected $gtmContainerId;
49
50    /**
51     * Constructor
52     *
53     * @param string|bool $gtmContainerId Container ID (false if disabled)
54     */
55    public function __construct($gtmContainerId)
56    {
57        $this->gtmContainerId = $gtmContainerId;
58    }
59
60    /**
61     * Returns GTM code block meant for the <head> element.
62     *
63     * @return string
64     */
65    public function getHeadCode()
66    {
67        if (!$this->gtmContainerId) {
68            return '';
69        }
70
71        // phpcs:disable -- line length should be kept for this vendor snippet
72        $js = <<<END
73            (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
74            new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
75            j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
76            'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
77            n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
78            })(window,document,'script','dataLayer','{$this->gtmContainerId}');
79            END;
80        // phpcs:enable
81        $inlineScript = $this->getView()->plugin('inlinescript');
82        $js = $inlineScript(\Laminas\View\Helper\HeadScript::SCRIPT, $js, 'SET');
83        return $js;
84    }
85
86    /**
87     * Returns GTM code block meant for the <body> element.
88     *
89     * @return string
90     */
91    public function getBodyCode()
92    {
93        if (!$this->gtmContainerId) {
94            return '';
95        }
96
97        // phpcs:disable -- line length should be kept for this vendor snippet
98        $js = <<<END
99            <!-- Google Tag Manager (noscript) -->
100            <noscript><iframe src="https://www.googletagmanager.com/ns.html?id={$this->gtmContainerId}"
101            height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
102            <!-- End Google Tag Manager (noscript) -->
103            END;
104        // phpcs:enable
105        return $js;
106    }
107}