10 defined(
'JPATH_PLATFORM') or die;
28 public function getCode()
30 return $this->getOption(
'code');
42 public function setCode($code)
44 $this->setOption(
'code', $code);
56 public function isAsync()
58 return $this->getOption(
'async') === null ?
true : $this->getOption(
'async');
68 public function useAsync()
70 $this->setOption(
'async',
true);
82 public function useSync()
84 $this->setOption(
'async',
false);
99 public function addCall($method, $params = array())
101 $call = array(
'name' => $method,
'params' => $params);
103 $calls = $this->listCalls();
105 $this->setOption(
'calls', $calls);
117 public function listCalls()
119 return $this->getOption(
'calls') ? $this->getOption(
'calls') : array();
131 public function deleteCall($index = null)
133 $calls = $this->listCalls();
137 $index = count($calls) - 1;
140 $call = $calls[$index];
141 unset($calls[$index]);
142 $calls = array_values($calls);
143 $this->setOption(
'calls', $calls);
158 public function createCall($method, $params = array())
160 $params = array_values($params);
162 if ($this->isAsync())
164 $output =
"_gaq.push(['{$method}',";
165 $output .= substr(json_encode($params), 1, -1);
170 $output =
"pageTracker.{$method}(";
171 $output .= substr(json_encode($params), 1, -1);
190 public function addCustomVar($slot, $name, $value, $scope = 3)
192 return $this->addCall(
'_setCustomVar', array($slot, $name, $value, $scope));
207 public function createCustomVar($slot, $name, $value, $scope = 3)
209 return $this->createCall(
'_setCustomVar', array($slot, $name, $value, $scope));
225 public function addEvent($category, $action, $label = null, $value = null, $noninteract =
false)
227 return $this->addCall(
'_trackEvent', array($category, $action, $label, $value, $noninteract));
243 public function createEvent($category, $action, $label = null, $value = null, $noninteract =
false)
245 return $this->createCall(
'_trackEvent', array($category, $action, $label, $value, $noninteract));
255 public function getHeader()
257 if (!$this->isAsync())
263 if (!$this->getOption(
'code'))
265 throw new UnexpectedValueException(
'A Google Analytics tracking code is required.');
268 $code = $this->getOption(
'code');
270 $output =
'<script type="text/javascript">';
271 $output .=
'var _gaq = _gaq || [];';
272 $output .=
"_gaq.push(['_setAccount', '{$code}']);";
274 foreach ($this->listCalls() as $call)
276 $output .= $this->createCall($call[
'name'], $call[
'params']);
279 $output .=
'_gaq.push(["_trackPageview"]);';
280 $output .=
'</script>';
292 public function getBody()
294 if (!$this->getOption(
'code'))
296 throw new UnexpectedValueException(
'A Google Analytics tracking code is required.');
299 $prefix = $this->isSecure() ?
'https://ssl' :
'http://www';
300 $code = $this->getOption(
'code');
302 if ($this->isAsync())
304 $output =
'<script type="text/javascript">';
305 $output .=
'(function() {';
306 $output .=
'var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;';
307 $output .=
"ga.src = '{$prefix}.google-analytics.com/ga.js';";
308 $output .=
'var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);';
310 $output .=
'</script>';
314 $output =
'<script type="text/javascript">';
315 $output .=
"document.write(unescape(\"%3Cscript src='{$prefix}.google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));";
316 $output .=
'</script>';
317 $output .=
'<script type="text/javascript">';
319 $output .=
"var pageTracker = _gat._getTracker('{$code}');";
321 foreach ($this->listCalls() as $call)
323 $output .= $this->createCall($call[
'name'], $call[
'params']);
326 $output .=
'pageTracker._trackPageview();';
327 $output .=
'} catch(err) {}</script>';