Subversion Repositories Applications.annuaire

Rev

Rev 287 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
282 aurelien 1
<?php
2
 
3
if(isset($_GET['source'])) {
4
    highlight_file(__FILE__);
5
    die;
6
}
7
 
8
/*
9
*
10
* This script is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General public License for more details.
14
*
15
* This copyright notice MUST APPEAR in all copies of the script!
16
*
17
*  @author            CERDAN Yohann <cerdanyohann@yahoo.fr>
18
*  @copyright      (c) 2009  CERDAN Yohann, All rights reserved
19
*  @ version         12:38 04/08/2009
20
*/
21
 
22
class GoogleAnalyticsAPI
23
{
24
	/** Google account login (email) **/
25
	private $login;
26
 
27
	/** Google account password **/
28
	private $password;
29
 
30
	/** Google analytics website ID (avalaible on your google analytics account url) **/
31
	private $ids;
32
 
33
	/** The login token to the google analytics service **/
34
	private $loginToken;
35
 
36
	/** The XML response send by the service **/
37
	private $response;
38
 
39
	/** Begin date of the displaying datas **/
40
	private $date_begin;
41
 
42
	/** End date of the displaying datas **/
43
	private $date_end;
44
 
45
	/** Sort the results **/
46
	private $sort;
47
 
48
	/** The param to sort (metrics or dimensions) **/
49
	private $sort_param;
50
 
51
	/**
52
          * Class constructor
53
          *
54
          * @param string $login the login (email)
55
          * @param string $password the password
56
          * @param string $ids the IDs of the website (find it in the google analytics gui)
57
          * @param string $date_begin the begin date
58
          * @param string $date_end the end date
59
          *
60
          * @return void
61
          */
62
 
63
	public function __construct($login,$password,$ids,$date_begin,$date_end = null)
64
	{
65
		$this->login = $login;
66
		$this->password = $password;
67
		$this->ids = $ids;
68
		$this->date_begin = $date_begin;
69
 
70
		if (!$date_end) {
71
			$this->date_end = $date_begin;
72
		} else {
73
			$this->date_end = $date_end;
74
		}
75
 
76
		$this->sort = "-";
77
		$this->sort_param = "metrics";
78
 
79
		// Authentication
80
		$this->login();
81
	}
82
 
83
	/**
84
          * Set the result's sort by metrics
85
          *
86
          * @param boolean $sort asc or desc sort
87
          *
88
          * @return void
89
          */
90
 
91
	public function setSortByMetrics ($sort)
92
	{
93
		if ($sort==true) {
94
			$this->sort = "";
95
		} else {
96
			$this->sort = "-";
97
		}
98
		$this->sort_param = 'metrics';
99
	}
100
 
101
	/**
102
          * Set the result's sort by dimensions
103
          *
104
          * @param boolean $sort asc or desc sort
105
          *
106
          * @return void
107
          */
108
 
109
	public function setSortByDimensions ($sort)
110
	{
111
		if ($sort==true) {
112
			$this->sort = "";
113
		} else {
114
			$this->sort = "-";
115
		}
116
		$this->sort_param = 'dimensions';
117
	}
118
 
119
	/**
120
          * Set the IDs of the website
121
          *
122
          * @param string $ids the IDs of the website (find it in the google analytics gui)
123
          *
124
          * @return void
125
          */
126
 
127
	public function setIds($ids)
128
	{
129
		$this->ids = $ids;
130
	}
131
 
132
	/**
133
          * Set the date of the export
134
          *
135
          * @param string $date_begin the begin date
136
          * @param string $date_end the end date
137
          *
138
          * @return void
139
          */
140
 
141
	public function setDate ($date_begin,$date_end = null)
142
	{
143
		$this->date_begin = $date_begin;
144
 
145
		if (!$date_end) {
146
			$this->date_end = $date_begin;
147
		} else {
148
			$this->date_end = $date_end;
149
		}
150
	}
151
 
152
	/**
153
          * Login to the google server
154
          * See : http://google-data-api.blogspot.com/2008/05/clientlogin-with-php-curl.html
155
          *
156
          * @return void
157
          */
158
 
159
	private function login()
160
	{
161
		$ch = curl_init();
162
		curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
163
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
164
 
165
		$data = array('accountType' => 'GOOGLE',
166
				  'Email' => $this->login,
167
				  'Passwd' => $this->password,
168
				  'source'=>'php_curl_analytics',
169
				  'service'=>'analytics');
170
 
171
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
172
		curl_setopt($ch, CURLOPT_POST, true);
173
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
174
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
175
 
176
		$hasil = curl_exec($ch);
177
		curl_close($ch);
178
 
179
		// Get the login token
180
		// SID=DQA...oUE
181
		// LSID=DQA...bbo
182
		// Auth=DQA...Sxq
183
		if (preg_match('/Auth=(.*)$/',$hasil,$matches)>0) {
184
			$this->loginToken = $matches[1];
185
		} else {
186
			trigger_error('Authentication problem',E_USER_WARNING);
187
			return null;
188
		}
189
	}
190
 
191
	/**
192
           * Get URL content using cURL.
193
          *
194
          * @param string $url the url
195
          *
196
          * @return string the html code
197
          */
198
 
199
	function getContent ($url)
200
	{
201
		if (!extension_loaded('curl')) {
202
            throw new Exception('curl extension is not available');
203
        }
204
 
205
		$ch = curl_init($url);
206
 
207
		$header[] = 'Authorization: GoogleLogin auth=' . $this->loginToken;
208
 
209
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
210
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
211
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
212
		curl_setopt($ch, CURLOPT_HEADER, false);
213
 
214
		$this->response = curl_exec($ch);
215
		$infos = curl_getinfo($ch);
216
		curl_close($ch);
217
 
218
		return $infos['http_code'];
219
	}
220
 
221
	/**
222
          * Get the google analytics datas by dimensions and metrics
223
          * See : http://code.google.com/intl/fr/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
224
          *
225
          * @param string $metrics the metrics
226
          * @param string $dimensions the dimensions
227
          *
228
          * @return array
229
          */
230
 
231
	public function getDimensionByMetric($metrics, $dimensions)
232
	{
233
		$url = "https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metrics . "&dimensions=ga:" . $dimensions . "&start-date=" . $this->date_begin . "&end-date=" . $this->date_end ."&sort=" . $this->sort . "ga:";
234
 
235
		if ($this->sort_param=='metrics') { // sort by metrics
236
			$url .= $metrics;
237
		}
238
 
239
		if ($this->sort_param=='dimensions') { // sort by dimensions
240
			$url .= $dimensions;
241
		}
242
 
243
		if($this->getContent($url) == 200) {
244
			$XML_object = simplexml_load_string($this->response);
245
			$labels_array = array();
246
			$datas_array = array();
247
 
248
			foreach($XML_object->entry as $m) {
249
				$dxp = $m->children('http://schemas.google.com/analytics/2009');
250
				$metric_att = $dxp->metric->attributes();
251
				$dimension_att = $dxp->dimension->attributes();
252
				$labels_array []= $dimension_att['value'] . ' (' . $metric_att['value'] . ')';
253
				$datas_array  []= (string)$metric_att['value'];
254
			}
255
 
256
			return array('labels' => $labels_array, 'datas' => $datas_array);
257
		} else {
258
			return null;
259
		}
260
	}
261
 
262
	/**
263
          * Get the google analytics datas by metrics
264
          * See : http://code.google.com/intl/fr/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
265
          *
266
          * @param string $metrics the metrics
267
          * @param string $uri the url of the website page (ex : /myurl/)
268
          *
269
          * @return array
270
          */
271
 
272
	public function getMetric($metric,$uri=null)
273
	{
274
		$url = "https://www.google.com/analytics/feeds/data?ids=ga:" . $this->ids . "&metrics=ga:" . $metric . "&start-date=" . $this->date_begin . "&end-date=" . $this->date_end;
275
 
276
		if ($uri) {
277
			$url .= "&dimensions=ga:pagePath&filters=ga:pagePath%3D%3D" . $uri;
278
		}
279
 
280
		if($this->getContent($url) == 200) {
281
			$XML_object = simplexml_load_string($this->response);
282
			$dxp = $XML_object->entry->children('http://schemas.google.com/analytics/2009');
283
			if (@count($dxp)>0) {
284
				$metric_att = $dxp->metric->attributes();
285
			}
286
			return $metric_att['value'] ? (string)$metric_att['value'] : 0;
287
		} else {
288
			return null;
289
		}
290
	}
291
 
292
}
293
 
294
?>