Skip to content
Snippets Groups Projects
Commit a81eb9e0 authored by Demian Katz's avatar Demian Katz
Browse files

Added configurable Google Analytics support.

Resolves VUFIND-744; thanks to Tom Misilo.
parent e7a1de94
No related merge requests found
...@@ -869,4 +869,8 @@ treeSearchLimit = 100 ...@@ -869,4 +869,8 @@ treeSearchLimit = 100
[Social] [Social]
; This controls the maximum length of a single tag; it should correspond with the ; This controls the maximum length of a single tag; it should correspond with the
; field size in the tags database table. ; field size in the tags database table.
max_tag_length = 64 max_tag_length = 64
\ No newline at end of file
; Uncomment this section and provide your API key to enable Google Analytics
;[GoogleAnalytics]
;apiKey = "mykey"
\ No newline at end of file
<?php
/**
* GoogleAnalytics view helper
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package View_Helpers
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
namespace VuFind\View\Helper\Root;
/**
* GoogleAnalytics view helper
*
* @category VuFind2
* @package View_Helpers
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper
{
/**
* API key (false if disabled)
*
* @var string|bool
*/
protected $key;
/**
* Constructor
*
* @param string|bool $key API key (false if disabled)
*/
public function __construct($key)
{
$this->key = $key;
}
/**
* Returns GA code (if active) or empty string if not.
*
* @return string
*/
public function __invoke()
{
if (!$this->key) {
return '';
}
$code = 'var key = "' . $this->key . '";' . "\n"
. "var _gaq = _gaq || [];\n"
. "_gaq.push(['_setAccount', key]);\n"
. "_gaq.push(['_trackPageview']);\n"
. "(function() {\n"
. "var ga = document.createElement('script'); "
. "ga.type = 'text/javascript'; ga.async = true;\n"
. "ga.src = ('https:' == document.location.protocol ? "
. "'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"
. "var s = document.getElementsByTagName('script')[0]; "
. "s.parentNode.insertBefore(ga, s);\n"
. "})();";
$inlineScript = $this->getView()->plugin('inlinescript');
return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code);
}
}
\ No newline at end of file
...@@ -103,5 +103,6 @@ ...@@ -103,5 +103,6 @@
<?=$this->layout()->poweredBy?> <?=$this->layout()->poweredBy?>
</div> </div>
</div> </div>
<?=$this->googleanalytics()?>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -38,5 +38,6 @@ ...@@ -38,5 +38,6 @@
<? endif; ?> <? endif; ?>
</div> </div>
</div> </div>
<?=$this->googleanalytics()?>
</body> </body>
</html> </html>
...@@ -60,6 +60,12 @@ return array( ...@@ -60,6 +60,12 @@ return array(
->get('FlashMessenger'); ->get('FlashMessenger');
return new \VuFind\View\Helper\Root\Flashmessages($messenger); return new \VuFind\View\Helper\Root\Flashmessages($messenger);
}, },
'googleanalytics' => function ($sm) {
$config = $sm->getServiceLocator()->get('VuFind\Config')->get('config');
$key = isset($config->GoogleAnalytics->apiKey)
? $config->GoogleAnalytics->apiKey : false;
return new \VuFind\View\Helper\Root\GoogleAnalytics($key);
},
'ils' => function ($sm) { 'ils' => function ($sm) {
return new \VuFind\View\Helper\Root\Ils( return new \VuFind\View\Helper\Root\Ils(
$sm->getServiceLocator()->get('VuFind\ILSConnection') $sm->getServiceLocator()->get('VuFind\ILSConnection')
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment