forked from wp-plugins/google-analyticator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.analytics.stats.php
287 lines (233 loc) · 8.49 KB
/
class.analytics.stats.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
<?php
/**
* Handles interactions with Google Analytics' Stat API
*
**/
class GoogleAnalyticsStats
{
var $client = false;
var $accountId;
var $baseFeed = 'https://www.googleapis.com/analytics/v3';
var $token = false;
/**
* Constructor
*
* @param token - a one-time use token to be exchanged for a real token
**/
function GoogleAnalyticsStats()
{
# Include SimplePie if it doesn't exist
if ( !class_exists('SimplePie') ) {
require_once (ABSPATH . WPINC . '/class-feed.php');
}
if ( !class_exists('Google_Client') ) {
require_once 'google-api-php-client/src/Google_Client.php';
}
if ( !class_exists('Google_AnalyticsService') ) {
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
}
$this->client = new Google_Client();
$this->client->setApprovalPrompt("force");
$this->client->setAccessType('offline');
$this->client->setClientId(GOOGLE_ANALYTICATOR_CLIENTID);
$this->client->setClientSecret(GOOGLE_ANALYTICATOR_CLIENTSECRET);
$this->client->setRedirectUri(GOOGLE_ANALYTICATOR_REDIRECT);
$this->client->setScopes(array("https://www.googleapis.com/auth/analytics"));
// Magic. Returns objects from the Analytics Service instead of associative arrays.
$this->client->setUseObjects(true);
try {
$this->analytics = new Google_AnalyticsService($this->client);
}
catch (Google_ServiceException $e)
{
print '(cas:48) There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();
return false;
}
}
function checkLogin()
{
$ga_google_authtoken = get_option('ga_google_authtoken');
if (!empty($ga_google_authtoken))
{
try
{
$this->client->setAccessToken($ga_google_authtoken);
}
catch( Google_AuthException $e )
{
print '(cas:72) Google Analyticator was unable to authenticate you with
Google using the Auth Token you pasted into the input box on the previous step. <br><br>
This could mean either you pasted the token wrong, or the time/date on your server is wrong,
or an SSL issue preventing Google from Authenticating. <br><br>
<a href="' . admin_url('/options-general.php?page=ga_reset').'"> Try Deauthorizing & Resetting Google Analyticator.</a>
<br><br><strong>Tech Info </strong> ' . $e->getCode() . ':' . $e->getMessage();
return false;
}
}
else
{
$authCode = get_option('ga_google_token');
if (empty($authCode)) return false;
try
{
$accessToken = $this->client->authenticate($authCode);
}
catch( Exception $e )
{
print '(cas:72) Google Analyticator was unable to authenticate you with
Google using the Auth Token you pasted into the input box on the previous step. <br><br>
This could mean either you pasted the token wrong, or the time/date on your server is wrong,
or an SSL issue preventing Google from Authenticating. <br><br>
<a href="' . admin_url('/options-general.php?page=ga_reset').'"> Try Deauthorizing & Resetting Google Analyticator.</a>
<br><br><strong>Tech Info </strong> ' . $e->getCode() . ':' . $e->getMessage();
return false;
}
if($accessToken)
{
$this->client->setAccessToken($accessToken);
update_option('ga_google_authtoken', $accessToken);
}
else
{
return false;
}
}
$this->token = $this->client->getAccessToken();
return true;
}
function deauthorize()
{
update_option('ga_google_token', '');
update_option('ga_google_authtoken', '');
}
function getSingleProfile()
{
$webproperty_id = get_option('ga_uid');
list($pre, $account_id, $post) = explode('-',$webproperty_id);
if (empty($webproperty_id)) return false;
try {
$profiles = $this->analytics->management_profiles->listManagementProfiles($account_id, $webproperty_id);
}
catch (Google_ServiceException $e)
{
print 'There was an Analytics API service error ' . $e->getCode() . ': ' . $e->getMessage();
return false;
}
$profile_id = $profiles->items[0]->id;
if (empty($profile_id)) return false;
$account_array = array();
array_push($account_array, array('id'=>$profile_id, 'ga:webPropertyId'=>$webproperty_id));
return $account_array;
}
function getAllProfiles()
{
$profile_array = array();
try {
$profiles = $this->analytics->management_webproperties->listManagementWebproperties('~all');
}
catch (Google_ServiceException $e)
{
print 'There was an Analytics API service error ' . $e->getCode() . ': ' . $e->getMessage();
}
if( !empty( $profiles->items ) )
{
foreach( $profiles->items as $profile )
{
$profile_array[ $profile->id ] = str_replace('http://','',$profile->name );
}
}
return $profile_array;
}
function getAnalyticsAccounts()
{
$analytics = new Google_AnalyticsService($this->client);
$accounts = $analytics->management_accounts->listManagementAccounts();
$account_array = array();
$items = $accounts->getItems();
if (count($items) > 0) {
foreach ($items as $key => $item)
{
$account_id = $item->getId();
$webproperties = $analytics->management_webproperties->listManagementWebproperties($account_id);
if (!empty($webproperties))
{
foreach ($webproperties->getItems() as $webp_key => $webp_item) {
$profiles = $analytics->management_profiles->listManagementProfiles($account_id, $webp_item->id);
$profile_id = $profiles->items[0]->id;
array_push($account_array, array('id'=>$profile_id, 'ga:webPropertyId'=>$webp_item->id));
}
}
}
return $account_array;
}
return false;
}
/**
* Sets the account id to use for queries
*
* @param id - the account id
**/
function setAccount($id)
{
$this->accountId = $id;
}
/**
* Get a specific data metrics
*
* @param metrics - the metrics to get
* @param startDate - the start date to get
* @param endDate - the end date to get
* @param dimensions - the dimensions to grab
* @param sort - the properties to sort on
* @param filter - the property to filter on
* @param limit - the number of items to get
* @return the specific metrics in array form
**/
function getMetrics($metric, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false)
{
$analytics = new Google_AnalyticsService($this->client);
$params = array();
if ($dimensions)
{
$params['dimensions'] = $dimensions;
}
if ($sort)
{
$params['sort'] = $sort;
}
if ($filter)
{
$params['filters'] = $filter;
}
if ($limit)
{
$params['max-results'] = $limit;
}
// Just incase, the ga: is still used in the account id, strip it out to prevent it breaking
$filtered_id = str_replace( 'ga:', '', $this->accountId );
if(!$filtered_id){
echo 'Error - Account ID is blank';
return false;
}
return $analytics->data_ga->get(
'ga:'.$filtered_id,
$startDate,
$endDate,
$metric,
$params
);
}
/**
* Checks the date against Jan. 1 2005 because GA API only works until that date
*
* @param date - the date to compare
* @return the correct date
**/
function verifyStartDate($date)
{
if ( strtotime($date) > strtotime('2005-01-01') )
return $date;
else
return '2005-01-01';
}
} // END class