forked from wanze/Google-Analytics-API-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleAnalyticsAPI.class.php
553 lines (431 loc) · 13.1 KB
/
GoogleAnalyticsAPI.class.php
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
/**
* GoogleAnalyticsAPI
*
* Simple class which provides methods to set up Auth with Google and query the Google Analytics API v3 with PHP.
* cURL is required.
* Please note that this class does not handle error codes returned from Google. But the the http status code
* is returned along with the data. You can check for the array-key 'status_code', which should be 200 if everything worked.
*
* See the readme on GitHub for instructions and examples how to use the class
*
* @author Stefan Wanzenried
* @copyright Stefan Wanzenried
* <www.everchanging.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
class GoogleAnalyticsAPI {
const API_URL = 'https://www.googleapis.com/analytics/v3/data/ga';
const WEBPROPERTIES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties';
const PROFILES_URL = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles';
public $auth = null;
protected $accessToken = '';
protected $accountId = '';
protected $assoc = true;
/**
* Default query parameters
*
*/
protected $defaultQueryParams = array();
/**
* Constructor
*
*/
public function __construct() {
if (!function_exists('curl_init')) throw new Exception('The curl extension for PHP is required.');
$this->auth = new GoogleOauth();
$this->defaultQueryParams = array(
'start-date' => date('Y-m-d', strtotime('-1 month')),
'end-date' => date('Y-m-d'),
'metrics' => 'ga:visits',
);
}
public function setAccessToken($token) {
$this->accessToken = $token;
}
public function setAccountId($id) {
$this->accountId = $id;
}
/**
* Set default query parameters
* Useful settings: start-date, end-date, max-results
*
* @access public
* @param array() $params
* @return void
*/
public function setDefaultQueryParams(array $params) {
$params = array_merge($this->defaultQueryParams, $params);
$this->defaultQueryParams = $params;
}
/**
* Return objects from json_decode instead of arrays
*
* @access public
* @param mixed $bool
* @return void
*/
public function returnObjects($bool) {
$this->assoc = !$bool;
$this->auth->returnObjects($bool);
}
/**
* Query the Analytics API
*
* @access protected
* @param array $params (default: array())
* @return void
*/
protected function _query($params=array()){
if (!$this->accessToken || !$this->accountId) {
throw new Exception('You must provide the accessToken and an accountId');
}
$_params = array_merge($this->defaultQueryParams, array('access_token' => $this->accessToken, 'ids' => $this->accountId));
$queryParams = array_merge($_params, $params);
$data = Http::curl(self::API_URL, $queryParams);
return json_decode($data, $this->assoc);
}
/**
* Query the Google Analytics API.
*
* @access public
* @param array $params (default: array()) Query parameters
* @return array data
*/
public function query($params=array()) {
return $this->_query($params);
}
/**
* Get all WebProperties
*
* @access public
* @return array data
*/
public function getWebProperties() {
if (!$this->accessToken) throw new Exception('You must provide the accessToken');
$data = Http::curl(self::WEBPROPERTIES_URL, array('access_token' => $this->accessToken));
return json_decode($data, $this->assoc);
}
/**
* Get all Profiles
*
* @access public
* @return array data
*/
public function getProfiles() {
if (!$this->accessToken) throw new Exception('You must provide the accessToken');
$data = Http::curl(self::PROFILES_URL, array('access_token' => $this->accessToken));
return json_decode($data, $this->assoc);
}
/*****************************************************************************************************************************
*
* The following methods implement queries for the most useful statistics, seperated by topics: Audience/Content/Traffic Sources
*
*****************************************************************************************************************************/
/*
* AUDIENCE
*/
public function getVisitsByDate($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:date',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getAudienceStatistics($params=array()) {
$defaults = array(
'metrics' => 'ga:visitors,ga:newVisits,ga:percentNewVisits,ga:visits,ga:bounces,ga:pageviews,ga:visitBounceRate,ga:timeOnSite,ga:avgTimeOnSite',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsByCountries($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:country',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsByCities($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:city',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsByLanguages($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:language',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsBySystemBrowsers($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:browser',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsBySystemOs($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:operatingSystem',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsBySystemResolutions($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:screenResolution',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsByMobileOs($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:operatingSystem',
'sort' => '-ga:visits',
'segment' => 'gaid::-11',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getVisitsByMobileResolutions($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:screenResolution',
'sort' => '-ga:visits',
'segment' => 'gaid::-11',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
/*
* CONTENT
*/
public function getPageviewsByDate($params=array()) {
$defaults = array(
'metrics' => 'ga:pageviews',
'dimensions' => 'ga:date',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getContentStatistics($params=array()) {
$defaults = array(
'metrics' => 'ga:pageviews,ga:uniquePageviews',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getContentTopPages($params=array()) {
$defaults = array(
'metrics' => 'ga:pageviews',
'dimensions' => 'ga:pagePath',
'sort' => '-ga:pageviews',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
/*
* TRAFFIC SOURCES
*/
public function getTrafficSources($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:medium',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getKeywords($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:keyword',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
public function getReferralTraffic($params=array()) {
$defaults = array(
'metrics' => 'ga:visits',
'dimensions' => 'ga:source',
'sort' => '-ga:visits',
);
$_params = array_merge($defaults, $params);
return $this->_query($_params);
}
}
/**
* GoogleOauth
*
* This class handles the authentication over oauth 2.0 with google.
* It provides methods to get the accessToken, refresh the accessToken and revoke access.
*
*/
class GoogleOauth {
const AUTH_URL = 'https://accounts.google.com/o/oauth2/auth';
const TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';
const REVOKE_URL = 'https://accounts.google.com/o/oauth2/revoke';
protected $clientId = '';
protected $clientSecret = '';
protected $redirectUri = '';
protected $assoc = true;
public function __construct() {}
public function setClientId($id) {
$this->clientId = $id;
}
public function setClientSecret($secret) {
$this->clientSecret = $secret;
}
public function setRedirectUri($uri) {
$this->redirectUri = $uri;
}
public function returnObjects($bool) {
$this->assoc = !$bool;
}
/**
* Build auth url
* The user has to login with his Google Account and give your app access to the Analytics API
*
* @access public
* @param array $params Custom parameters
* @return string The auth login-url
*/
public function buildAuthUrl($params = array()) {
if (!$this->clientId || !$this->redirectUri) {
throw new Exception('You must provide the clientId and a redirectUri');
}
$defaults = array(
'response_type' => 'code',
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUri,
'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
'access_type' => 'offline',
'approval_prompt' => 'force',
);
$params = array_merge($defaults, $params);
$url = self::AUTH_URL . "?" . http_build_query($params);
return $url;
}
/**
* Get the AccessToken in exchange with the code from the auth along with a refreshToken
*
* @access public
* @param mixed $code The code received with GET after auth
* @return array Array with the following keys: access_token, refresh_token, expires_in
*/
public function getAccessToken($code) {
if (!$this->clientId || !$this->clientSecret || !$this->redirectUri) {
throw new Exception('You must provide the clientId, clientSecret and a redirectUri');
}
$params = array(
'code' => $code,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'grant_type' => 'authorization_code',
);
$auth = Http::curl(self::TOKEN_URL, $params, true);
return json_decode($auth, $this->assoc);
}
/**
* Get a new accessToken with the refreshToken
*
* @access public
* @param mixed $refreshToken The refreshToken
* @return array Array with the following keys: access_token, expires_in
*/
public function refreshAccessToken($refreshToken) {
if (!$this->clientId || !$this->clientSecret) {
throw new Exception('You must provide the clientId and clientSecret');
}
$params = array(
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
);
$auth = Http::curl(self::TOKEN_URL, $params, true);
return json_decode($auth, $this->assoc);
}
/**
* Revoke access
*
* @access public
* @param mixed $token accessToken or refreshToken
* @return void
*/
public function revokeAccess($token) {
$params = array('token' => $token);
$data = Http::curl(self::REVOKE_URL, $params);
return json_decode($data, $this->assoc);
}
}
/**
* Http
*
* Provides a method to send http data with curl
*
*/
class Http {
/**
* Send http requests
*
* @access public
* @static
* @param mixed $url The url to send data
* @param array $params (default: array()) Array with key/value pairs to send
* @param bool $post (default: false) True when sending with POST
* @return void
*/
public static function curl($url, $params=array(), $post=false) {
if (empty($url)) return false;
if (!$post && !empty($params)) {
$url = $url . "?" . http_build_query($params);
}
$curl = curl_init($url);
if ($post) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$http_code = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE);
//Add the status code to the json data, useful for error-checking
$data = preg_replace('/^{/', '{"http_code":'.$http_code.',', $data);
curl_close($curl);
return $data;
}
}
?>