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

Refactor GA view helper for improved extensibility.

parent 0a62b293
No related merge requests found
...@@ -65,36 +65,17 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper ...@@ -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 * @param string $customUrl override URL to send to Google Analytics
* *
* @return string * @return string
*/ */
public function __invoke($customUrl = false) protected function getRawJavascript($customUrl = false)
{ {
if (!$this->key) { // Simple case: Universal
return ''; if ($this->universal) {
} return '(function(i,s,o,g,r,a,m){'
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){'
. "i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" . "i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){"
. '(i[r].q=i[r].q||[]).push(arguments)},' . '(i[r].q=i[r].q||[]).push(arguments)},'
. 'i[r].l=1*new Date();a=s.createElement(o),' . 'i[r].l=1*new Date();a=s.createElement(o),'
...@@ -105,6 +86,40 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper ...@@ -105,6 +86,40 @@ class GoogleAnalytics extends \Zend\View\Helper\AbstractHelper
. "ga('create', '{$this->key}', 'auto');" . "ga('create', '{$this->key}', 'auto');"
. "ga('send', 'pageview');"; . "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'); $inlineScript = $this->getView()->plugin('inlinescript');
return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET'); return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET');
} }
......
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