Blame view

libraries/joomla/google/embed/analytics.php 7.83 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
<?php
/**
 * @package     Joomla.Platform
 * @subpackage  Google
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

/**
 * Google Analytics embed class for the Joomla Platform.
 *
 * @since       3.1.4
 * @deprecated  4.0  Use the `joomla/google` package via Composer instead
 */
class JGoogleEmbedAnalytics extends JGoogleEmbed
{
	/**
	 * Method to get the tracking code
	 *
	 * @return  string  The Google Analytics tracking code
	 *
	 * @since   3.1.4
	 */
	public function getCode()
	{
		return $this->getOption('code');
	}

	/**
	 * Method to set the tracking code
	 *
	 * @param   string  $code  The Google Analytics tracking code
	 *
	 * @return  JGoogleEmbedAnalytics  The object for method chaining
	 *
	 * @since   3.1.4
	 */
	public function setCode($code)
	{
		$this->setOption('code', $code);

		return $this;
	}

	/**
	 * Checks if the javascript is set to be asynchronous
	 *
	 * @return  boolean  True if asynchronous
	 *
	 * @since   3.1.4
	 */
	public function isAsync()
	{
		return $this->getOption('async') === null ? true : $this->getOption('async');
	}

	/**
	 * Load javascript asynchronously
	 *
	 * @return  JGoogleEmbedAnalytics  The object for method chaining
	 *
	 * @since   3.1.4
	 */
	public function useAsync()
	{
		$this->setOption('async', true);

		return $this;
	}

	/**
	 * Load javascript synchronously
	 *
	 * @return  JGoogleEmbedAnalytics  The object for method chaining
	 *
	 * @since   3.1.4
	 */
	public function useSync()
	{
		$this->setOption('async', false);

		return $this;
	}

	/**
	 * Add an analytics call
	 *
	 * @param   string  $method  The name of the function
	 * @param   array   $params  The parameters for the call
	 *
	 * @return  array  The added call
	 *
	 * @since   3.1.4
	 */
	public function addCall($method, $params = array())
	{
		$call = array('name' => $method, 'params' => $params);

		$calls = $this->listCalls();
		$calls[] = $call;
		$this->setOption('calls', $calls);

		return $call;
	}

	/**
	 * List the analytics calls to be executed
	 *
	 * @return  array  A list of calls
	 *
	 * @since   3.1.4
	 */
	public function listCalls()
	{
		return $this->getOption('calls') ? $this->getOption('calls') : array();
	}

	/**
	 * Delete a call from the stack
	 *
	 * @param   int  $index  Index of call to delete (defaults to last added call)
	 *
	 * @return  array  The deleted call
	 *
	 * @since   3.1.4
	 */
	public function deleteCall($index = null)
	{
		$calls = $this->listCalls();

		if ($index === null)
		{
			$index = count($calls) - 1;
		}

		$call = $calls[$index];
		unset($calls[$index]);
		$calls = array_values($calls);
		$this->setOption('calls', $calls);

		return $call;
	}

	/**
	 * Create a javascript function from the call parameters
	 *
	 * @param   string  $method  The name of the function
	 * @param   array   $params  The parameters for the call
	 *
	 * @return  string  The created call
	 *
	 * @since   3.1.4
	 */
	public function createCall($method, $params = array())
	{
		$params = array_values($params);

		if ($this->isAsync())
		{
			$output = "_gaq.push(['{$method}',";
			$output .= substr(json_encode($params), 1, -1);
			$output .= ']);';
		}
		else
		{
			$output = "pageTracker.{$method}(";
			$output .= substr(json_encode($params), 1, -1);
			$output .= ');';
		}

		return $output;
	}

	/**
	 * Add a custom variable to the analytics
	 *
	 * @param   int     $slot   The slot to store the variable in (1-5)
	 * @param   string  $name   The variable name
	 * @param   string  $value  The variable value
	 * @param   int     $scope  The scope of the variable (1: visitor level, 2: session level, 3: page level)
	 *
	 * @return  array  The added call
	 *
	 * @since   3.1.4
	 */
	public function addCustomVar($slot, $name, $value, $scope = 3)
	{
		return $this->addCall('_setCustomVar', array($slot, $name, $value, $scope));
	}

