forked from vufind-org/vufind
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Google Tag Manager support (vufind-org#2637)
- Loading branch information
1 parent
ef166c0
commit 8863651
Showing
6 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
module/VuFind/src/VuFind/View/Helper/Root/GoogleTagManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* GoogleTagManager view helper | ||
* | ||
* PHP version 7 | ||
* | ||
* Copyright (C) Villanova University 2022. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Site | ||
*/ | ||
namespace VuFind\View\Helper\Root; | ||
|
||
/** | ||
* GoogleTagManager view helper | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org Main Site | ||
*/ | ||
class GoogleTagManager extends \Laminas\View\Helper\AbstractHelper | ||
{ | ||
/** | ||
* GTM Container ID (false if disabled) | ||
* | ||
* @var string|bool | ||
*/ | ||
protected $gtmContainerId; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string|bool $gtmContainerId Container ID (false if disabled) | ||
*/ | ||
public function __construct($gtmContainerId) | ||
{ | ||
$this->gtmContainerId = $gtmContainerId; | ||
} | ||
|
||
/** | ||
* Returns GTM code block meant for the <head> element. | ||
* | ||
* @return string | ||
*/ | ||
public function getHeadCode() | ||
{ | ||
if (!$this->gtmContainerId) { | ||
return ''; | ||
} | ||
|
||
// phpcs:disable -- line length should be kept for this vendor snippet | ||
$js = <<<END | ||
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]'); | ||
n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','dataLayer','{$this->gtmContainerId}'); | ||
END; | ||
// phpcs:enable | ||
$inlineScript = $this->getView()->plugin('inlinescript'); | ||
$js = $inlineScript(\Laminas\View\Helper\HeadScript::SCRIPT, $js, 'SET'); | ||
return $js; | ||
} | ||
|
||
/** | ||
* Returns GTM code block meant for the <body> element. | ||
* | ||
* @return string | ||
*/ | ||
public function getBodyCode() | ||
{ | ||
if (!$this->gtmContainerId) { | ||
return ''; | ||
} | ||
|
||
// phpcs:disable -- line length should be kept for this vendor snippet | ||
$js = <<<END | ||
<!-- Google Tag Manager (noscript) --> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={$this->gtmContainerId}" | ||
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | ||
<!-- End Google Tag Manager (noscript) --> | ||
END; | ||
// phpcs:enable | ||
return $js; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
module/VuFind/src/VuFind/View/Helper/Root/GoogleTagManagerFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* GoogleTagManager helper factory. | ||
* | ||
* PHP version 7 | ||
* | ||
* Copyright (C) Villanova University 2022. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
namespace VuFind\View\Helper\Root; | ||
|
||
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; | ||
use Laminas\ServiceManager\Exception\ServiceNotFoundException; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Psr\Container\ContainerExceptionInterface as ContainerException; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* GoogleTagManager helper factory. | ||
* | ||
* @category VuFind | ||
* @package View_Helpers | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development Wiki | ||
*/ | ||
class GoogleTagManagerFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create an object | ||
* | ||
* @param ContainerInterface $container Service manager | ||
* @param string $requestedName Service being created | ||
* @param null|array $options Extra options (optional) | ||
* | ||
* @return object | ||
* | ||
* @throws ServiceNotFoundException if unable to resolve the service. | ||
* @throws ServiceNotCreatedException if an exception is raised when | ||
* creating a service. | ||
* @throws ContainerException&\Throwable if any other error occurs | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
array $options = null | ||
) { | ||
if (!empty($options)) { | ||
throw new \Exception('Unexpected options sent to factory.'); | ||
} | ||
$config = $container->get(\VuFind\Config\PluginManager::class) | ||
->get('config'); | ||
$gtmContainerId = $config->GoogleTagManager->gtmContainerId ?? false; | ||
return new $requestedName($gtmContainerId); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
module/VuFind/tests/unit-tests/src/VuFindTest/View/Helper/Root/GoogleTagManagerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
/** | ||
* GoogleTagManager view helper Test Class | ||
* | ||
* PHP version 7 | ||
* | ||
* Copyright (C) Villanova University 2022. | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 2, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* @category VuFind | ||
* @package Tests | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki | ||
*/ | ||
namespace VuFindTest\View\Helper\Root; | ||
|
||
use VuFind\View\Helper\Root\GoogleTagManager; | ||
|
||
/** | ||
* GoogleTagManager view helper Test Class | ||
* | ||
* @category VuFind | ||
* @package Tests | ||
* @author Maccabee Levine <[email protected]> | ||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License | ||
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki | ||
*/ | ||
class GoogleTagManagerTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
use \VuFindTest\Feature\ViewTrait; | ||
|
||
/** | ||
* Test getHeadCode() | ||
* | ||
* @return void | ||
*/ | ||
public function testHeadCode(): void | ||
{ | ||
$output = $this->renderGTMHeadCode('fakeContainerId'); | ||
$this->assertTrue(false !== strstr($output, 'gtm.js')); | ||
$this->assertTrue(false !== strstr($output, 'fakeContainerId')); | ||
} | ||
|
||
/** | ||
* Test getBodyCode() | ||
* | ||
* @return void | ||
*/ | ||
public function testBodyCode(): void | ||
{ | ||
$output = $this->renderGTMBodyCode('fakeContainerId'); | ||
$this->assertTrue(false !== strstr($output, 'ns.html')); | ||
$this->assertTrue(false !== strstr($output, 'fakeContainerId')); | ||
} | ||
|
||
/** | ||
* Test the helper (disabled mode) | ||
* | ||
* @return void | ||
*/ | ||
public function testDisabled(): void | ||
{ | ||
$this->assertEquals('', $this->renderGTMHeadCode(false)); | ||
$this->assertEquals('', $this->renderGTMBodyCode(false)); | ||
} | ||
|
||
/** | ||
* Render the GTM Head element code | ||
* | ||
* @param string $gtmContainerId GTM Container ID (false for disabled) | ||
* | ||
* @return string | ||
*/ | ||
protected function renderGTMHeadCode(string $gtmContainerId): string | ||
{ | ||
$helper = new GoogleTagManager($gtmContainerId); | ||
$helper->setView($this->getPhpRenderer()); | ||
return (string)$helper->getHeadCode(); | ||
} | ||
|
||
/** | ||
* Render the GTM Body element code | ||
* | ||
* @param string $gtmContainerId GTM Container ID (false for disabled) | ||
* | ||
* @return string | ||
*/ | ||
protected function renderGTMBodyCode(string $gtmContainerId): string | ||
{ | ||
$helper = new GoogleTagManager($gtmContainerId); | ||
$helper->setView($this->getPhpRenderer()); | ||
return (string)$helper->getBodyCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters