diff --git a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php index e5e2daed54b841e19107a317633c1a3173d0c34a..fdd8f92e92983cb0d9389d7e5d2402df84fae764 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/GoogleAnalytics.php @@ -65,36 +65,17 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper } /** - * Returns GA code (if active) or empty string if not. + * Returns GA Javascript code. * * @param string $customUrl override URL to send to Google Analytics * * @return string */ - public function __invoke($customUrl = false) + protected function getRawJavascript($customUrl = false) { - if (!$this->key) { - return ''; - } - if (!$this->universal) { - $code = 'var key = "' . $this->key . '";' . "\n" - . "var _gaq = _gaq || [];\n" - . "_gaq.push(['_setAccount', key]);\n"; - if ($customUrl) { - $code .= "_gaq.push(['_trackPageview', '" . $customUrl . "']);\n"; - } else { - $code .= "_gaq.push(['_trackPageview']);\n"; - } - $code .= "(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" - . "})();"; - } else { - $code = '(function(i,s,o,g,r,a,m){' + // Simple case: Universal + if ($this->universal) { + return '(function(i,s,o,g,r,a,m){' . "i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" . '(i[r].q=i[r].q||[]).push(arguments)},' . 'i[r].l=1*new Date();a=s.createElement(o),' @@ -105,6 +86,40 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper . "ga('create', '{$this->key}', 'auto');" . "ga('send', 'pageview');"; } + + // Alternate (legacy) case: + $code = 'var key = "' . $this->key . '";' . "\n" + . "var _gaq = _gaq || [];\n" + . "_gaq.push(['_setAccount', key]);\n"; + if ($customUrl) { + $code .= "_gaq.push(['_trackPageview', '" . $customUrl . "']);\n"; + } else { + $code .= "_gaq.push(['_trackPageview']);\n"; + } + $code .= "(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" + . "})();"; + return $code; + } + + /** + * Returns GA code (if active) or empty string if not. + * + * @param string $customUrl override URL to send to Google Analytics + * + * @return string + */ + public function __invoke($customUrl = false) + { + if (!$this->key) { + return ''; + } + $code = $this->getRawJavascript($customUrl); $inlineScript = $this->getView()->plugin('inlinescript'); return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET'); }