	/**
	 * Get the code to create a custom analytics variable
	 *
	 * @param   int     $slot   The slot to store the variable in (1-5)
	 * @param   string  $name   The variable name
	 * @param   string  $value  The variable value
	 * @param   int     $scope  The scope of the variable (1: visitor level, 2: session level, 3: page level)
	 *
	 * @return  string  The created call
	 *
	 * @since   3.1.4
	 */
	public function createCustomVar($slot, $name, $value, $scope = 3)
	{
		return $this->createCall('_setCustomVar', array($slot, $name, $value, $scope));
	}

	/**
	 * Track an analytics event
	 *
	 * @param   string   $category     The general event category
	 * @param   string   $action       The event action
	 * @param   string   $label        The event description
	 * @param   string   $value        The value of the event
	 * @param   boolean  $noninteract  Don't allow this event to impact bounce statistics
	 *
	 * @return  array  The added call
	 *
	 * @since   3.1.4
	 */
	public function addEvent($category, $action, $label = null, $value = null, $noninteract = false)
	{
		return $this->addCall('_trackEvent', array($category, $action, $label, $value, $noninteract));
	}

	/**
	 * Get the code to track an analytics event
	 *
	 * @param   string   $category     The general event category
	 * @param   string   $action       The event action
	 * @param   string   $label        The event description
	 * @param   string   $value        The value of the event
	 * @param   boolean  $noninteract  Don't allow this event to impact bounce statistics
	 *
	 * @return  string  The created call
	 *
	 * @since   3.1.4
	 */
	public function createEvent($category, $action, $label = null, $value = null, $noninteract = false)
	{
		return $this->createCall('_trackEvent', array($category, $action, $label, $value, $noninteract));
	}

	/**
	 * Get code to load Google Analytics javascript
	 *
	 * @return  string  Javascript code
	 *
	 * @since   3.1.4
	 */
	public function getHeader()
	{
		if (!$this->isAsync())
		{
			// Synchronous code is included only in the body
			return '';
		}

		if (!$this->getOption('code'))
		{
			throw new UnexpectedValueException('A Google Analytics tracking code is required.');
		}

		$code = $this->getOption('code');

		$output = '<script type="text/javascript">';
		$output .= 'var _gaq = _gaq || [];';
		$output .= "_gaq.push(['_setAccount', '{$code}']);";

		foreach ($this->listCalls() as $call)
		{
			$output .= $this->createCall($call['name'], $call['params']);
		}

		$output .= '_gaq.push(["_trackPageview"]);';
		$output .= '</script>';

		return $output;
	}

	/**
	 * Google Analytics only needs to be included in the header
	 *
	 * @return  null
	 *
	 * @since   3.1.4
	 */
	public function getBody()
	{
		if (!$this->getOption('code'))
		{
			throw new UnexpectedValueException('A Google Analytics tracking code is required.');
		}

		$prefix = $this->isSecure() ? 'https://ssl' : 'http://www';
		$code = $this->getOption('code');

		if ($this->isAsync())
		{
			$output = '<script type="text/javascript">';
			$output .= '(function() {';
			$output .= 'var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;';
			$output .= "ga.src = '{$prefix}.google-analytics.com/ga.js';";
			$output .= 'var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);';
			$output .= '})();';
			$output .= '</script>';
		}
		else
		{
			$output = '<script type="text/javascript">';
			$output .= "document.write(unescape(\"%3Cscript src='{$prefix}.google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));";
			$output .= '</script>';
			$output .= '<script type="text/javascript">';
			$output .= 'try{';
			$output .= "var pageTracker = _gat._getTracker('{$code}');";

			foreach ($this->listCalls() as $call)
			{
				$output .= $this->createCall($call['name'], $call['params']);
			}

			$output .= 'pageTracker._trackPageview();';
			$output .= '} catch(err) {}</script>';
		}

		return $output;
	}
}