Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
analytics.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Google
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Google Analytics embed class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Google
17  * @since 12.3
18  */
20 {
21  /**
22  * Method to get the tracking code
23  *
24  * @return string The Google Analytics tracking code
25  *
26  * @since 12.3
27  */
28  public function getCode()
29  {
30  return $this->getOption('code');
31  }
32 
33  /**
34  * Method to set the tracking code
35  *
36  * @param string $code The Google Analytics tracking code
37  *
38  * @return JGoogleEmbedAnalytics The object for method chaining
39  *
40  * @since 12.3
41  */
42  public function setCode($code)
43  {
44  $this->setOption('code', $code);
45 
46  return $this;
47  }
48 
49  /**
50  * Checks if the javascript is set to be asynchronous
51  *
52  * @return boolean True if asynchronous
53  *
54  * @since 12.3
55  */
56  public function isAsync()
57  {
58  return $this->getOption('async') === null ? true : $this->getOption('async');
59  }
60 
61  /**
62  * Load javascript asynchronously
63  *
64  * @return JGoogleEmbedAnalytics The object for method chaining
65  *
66  * @since 12.3
67  */
68  public function useAsync()
69  {
70  $this->setOption('async', true);
71 
72  return $this;
73  }
74 
75  /**
76  * Load javascript synchronously
77  *
78  * @return JGoogleEmbedAnalytics The object for method chaining
79  *
80  * @since 12.3
81  */
82  public function useSync()
83  {
84  $this->setOption('async', false);
85 
86  return $this;
87  }
88 
89  /**
90  * Add an analytics call
91  *
92  * @param string $method The name of the function
93  * @param array $params The parameters for the call
94  *
95  * @return array The added call
96  *
97  * @since 12.3
98  */
99  public function addCall($method, $params = array())
100  {
101  $call = array('name' => $method, 'params' => $params);
102 
103  $calls = $this->listCalls();
104  $calls[] = $call;
105  $this->setOption('calls', $calls);
106 
107  return $call;
108  }
109 
110  /**
111  * List the analytics calls to be executed
112  *
113  * @return array A list of calls
114  *
115  * @since 12.3
116  */
117  public function listCalls()
118  {
119  return $this->getOption('calls') ? $this->getOption('calls') : array();
120  }
121 
122  /**
123  * Delete a call from the stack
124  *
125  * @param int $index Index of call to delete (defaults to last added call)
126  *
127  * @return array The deleted call
128  *
129  * @since 12.3
130  */
131  public function deleteCall($index = null)
132  {
133  $calls = $this->listCalls();
134 
135  if ($index === null)
136  {
137  $index = count($calls) - 1;
138  }
139 
140  $call = $calls[$index];
141  unset($calls[$index]);
142  $calls = array_values($calls);
143  $this->setOption('calls', $calls);
144 
145  return $call;
146  }
147 
148  /**
149  * Create a javascript function from the call parameters
150  *
151  * @param string $method The name of the function
152  * @param array $params The parameters for the call
153  *
154  * @return string The created call
155  *
156  * @since 12.3
157  */
158  public function createCall($method, $params = array())
159  {
160  $params = array_values($params);
161 
162  if ($this->isAsync())
163  {
164  $output = "_gaq.push(['{$method}',";
165  $output .= substr(json_encode($params), 1, -1);
166  $output .= ']);';
167  }
168  else
169  {
170  $output = "pageTracker.{$method}(";
171  $output .= substr(json_encode($params), 1, -1);
172  $output .= ');';
173  }
174 
175  return $output;
176  }
177 
178  /**
179  * Add a custom variable to the analytics
180  *
181  * @param int $slot The slot to store the variable in (1-5)
182  * @param string $name The variable name
183  * @param string $value The variable value
184  * @param int $scope The scope of the variable (1: visitor level, 2: session level, 3: page level)
185  *
186  * @return array The added call
187  *
188  * @since 12.3
189  */
190  public function addCustomVar($slot, $name, $value, $scope = 3)
191  {
192  return $this->addCall('_setCustomVar', array($slot, $name, $value, $scope));
193  }
194 
195  /**
196  * Get the code to create a custom analytics variable
197  *
198  * @param int $slot The slot to store the variable in (1-5)
199  * @param string $name The variable name
200  * @param string $value The variable value
201  * @param int $scope The scope of the variable (1: visitor level, 2: session level, 3: page level)
202  *
203  * @return string The created call
204  *
205  * @since 12.3
206  */
207  public function createCustomVar($slot, $name, $value, $scope = 3)
208  {
209  return $this->createCall('_setCustomVar', array($slot, $name, $value, $scope));
210  }
211 
212  /**
213  * Track an analytics event
214  *
215  * @param string $category The general event category
216  * @param string $action The event action
217  * @param string $label The event description
218  * @param string $value The value of the event
219  * @param boolean $noninteract Don't allow this event to impact bounce statistics
220  *
221  * @return array The added call
222  *
223  * @since 12.3
224  */
225  public function addEvent($category, $action, $label = null, $value = null, $noninteract = false)
226  {
227  return $this->addCall('_trackEvent', array($category, $action, $label, $value, $noninteract));
228  }
229 
230  /**
231  * Get the code to track an analytics event
232  *
233  * @param string $category The general event category
234  * @param string $action The event action
235  * @param string $label The event description
236  * @param string $value The value of the event
237  * @param boolean $noninteract Don't allow this event to impact bounce statistics
238  *
239  * @return string The created call
240  *
241  * @since 12.3
242  */
243  public function createEvent($category, $action, $label = null, $value = null, $noninteract = false)
244  {
245  return $this->createCall('_trackEvent', array($category, $action, $label, $value, $noninteract));
246  }
247 
248  /**
249  * Get code to load Google Analytics javascript
250  *
251  * @return string Javascript code
252  *
253  * @since 12.3
254  */
255  public function getHeader()
256  {
257  if (!$this->isAsync())
258  {
259  // Synchronous code is included only in the body
260  return '';
261  }
262 
263  if (!$this->getOption('code'))
264  {
265  throw new UnexpectedValueException('A Google Analytics tracking code is required.');
266  }
267 
268  $code = $this->getOption('code');
269 
270  $output = '<script type="text/javascript">';
271  $output .= 'var _gaq = _gaq || [];';
272  $output .= "_gaq.push(['_setAccount', '{$code}']);";
273 
274  foreach ($this->listCalls() as $call)
275  {
276  $output .= $this->createCall($call['name'], $call['params']);
277  }
278 
279  $output .= '_gaq.push(["_trackPageview"]);';
280  $output .= '</script>';
281 
282  return $output;
283  }
284 
285  /**
286  * Google Analytics only needs to be included in the header
287  *
288  * @return null
289  *
290  * @since 12.3
291  */
292  public function getBody()
293  {
294  if (!$this->getOption('code'))
295  {
296  throw new UnexpectedValueException('A Google Analytics tracking code is required.');
297  }
298 
299  $prefix = $this->isSecure() ? 'https://ssl' : 'http://www';
300  $code = $this->getOption('code');
301 
302  if ($this->isAsync())
303  {
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);';
309  $output .= '})();';
310  $output .= '</script>';
311  }
312  else
313  {
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">';
318  $output .= 'try{';
319  $output .= "var pageTracker = _gat._getTracker('{$code}');";
320 
321  foreach ($this->listCalls() as $call)
322  {
323  $output .= $this->createCall($call['name'], $call['params']);
324  }
325 
326  $output .= 'pageTracker._trackPageview();';
327  $output .= '} catch(err) {}</script>';
328  }
329 
330  return $output;
331  }
332 }