Version Description
- Fixed: Conflict with some other plugins using Google API PHP library
- Fixed: Unable to authorize again after logout
- Fixed: JS errors on page/post editor if user not authenticated
- Change: Introducing Backup WD plugin
Download this release
Release Info
Developer | webdorado |
Plugin | WD Google Analytics |
Version | 1.1.2 |
Comparing to | |
See all releases |
Code changes from version 1.1.1 to 1.1.2
- admin/gawd_google_class.php +1667 -1605
- assets/logo.png +0 -0
- gawd_class.php +20 -2
- google-analytics-wd.php +59 -2
- google/vendor/autoload.php +1 -1
- google/vendor/composer/autoload_real.php +7 -7
- google/vendor/composer/autoload_static.php +5 -5
- inc/css/wd_bp_install.css +46 -0
- inc/js/wd_bp_install.js +12 -0
- readme.txt +7 -1
admin/gawd_google_class.php
CHANGED
@@ -2,1777 +2,1839 @@
|
|
2 |
|
3 |
require_once(GAWD_DIR . '/google/vendor/autoload.php');
|
4 |
|
5 |
-
class GAWD_google_client
|
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 |
-
private function set_google_client() {
|
37 |
|
38 |
-
|
39 |
-
if($access_token != ''){
|
40 |
-
$this->google_client->setAccessToken($access_token);
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
-
$this->google_client->setClientId(GAWD::get_instance()->get_project_client_id());
|
46 |
-
$this->google_client->setClientSecret(GAWD::get_instance()->get_project_client_secret());
|
47 |
-
$this->google_client->setRedirectUri(GAWD::get_instance()->redirect_uri);
|
48 |
-
// $this->google_client->setAuthConfigFile(GAWD_DIR . '/client_secrets.json');
|
49 |
-
$this->google_client->refreshToken($refresh_token);
|
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 |
-
$client->authenticate($code);
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
/* if ($profiles instanceof Google_Service_Exception) {
|
94 |
-
|
95 |
-
delete_option('gawd_user_data');
|
96 |
-
$errors = $profiles->getErrors();
|
97 |
-
return $errors[0]["message"];
|
98 |
-
}*/
|
99 |
-
|
100 |
-
|
101 |
-
update_option('gawd_user_data', array(
|
102 |
-
'access_token' => $access_token,
|
103 |
-
'refresh_token' => $refresh_token,
|
104 |
-
'gawd_profiles' => $profiles,
|
105 |
-
)
|
106 |
-
);
|
107 |
-
return true;
|
108 |
-
}
|
109 |
-
} catch (Google_Service_Exception $e) {
|
110 |
-
delete_option('gawd_user_data');
|
111 |
-
$errors = $e->getErrors();
|
112 |
-
return $errors[0]["message"];
|
113 |
-
} catch (Exception $e) {
|
114 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
115 |
-
$fh = fopen($myFile, 'a');
|
116 |
-
fwrite($fh, $e->getMessage(). "----authenticate function".PHP_EOL);
|
117 |
-
fclose($fh);
|
118 |
-
return $e->getMessage();
|
119 |
}
|
120 |
}
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
$permissions = $account['permissions']['effective'];
|
142 |
-
foreach ($permissions as $permission) {
|
143 |
-
if ($permission == 'EDIT') {
|
144 |
-
$edit_flag = TRUE;
|
145 |
-
}
|
146 |
-
}
|
147 |
-
$accounts_light[] = array(
|
148 |
-
'name' => $account['name'],
|
149 |
-
'id' => $account['id'],
|
150 |
-
'edit_permissions' => $edit_flag
|
151 |
-
);
|
152 |
-
/*if ($edit_flag == TRUE) {
|
153 |
-
$accounts_light[] = array(
|
154 |
-
'name' => $account['name'],
|
155 |
-
'id' => $account['id']
|
156 |
-
);
|
157 |
-
}*/
|
158 |
-
}
|
159 |
-
}catch (Google_Service_Exception $e) {
|
160 |
-
//return $e->getErrors()[0]["message"];
|
161 |
-
} catch (Exception $e) {
|
162 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
163 |
-
$fh = fopen($myFile, 'a');
|
164 |
-
fwrite($fh, $e->getMessage(). "----get_management_accounts function".PHP_EOL);
|
165 |
-
fclose($fh);
|
166 |
-
//return $e->getMessage();
|
167 |
-
}
|
168 |
-
return $accounts_light;
|
169 |
}
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
foreach ($web_properties as $web_property) {
|
188 |
-
$current_url = $web_property['websiteUrl'];
|
189 |
-
if (($current_url == $site_url) || (($current_url . '/') == $site_url)) {
|
190 |
-
$exact_properties[] = $web_property;
|
191 |
-
}
|
192 |
-
}
|
193 |
-
if (!empty($exact_properties)) {
|
194 |
-
return $exact_properties;
|
195 |
-
} else {
|
196 |
-
return 'no_matches';
|
197 |
-
}
|
198 |
}
|
|
|
|
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
$
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
}
|
217 |
|
218 |
-
|
|
|
|
|
|
|
|
|
219 |
|
220 |
-
$analytics = $this->analytics_member;
|
221 |
-
$websiteUrl = get_site_url();
|
222 |
try {
|
223 |
-
|
224 |
-
$property->setName($name);
|
225 |
-
$property->setWebsiteUrl($websiteUrl);
|
226 |
-
$analytics->management_webproperties->insert($accountId, $property);
|
227 |
} catch (apiServiceException $e) {
|
228 |
-
|
229 |
-
|
230 |
} catch (apiException $e) {
|
231 |
-
|
232 |
-
|
233 |
}
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
$analytics->management_profiles->insert($accountId, $web_property['id'], $profile);
|
253 |
-
} catch (apiServiceException $e) {
|
254 |
-
print 'There was an Analytics API service error '
|
255 |
-
. $e->getCode() . ':' . $e->getMessage();
|
256 |
-
} catch (apiException $e) {
|
257 |
-
print 'There was a general API error '
|
258 |
-
. $e->getCode() . ':' . $e->getMessage();
|
259 |
-
}
|
260 |
-
$current_profiles = $this->analytics_member->management_profiles->listManagementProfiles($accountId,$web_property['id'])->getItems();
|
261 |
-
try {
|
262 |
-
$property = new Google_Service_Analytics_Webproperty();
|
263 |
-
$property->setName($name);
|
264 |
-
$property->setWebsiteUrl($websiteUrl);
|
265 |
-
$property->setDefaultProfileId($current_profiles[0]['id']);
|
266 |
-
$analytics->management_webproperties->update($accountId, $web_property['id'], $property);
|
267 |
-
} catch (apiServiceException $e) {
|
268 |
-
print 'There was an Analytics API service error '
|
269 |
-
. $e->getCode() . ':' . $e->getMessage();
|
270 |
-
} catch (apiException $e) {
|
271 |
-
print 'There was a general API error '
|
272 |
-
. $e->getCode() . ':' . $e->getMessage();
|
273 |
-
}
|
274 |
-
catch (Google_Service_Exception $e) {
|
275 |
-
return 'There was a general API error '
|
276 |
-
. $e->getCode() . ':' . $e->getMessage();
|
277 |
-
}catch (Exception $e) {
|
278 |
-
echo $e->getCode() . ':' . $e->getMessage();
|
279 |
-
}
|
280 |
-
}
|
281 |
}
|
282 |
-
|
283 |
}
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
try{
|
298 |
-
|
299 |
-
if($this->analytics_member && $this->analytics_member->management_webproperties) {
|
300 |
-
$web_properties = $this->analytics_member->management_webproperties->listManagementWebproperties( '~all' )->getItems();
|
301 |
-
$profiles = $this->analytics_member->management_profiles->listManagementProfiles( '~all', '~all' )->getItems();
|
302 |
-
$profiles_count = count( $profiles );
|
303 |
-
$web_properties_count = count( $web_properties );
|
304 |
-
for ( $i = 0; $i < $web_properties_count; $i ++ ) {
|
305 |
-
for ( $j = 0; $j < $profiles_count; $j ++ ) {
|
306 |
-
if ( $web_properties[ $i ]['id'] == $profiles[ $j ]['webPropertyId'] ) {
|
307 |
-
$profiles_light[ $web_properties[ $i ]['name'] ][] = array(
|
308 |
-
'id' => $profiles[ $j ]['id'],
|
309 |
-
'name' => $profiles[ $j ]['name'],
|
310 |
-
'webPropertyId' => $profiles[ $j ]['webPropertyId'],
|
311 |
-
'websiteUrl' => $profiles[ $j ]['websiteUrl'],
|
312 |
-
'accountId' => $profiles[ $j ]['accountId']
|
313 |
-
);
|
314 |
-
}
|
315 |
-
}
|
316 |
-
}
|
317 |
-
}else{
|
318 |
-
return 'Something went wrong';
|
319 |
-
}
|
320 |
-
if (!isset($gawd_user_data['gawd_id']) || $gawd_user_data['gawd_id'] == '' || $gawd_user_data['gawd_id'] == NULL) {
|
321 |
-
if (!empty($profiles_light)) {
|
322 |
-
$first_profiles = reset($profiles_light);
|
323 |
-
$first_profile = $first_profiles[0];
|
324 |
-
$gawd_user_data['gawd_id'] = $first_profile['id'];
|
325 |
-
$gawd_user_data['webPropertyId'] = $first_profile['webPropertyId'];
|
326 |
-
$gawd_user_data['accountId'] = $first_profile['accountId'];
|
327 |
-
$gawd_user_data['web_property_name'] = $web_properties[0]['name'];
|
328 |
-
}
|
329 |
-
}
|
330 |
-
|
331 |
-
$gawd_user_data['gawd_profiles'] = $profiles_light;
|
332 |
-
update_option('gawd_user_data', $gawd_user_data);
|
333 |
-
set_transient('gawd_user_profiles',$profiles_light, 60);
|
334 |
-
return $profiles_light;
|
335 |
-
}catch(Google_Service_Exception $e){
|
336 |
-
return $e;
|
337 |
-
}catch(Exception $e){
|
338 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
339 |
-
$fh = fopen($myFile, 'a');
|
340 |
-
fwrite($fh, $e->getMessage(). "----get_profiles function".PHP_EOL);
|
341 |
-
fclose($fh);
|
342 |
-
return $e;
|
343 |
-
}
|
344 |
}
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
if ($
|
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 |
else {
|
382 |
-
|
383 |
-
$dimensions_light[] = array(
|
384 |
-
'name' => $dimension['name'],
|
385 |
-
'id' => $dimension['id']
|
386 |
-
);
|
387 |
-
}
|
388 |
-
$supported_dimensions = array("Logged in", "Post type", "Author", "Category", "Tags", "Published Month", "Published Year");
|
389 |
-
$dimensions = array();
|
390 |
-
foreach ($dimensions_light as $dimension) {
|
391 |
-
foreach ($supported_dimensions as $supported_dimension) {
|
392 |
-
if (trim(strtolower($dimension['name'])) == strtolower($supported_dimension)) {
|
393 |
-
$dimension['name'] = $supported_dimension;
|
394 |
-
$dimension['id'] = substr($dimension['id'], -1);
|
395 |
-
$dimensions[] = $dimension;
|
396 |
-
}
|
397 |
-
}
|
398 |
-
}
|
399 |
-
if ($default == 'default') {
|
400 |
-
update_option('gawd_custom_dimensions', $dimensions);
|
401 |
-
}
|
402 |
-
set_transient('gawd-custom-dimensions-' . $webPropertyId, json_encode($dimensions_light), 12 * HOUR_IN_SECONDS);
|
403 |
-
return $dimensions_light;
|
404 |
}
|
|
|
405 |
}
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
426 |
}
|
|
|
|
|
427 |
update_option('gawd_custom_dimensions', $dimensions);
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
return "no_custom_dimensions_exist";
|
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 |
-
$name = get_the_author_meta('user_nicename');
|
459 |
-
$value = trim($name);
|
460 |
-
return $value;
|
461 |
}
|
|
|
|
|
|
|
|
|
462 |
}
|
|
|
463 |
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
if ($categories) {
|
469 |
-
foreach ($categories as $category) {
|
470 |
-
$category_names[] = $category->slug;
|
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 |
-
public static function gawd_cd_published_year() {
|
499 |
-
if (is_singular()) {
|
500 |
-
return get_the_date('Y');
|
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 |
-
return $all_filters;
|
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 |
-
|
554 |
-
|
555 |
-
|
556 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
557 |
-
$fh = fopen($myFile, 'a');
|
558 |
-
fwrite($fh, $e->getMessage(). "----get_management_goals function".PHP_EOL);
|
559 |
-
fclose($fh);
|
560 |
-
|
561 |
-
}
|
562 |
-
if (0 == sizeof($goals)) {
|
563 |
-
return "no_goals_exist";
|
564 |
-
} else {
|
565 |
-
foreach ($goals as $goal) {
|
566 |
-
$goals_light[] = array(
|
567 |
-
'name' => $goal['name'],
|
568 |
-
'id' => $goal['id']
|
569 |
-
);
|
570 |
-
}
|
571 |
-
return $goals_light;
|
572 |
-
}
|
573 |
}
|
574 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
575 |
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
$
|
587 |
-
|
588 |
-
fclose($fh);
|
589 |
-
|
590 |
}
|
591 |
-
if (
|
592 |
-
|
593 |
-
}
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
605 |
}
|
606 |
-
$
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
$caseSensitive = $goal["modelData"]['urlDestinationDetails']['caseSensitive'];
|
624 |
-
}
|
625 |
-
elseif($goal['type'] == 'VISIT_TIME_ON_SITE'){
|
626 |
-
$type = 'Duration';
|
627 |
-
if($goal["modelData"]['visitTimeOnSiteDetails']['comparisonType'] == 'GREATER_THAN'){
|
628 |
-
$match_type = 'Greater than';
|
629 |
-
}
|
630 |
-
$value = $goal["modelData"]['visitTimeOnSiteDetails']['comparisonValue'];
|
631 |
-
$hours = strlen(floor($value / 3600)) < 2 ? '0' . floor($value / 3600) : floor($value / 3600);
|
632 |
-
$mins = strlen(floor($value / 60 % 60)) < 2 ? '0' . floor($value / 60 % 60) : floor($value / 60 % 60);
|
633 |
-
$secs = strlen(floor($value % 60)) < 2 ? '0' . floor($value % 60) : floor($value % 60);
|
634 |
-
$value = $hours.':'.$mins.':'.$secs;
|
635 |
-
}
|
636 |
-
else{
|
637 |
-
$type = 'Pages/Screens per session';
|
638 |
-
if($goal["modelData"]['visitNumPagesDetails']['comparisonType'] == 'GREATER_THAN'){
|
639 |
-
$match_type = 'Greater than';
|
640 |
-
}
|
641 |
-
$value = $goal["modelData"]['visitNumPagesDetails']['comparisonValue'];
|
642 |
-
}
|
643 |
-
|
644 |
-
$goals_light[$profile][] = array(
|
645 |
-
'name' => $goal['name'],
|
646 |
-
'id' => $goal['id'],
|
647 |
-
'type' => $type,
|
648 |
-
'match_type' => $match_type,
|
649 |
-
'profileID' => $goal['profileId'],
|
650 |
-
'caseSensitive' => $caseSensitive,
|
651 |
-
'value' => $value,
|
652 |
-
);
|
653 |
-
}
|
654 |
-
}
|
655 |
}
|
656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
657 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
658 |
}
|
659 |
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
680 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
681 |
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
688 |
|
689 |
-
|
690 |
-
|
691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
692 |
}
|
693 |
}
|
694 |
-
|
695 |
-
|
696 |
-
public function add_goal($gawd_goal_profile, $goal_max_id, $gawd_goal_type, $gawd_goal_name, $gawd_goal_comparison = "GREATER_THAN", $gawd_goal_value, $url_case_sensitve = 'false') {
|
697 |
-
$this->gawd_user_data = get_option('gawd_user_data');
|
698 |
-
/* This request creates a new Goal. */
|
699 |
-
// Construct the body of the request.
|
700 |
-
$goal = new Google_Service_Analytics_Goal();
|
701 |
-
$goal->setId($goal_max_id); //ID
|
702 |
-
$goal->setActive(True); //ACTIVE/INACTIVE
|
703 |
-
$goal->setType($gawd_goal_type); //URL_DESTINATION, VISIT_TIME_ON_SITE, VISIT_NUM_PAGES, AND EVENT
|
704 |
-
$goal->setName($gawd_goal_name); //NAME
|
705 |
-
// Construct the time on site details.
|
706 |
-
if ($gawd_goal_type == 'VISIT_TIME_ON_SITE') {
|
707 |
-
$details = new Google_Service_Analytics_GoalVisitTimeOnSiteDetails();
|
708 |
-
$details->setComparisonType($gawd_goal_comparison); //VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
709 |
-
$details->setComparisonValue($gawd_goal_value);
|
710 |
-
$goal->setVisitTimeOnSiteDetails($details);
|
711 |
-
} elseif ($gawd_goal_type == 'URL_DESTINATION') {
|
712 |
-
if($url_case_sensitve != ''){
|
713 |
-
$url_case_sensitve = true;
|
714 |
-
}
|
715 |
-
$details = new Google_Service_Analytics_GoalUrlDestinationDetails();
|
716 |
-
$details->setCaseSensitive($url_case_sensitve);
|
717 |
-
$details->setFirstStepRequired('false');
|
718 |
-
$details->setMatchType($gawd_goal_comparison);
|
719 |
-
$details->setUrl($gawd_goal_value);
|
720 |
-
$goal->setUrlDestinationDetails($details);
|
721 |
-
} elseif ($gawd_goal_type == 'VISIT_NUM_PAGES') {
|
722 |
-
$details = new Google_Service_Analytics_GoalVisitNumPagesDetails();
|
723 |
-
$details->setComparisonType($gawd_goal_comparison); //VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
724 |
-
$details->setComparisonValue($gawd_goal_value);
|
725 |
-
$goal->setVisitNumPagesDetails($details);
|
726 |
-
} elseif ($gawd_goal_type == 'EVENT') {
|
727 |
-
/* $details = new Google_Service_Analytics_GoalEventDetails();
|
728 |
-
$details = new Google_Service_Analytics_GoalEventDetailsEventConditions();
|
729 |
-
$detailssetComparisonType
|
730 |
-
//$details->setEventConditions($gawd_goal_comparison);//VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
731 |
-
//$details->setUseEventValue($gawd_goal_value); */
|
732 |
-
// $goal->setEventDetails($details);
|
733 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
734 |
|
735 |
-
|
736 |
-
|
737 |
-
$this->gawd_user_data = get_option('gawd_user_data');
|
738 |
-
$accountId = $this->get_default_accountId();
|
739 |
-
$webPropertyId = $this->get_default_webPropertyId();
|
740 |
-
$profileId = $gawd_goal_profile;
|
741 |
-
$analytics = $this->analytics_member;
|
742 |
-
try {
|
743 |
-
$analytics->management_goals->insert($accountId, $webPropertyId, $profileId, $goal);
|
744 |
-
} catch (apiServiceException $e) {
|
745 |
-
echo 'There was an Analytics API service error '
|
746 |
-
. $e->getCode() . ':' . $e->getMessage();
|
747 |
-
} catch (apiException $e) {
|
748 |
-
echo 'There was a general API error '
|
749 |
-
. $e->getCode() . ':' . $e->getMessage();
|
750 |
-
}
|
751 |
-
catch (Exception $e) {
|
752 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
753 |
-
$fh = fopen($myFile, 'a');
|
754 |
-
fwrite($fh, $e->getMessage(). "----add goal function".PHP_EOL);
|
755 |
-
fclose($fh);
|
756 |
-
$error = array('error_message' => $e->getMessage());
|
757 |
-
if (strpos($e->getMessage(), 'User does not have permission to perform this operation')) {
|
758 |
-
$error['error_message'] = 'User does not have permission to perform this operation';
|
759 |
}
|
760 |
-
|
761 |
-
|
762 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
763 |
|
764 |
-
|
765 |
-
$this->gawd_user_data = get_option('gawd_user_data');
|
766 |
-
$accountId = $this->get_profile_accountId();
|
767 |
-
$profileId = $this->get_profile_id();
|
768 |
-
$webPropertyId = $this->get_profile_webPropertyId();
|
769 |
-
$analytics = $this->analytics_member;
|
770 |
-
$condition = $type == 'GEO_IP_ADDRESS' ? 'EQUAL' : 'MATCHES';
|
771 |
-
/**
|
772 |
-
* Note: This code assumes you have an authorized Analytics service object.
|
773 |
-
* See the Filters Developer Guide for details.
|
774 |
-
*/
|
775 |
-
/**
|
776 |
-
* This request creates a new filter.
|
777 |
-
*/
|
778 |
-
try {
|
779 |
-
// Construct the filter expression object.
|
780 |
-
$details = new Google_Service_Analytics_FilterExpression();
|
781 |
-
$details->setField($type);
|
782 |
-
$details->setMatchType($type);
|
783 |
-
$details->setExpressionValue($value);
|
784 |
-
$details->setCaseSensitive(false);
|
785 |
-
// Construct the filter and set the details.
|
786 |
-
$filter = new Google_Service_Analytics_Filter();
|
787 |
-
$filter->setName($name);
|
788 |
-
$filter->setType("EXCLUDE");
|
789 |
-
$filter->setExcludeDetails($details);
|
790 |
-
|
791 |
-
$insertedFilter = $analytics->management_filters->insert($accountId, $filter);
|
792 |
-
$analyticsFilterRef = new Google_Service_Analytics_FilterRef();
|
793 |
-
$analyticsFilterRef->setId($insertedFilter->id);
|
794 |
-
$filterData = new Google_Service_Analytics_ProfileFilterLink();
|
795 |
-
$filterData->setFilterRef($analyticsFilterRef );
|
796 |
-
// Add view to inserted filter
|
797 |
-
$res = $analytics->management_profileFilterLinks->insert($accountId, $webPropertyId, $profileId, $filterData);
|
798 |
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
} catch (apiException $e) {
|
803 |
-
echo 'There was a general API error '
|
804 |
-
. $e->getCode() . ':' . $e->getMessage();
|
805 |
-
} catch (Exception $e) {
|
806 |
|
807 |
-
|
808 |
-
$
|
809 |
-
|
810 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
811 |
|
812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
813 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
814 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
815 |
|
816 |
-
|
817 |
-
|
818 |
-
$analytics = $this->analytics_member;
|
819 |
-
$metric = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration';
|
820 |
-
|
821 |
-
try {
|
822 |
-
$results = $analytics->data_ga->get(
|
823 |
-
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
824 |
-
'dimensions' => 'ga:' . $dimension,
|
825 |
-
'sort' => 'ga:' . $dimension,
|
826 |
-
'filters' => 'ga:' . $geo_type . '==' . $country_filter
|
827 |
-
)
|
828 |
-
);
|
829 |
-
} catch (Exception $e) {
|
830 |
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
835 |
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
);
|
854 |
-
$data[$j]['No'] = floatval($j + 1);
|
855 |
-
for ($i = 0; $i < count($metric); $i++) {
|
856 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = floatval($row[$i + 1]);
|
857 |
-
}
|
858 |
-
$j++;
|
859 |
-
}
|
860 |
-
} else {
|
861 |
-
$empty[0] = array(
|
862 |
-
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => 0
|
863 |
-
);
|
864 |
-
$empty[0]['No'] = 1;
|
865 |
-
for ($i = 0; $i < count($metric); $i++) {
|
866 |
-
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
867 |
-
}
|
868 |
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
878 |
}
|
879 |
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
$metric =
|
884 |
-
|
885 |
-
$diff = date_diff(date_create($start_date),date_create($end_date));
|
886 |
-
if(intval($diff->format("%a")) > 7){
|
887 |
-
$dimension = 'week';
|
888 |
-
}
|
889 |
-
if(intval($diff->format("%a")) > 60){
|
890 |
-
$dimension = 'month';
|
891 |
-
}
|
892 |
-
}
|
893 |
-
// Get the results from the Core Reporting API and print the results.
|
894 |
-
// Calls the Core Reporting API and queries for the number of sessions
|
895 |
-
// for the last seven days.
|
896 |
-
|
897 |
-
$filter_type = 'pagePath';
|
898 |
-
|
899 |
-
try {
|
900 |
-
$results = $analytics->data_ga->get(
|
901 |
-
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
902 |
-
'dimensions' => 'ga:' . $dimension,
|
903 |
-
'sort' => 'ga:' . $dimension,
|
904 |
-
'filters' => 'ga:' . $filter_type . '=~' . $filter
|
905 |
-
)
|
906 |
-
);
|
907 |
-
} catch (Exception $e) {
|
908 |
-
|
909 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
910 |
-
$fh = fopen($myFile, 'a');
|
911 |
-
fwrite($fh, $e->getMessage(). "----get_post_page_data function".PHP_EOL);
|
912 |
-
fclose($fh);
|
913 |
-
|
914 |
-
$error = array('error_message' => 'Error');
|
915 |
-
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
916 |
-
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
917 |
-
}
|
918 |
-
return json_encode($error);
|
919 |
-
}
|
920 |
-
|
921 |
-
$rows = $results->getRows();
|
922 |
-
$metric = explode(',', $metric);
|
923 |
-
if ($rows) {
|
924 |
-
$j = 0;
|
925 |
-
$data_sum = array();
|
926 |
-
/*if ($dimension == 'week') {
|
927 |
-
$date = $start_date;
|
928 |
-
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
929 |
-
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
930 |
-
$end_date = date("M d,Y");
|
931 |
-
}
|
932 |
-
foreach ($rows as $row) {
|
933 |
-
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
934 |
-
|
935 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
936 |
-
// echo $date;
|
937 |
-
}
|
938 |
-
else {
|
939 |
-
|
940 |
-
if (strtotime($date) != strtotime($end_date)) {
|
941 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
942 |
-
} else {
|
943 |
-
break;
|
944 |
-
}
|
945 |
-
}
|
946 |
-
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
947 |
-
$data[$j]['No'] = floatval($j + 1);
|
948 |
-
for ($i = 0; $i < count($metric); $i++) {
|
949 |
-
$val = $i + 1;
|
950 |
-
$metric_val = floatval($row[$val]);
|
951 |
-
if(substr($metric[$i], 3) == 'bounceRate'){
|
952 |
-
$metric_val = $metric_val;
|
953 |
-
}
|
954 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
955 |
-
}
|
956 |
-
$j++;
|
957 |
-
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
958 |
-
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
959 |
-
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
960 |
-
$_end_date = $end_date;
|
961 |
-
}
|
962 |
-
}
|
963 |
-
}*/
|
964 |
-
if ($dimension == 'week' || $dimension == 'month') {
|
965 |
-
$date = $start_date;
|
966 |
-
if ($dimension == 'week') {
|
967 |
-
$_end_date = date("l", strtotime($date)) == 'Saturday' ? date("M d,Y", strtotime($date)) : date("M d,Y", strtotime('next Saturday ' . $date));
|
968 |
-
}
|
969 |
-
elseif ($dimension == 'month') {
|
970 |
-
$_end_date = date("M t,Y", strtotime($date));
|
971 |
-
if(strtotime($_end_date) > strtotime(date('Y-m-d'))){
|
972 |
-
$_end_date = date("M d,Y",strtotime('-1 day ' . date('Y-m-d')));
|
973 |
-
}
|
974 |
-
}
|
975 |
-
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
976 |
-
$end_date = date("M d,Y");
|
977 |
-
}
|
978 |
-
foreach ($rows as $row) {
|
979 |
-
if ($dimension == 'hour') {
|
980 |
-
$dimension_value = date("M d,Y", strtotime($row[0])) . ' ' . $row[1] . ':00';
|
981 |
-
}
|
982 |
-
else {
|
983 |
-
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
984 |
-
|
985 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
986 |
-
// echo $date;
|
987 |
-
} else {
|
988 |
-
if ($dimension == 'month') {
|
989 |
-
//continue;
|
990 |
-
}
|
991 |
-
if (strtotime($date) != strtotime($end_date) ) {
|
992 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
993 |
-
} else {
|
994 |
-
break;
|
995 |
-
}
|
996 |
-
}
|
997 |
-
}
|
998 |
-
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
999 |
-
$data[$j]['No'] = floatval($j + 1);
|
1000 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1001 |
-
$val = $i + 1;
|
1002 |
-
if ($dimension == 'hour') {
|
1003 |
-
$val = $i + 2;
|
1004 |
-
}
|
1005 |
-
$metric_val = floatval($row[$val]);
|
1006 |
-
if(substr($metric[$i], 3) == 'bounceRate'){
|
1007 |
-
$metric_val = $metric_val;
|
1008 |
-
}
|
1009 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
1010 |
-
}
|
1011 |
-
|
1012 |
-
$j++;
|
1013 |
-
|
1014 |
-
if(isset($break) && $break){
|
1015 |
-
break;
|
1016 |
-
}
|
1017 |
-
|
1018 |
-
if ($dimension == 'week') {
|
1019 |
-
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
1020 |
-
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
1021 |
-
} elseif ($dimension == 'month') {
|
1022 |
-
$date = date("M d,Y", strtotime('+1 day ' . $_end_date));
|
1023 |
-
$_end_date = date("M t,Y", strtotime($date));
|
1024 |
-
}
|
1025 |
-
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
1026 |
-
$_end_date = date("M d,Y", strtotime($end_date));
|
1027 |
-
$break = true;
|
1028 |
-
}
|
1029 |
-
}
|
1030 |
-
}
|
1031 |
-
else{
|
1032 |
-
foreach ($rows as $row) {
|
1033 |
-
|
1034 |
-
if ($dimension == 'date') {
|
1035 |
-
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1036 |
-
}
|
1037 |
-
$data[$j] = array(
|
1038 |
-
$dimension => $row[0]
|
1039 |
-
);
|
1040 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1041 |
-
$data[$j][substr($metric[$i], 3)] = floatval($row[$i + 1]);
|
1042 |
-
if (isset($data_sum[substr($metric[$i], 3)])) {
|
1043 |
-
$data_sum[substr($metric[$i], 3)] += floatval($row[$i + 1]);
|
1044 |
-
} else {
|
1045 |
-
if (substr($metric[$i], 3) != 'percentNewSessions' && substr($metric[$i], 3) != 'bounceRate') {
|
1046 |
-
$data_sum[substr($metric[$i], 3)] = floatval($row[$i + 1]);
|
1047 |
-
}
|
1048 |
-
}
|
1049 |
-
}
|
1050 |
-
$j++;
|
1051 |
-
}
|
1052 |
-
}
|
1053 |
-
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1054 |
-
if (isset($same_dimension) && $same_dimension != null) {
|
1055 |
-
$dimension = $same_dimension;
|
1056 |
-
}
|
1057 |
-
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1058 |
-
set_transient('gawd-page-post-' . $profileId . '-' . $filter . '-' . '-' . $dimension . '-' . $start_date . '-' . $end_date . '-' . $chart, json_encode($result), $expiration);
|
1059 |
-
return json_encode($result);
|
1060 |
-
} else {
|
1061 |
-
$empty[0][$dimension] = date("Y-m-d");
|
1062 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1063 |
-
$empty[0][trim(str_replace(" ", "", preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1064 |
-
}
|
1065 |
-
return json_encode($empty);
|
1066 |
-
}
|
1067 |
}
|
1068 |
|
1069 |
-
|
1070 |
-
if(
|
1071 |
-
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1075 |
}
|
1076 |
-
|
|
|
|
|
|
|
|
|
|
|
1077 |
|
1078 |
-
|
1079 |
-
$
|
1080 |
-
|
1081 |
-
$
|
1082 |
-
$
|
1083 |
-
|
1084 |
-
|
|
|
1085 |
}
|
1086 |
-
if (strpos($
|
1087 |
-
|
1088 |
}
|
1089 |
-
|
1090 |
-
|
1091 |
|
1092 |
-
|
1093 |
-
|
1094 |
-
|
1095 |
-
}
|
1096 |
-
elseif ($dimension == 'siteSpeed') {
|
1097 |
-
$dimension = 'date';
|
1098 |
-
$metrics = 'ga:avgPageLoadTime,ga:avgRedirectionTime,ga:avgServerResponseTime,ga:avgPageDownloadTime';
|
1099 |
-
if (strpos($metrics, $metric) !== false) {
|
1100 |
-
$metric = $metrics;
|
1101 |
-
}
|
1102 |
-
}
|
1103 |
-
elseif ($dimension == 'eventLabel' || $dimension == 'eventAction' || $dimension == 'eventCategory') {
|
1104 |
-
$metrics = 'ga:eventsPerSessionWithEvent,ga:sessionsWithEvent,ga:avgEventValue,ga:eventValue,ga:uniqueEvents,ga:totalEvents';
|
1105 |
-
if (strpos($metrics, $metric) !== false) {
|
1106 |
-
$metric = $metrics;
|
1107 |
-
}
|
1108 |
-
}
|
1109 |
|
1110 |
-
$
|
1111 |
-
|
1112 |
-
|
1113 |
-
|
1114 |
-
$metric = $metrics;
|
1115 |
-
}
|
1116 |
-
} elseif ($same_dimension == 'adsense' && ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour')) {
|
1117 |
-
$metrics = 'ga:adsenseRevenue,ga:adsenseAdsClicks';
|
1118 |
-
if (strpos($metrics, $metric) !== false) {
|
1119 |
-
$metric = $metrics;
|
1120 |
-
}
|
1121 |
-
} elseif ($same_dimension == 'siteSpeed' && ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour')) {
|
1122 |
-
$metrics = 'ga:avgPageLoadTime,ga:avgRedirectionTime,ga:avgServerResponseTime,ga:avgPageDownloadTime';
|
1123 |
-
if (strpos($metrics, $metric) !== false) {
|
1124 |
-
$metric = $metrics;
|
1125 |
-
}
|
1126 |
}
|
1127 |
|
1128 |
-
if ($
|
1129 |
-
$metrics = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration';
|
1130 |
-
if (strpos($metrics, $metric) !== false) {
|
1131 |
-
$metric = $metrics;
|
1132 |
-
}
|
1133 |
-
}
|
1134 |
|
1135 |
-
|
1136 |
-
if (
|
1137 |
-
|
1138 |
}
|
1139 |
-
|
1140 |
-
|
1141 |
-
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
$gawd_dimension = array(
|
1146 |
-
'dimensions' => 'ga:date, ga:hour',
|
1147 |
-
'sort' => 'ga:date',
|
1148 |
-
);
|
1149 |
-
}
|
1150 |
-
else {
|
1151 |
-
if($dimension != 'sessionDurationBucket'){
|
1152 |
-
$gawd_dimension = array(
|
1153 |
-
'dimensions' => 'ga:' . $dimension,
|
1154 |
-
'sort' => '-ga:' . $selected_metric,
|
1155 |
-
);
|
1156 |
}
|
1157 |
-
|
1158 |
-
$
|
1159 |
-
'dimensions' => 'ga:' . $dimension,
|
1160 |
-
'sort' => 'ga:' . $dimension,
|
1161 |
-
);
|
1162 |
}
|
1163 |
-
|
1164 |
-
|
1165 |
-
|
1166 |
-
$results = $analytics->data_ga->get(
|
1167 |
-
'ga:' . $profileId, $start_date, $end_date, $metric, $gawd_dimension
|
1168 |
-
);
|
1169 |
-
|
1170 |
-
}
|
1171 |
-
catch (Exception $e) {
|
1172 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
1173 |
-
$fh = fopen($myFile, 'a');
|
1174 |
-
fwrite($fh, $e->getMessage(). "----get_data function".PHP_EOL);
|
1175 |
-
fclose($fh);
|
1176 |
-
$error = array('error_message' => 'Error');
|
1177 |
-
if (strpos($e->getMessage(), 'Selected dimensions and metrics cannot be queried together')) {
|
1178 |
-
$error['error_message'] = 'Selected dimensions and metrics cannot be queried together';
|
1179 |
-
} else if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
1180 |
-
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
1181 |
}
|
1182 |
-
|
1183 |
-
|
1184 |
-
|
1185 |
-
$metric = explode(',', $metric);
|
1186 |
-
$rows = $results->getRows();
|
1187 |
-
if ($rows) {
|
1188 |
-
|
1189 |
-
$j = 0;
|
1190 |
-
$data_sum = array();
|
1191 |
-
foreach($results->getTotalsForAllResults() as $key => $value){
|
1192 |
-
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($key,3))))] = $value;
|
1193 |
-
}
|
1194 |
-
|
1195 |
-
if ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour') {
|
1196 |
|
1197 |
-
$
|
1198 |
-
|
1199 |
-
|
1200 |
-
|
1201 |
-
|
1202 |
-
|
1203 |
-
if(strtotime($_end_date) > strtotime(date('Y-m-d'))){
|
1204 |
-
$_end_date = date("M d,Y",strtotime('-1 day ' . date('Y-m-d')));
|
1205 |
-
}
|
1206 |
-
}
|
1207 |
-
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
1208 |
-
$end_date = date("M d,Y");
|
1209 |
}
|
1210 |
-
|
1211 |
-
|
1212 |
-
$dimension_value = date("M d,Y", strtotime($row[0])) . ' ' . $row[1] . ':00';
|
1213 |
-
}
|
1214 |
-
else {
|
1215 |
-
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
1216 |
-
|
1217 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
1218 |
-
// echo $date;
|
1219 |
-
} else {
|
1220 |
-
if ($dimension == 'month') {
|
1221 |
-
//continue;
|
1222 |
-
}
|
1223 |
-
if (strtotime($date) != strtotime($end_date) ) {
|
1224 |
-
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
1225 |
-
} else {
|
1226 |
-
break;
|
1227 |
-
}
|
1228 |
-
}
|
1229 |
-
}
|
1230 |
-
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
1231 |
-
$data[$j]['No'] = floatval($j + 1);
|
1232 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1233 |
-
$val = $i + 1;
|
1234 |
-
if ($dimension == 'hour') {
|
1235 |
-
$val = $i + 2;
|
1236 |
-
}
|
1237 |
-
$metric_val = floatval($row[$val]);
|
1238 |
-
if(substr($metric[$i], 3) == 'bounceRate'){
|
1239 |
-
$metric_val = $metric_val;
|
1240 |
-
}
|
1241 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
1242 |
-
}
|
1243 |
-
|
1244 |
-
$j++;
|
1245 |
-
|
1246 |
-
if(isset($break) && $break){
|
1247 |
-
break;
|
1248 |
-
}
|
1249 |
-
|
1250 |
-
if ($dimension == 'week') {
|
1251 |
-
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
1252 |
-
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
1253 |
-
} elseif ($dimension == 'month') {
|
1254 |
-
$date = date("M d,Y", strtotime('+1 day ' . $_end_date));
|
1255 |
-
$_end_date = date("M t,Y", strtotime($date));
|
1256 |
-
}
|
1257 |
-
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
1258 |
-
$_end_date = date("M d,Y", strtotime($end_date));
|
1259 |
-
$break = true;
|
1260 |
-
}
|
1261 |
}
|
1262 |
-
|
1263 |
-
|
1264 |
-
foreach ($rows as $row) {
|
1265 |
-
if (strtolower($dimension) == 'date') {
|
1266 |
-
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1267 |
-
}
|
1268 |
-
elseif(strtolower($dimension) == 'sessiondurationbucket'){
|
1269 |
-
if($row[0] >= 0 && $row[0] <= 10){
|
1270 |
-
$row[0] = '0-10';
|
1271 |
-
}
|
1272 |
-
elseif($row[0] >= 11 && $row[0] <= 30){
|
1273 |
-
$row[0] = '11-30';
|
1274 |
-
}
|
1275 |
-
elseif($row[0] >= 31 && $row[0] <= 40){
|
1276 |
-
$row[0] = '31-40';
|
1277 |
-
}
|
1278 |
-
elseif($row[0] >= 41 && $row[0] <= 60){
|
1279 |
-
$row[0] = '41-60';
|
1280 |
-
}
|
1281 |
-
elseif($row[0] >= 61 && $row[0] <= 180){
|
1282 |
-
$row[0] = '61-180';
|
1283 |
-
}
|
1284 |
-
elseif($row[0] >= 181 && $row[0] <= 600){
|
1285 |
-
$row[0] = '181-600';
|
1286 |
-
}
|
1287 |
-
elseif($row[0] >= 601 && $row[0] <= 1800){
|
1288 |
-
$row[0] = '601-1800';
|
1289 |
-
}
|
1290 |
-
elseif($row[0] >= 1801){
|
1291 |
-
$row[0] = '1801';
|
1292 |
-
}
|
1293 |
-
}
|
1294 |
-
elseif(strpos($dimension,'dimension') >-1){
|
1295 |
-
$dimension_data = $this->get_custom_dimensions();
|
1296 |
-
foreach($dimension_data as $key => $value){
|
1297 |
-
if($dimension == substr($value['id'],3)){
|
1298 |
-
$dimension = $value['name'];
|
1299 |
-
}
|
1300 |
-
}
|
1301 |
-
}
|
1302 |
-
$data[$j]['No'] = floatval($j + 1);
|
1303 |
-
$dimension_data = ctype_digit($row[0]) ? intval($row[0]) : $row[0];
|
1304 |
-
$dimension_data = strpos($dimension_data,'T') ? substr($dimension_data ,0,strpos($dimension_data,'T')) : $dimension_data;
|
1305 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))] = $dimension_data;
|
1306 |
-
|
1307 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1308 |
-
$metric_val = floatval($row[$i + 1]);
|
1309 |
-
if(substr($metric[$i], 3) == 'avgSessionDuration'){
|
1310 |
-
$metric_val = ceil($row[$i + 1]);
|
1311 |
-
}
|
1312 |
-
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
1313 |
-
}
|
1314 |
-
$j++;
|
1315 |
}
|
|
|
1316 |
}
|
1317 |
-
$
|
1318 |
-
|
1319 |
-
|
1320 |
-
|
1321 |
-
|
1322 |
-
|
1323 |
-
|
1324 |
-
|
1325 |
-
|
1326 |
-
|
1327 |
-
|
1328 |
-
|
1329 |
-
}
|
1330 |
}
|
1331 |
-
elseif($dimension == "sessionDurationBucket"){
|
1332 |
-
$_data = array();
|
1333 |
-
//$j = 1;
|
1334 |
-
foreach($data as $val){
|
1335 |
-
if(isset($_data[$val["Session Duration Bucket"]])){
|
1336 |
-
$_data[$val["Session Duration Bucket"]]["Users"] += floatval($val["Users"]);
|
1337 |
-
$_data[$val["Session Duration Bucket"]]["Sessions"] += floatval($val["Sessions"]);
|
1338 |
-
$_data[$val["Session Duration Bucket"]]["Percent New Sessions"] += floatval($val["Percent New Sessions"]);
|
1339 |
-
$_data[$val["Session Duration Bucket"]]["Bounce Rate"] += floatval($val["Bounce Rate"]);
|
1340 |
-
$_data[$val["Session Duration Bucket"]]["Pageviews"] += floatval($val["Pageviews"]);
|
1341 |
-
$_data[$val["Session Duration Bucket"]]["Avg Session Duration"] += $val["Avg Session Duration"];
|
1342 |
-
}
|
1343 |
-
else{
|
1344 |
-
// $val["No"] = $j;
|
1345 |
-
// $j++;
|
1346 |
-
$_data[$val["Session Duration Bucket"]] = $val;
|
1347 |
-
$_data[$val["Session Duration Bucket"]]["order"] = intval($val["Session Duration Bucket"]);
|
1348 |
-
}
|
1349 |
|
|
|
1350 |
|
1351 |
-
|
1352 |
-
|
1353 |
-
foreach ($data as $key => $row) {
|
1354 |
-
$yyy[$key] = $row['order'];
|
1355 |
-
}
|
1356 |
-
array_multisort($yyy, SORT_ASC, $data);
|
1357 |
-
foreach($data as $j=>$val){
|
1358 |
-
$val["No"] = ($j+1);
|
1359 |
-
$data[$j] = $val;
|
1360 |
-
}
|
1361 |
}
|
1362 |
-
|
1363 |
-
|
1364 |
-
|
1365 |
-
|
1366 |
-
|
1367 |
-
|
1368 |
-
|
1369 |
-
|
1370 |
-
$val["No"] = ($j+1);
|
1371 |
-
$data[$j] = $val;
|
1372 |
-
}
|
1373 |
-
}
|
1374 |
}
|
1375 |
-
$
|
1376 |
-
|
1377 |
-
|
1378 |
}
|
1379 |
-
|
1380 |
-
return json_encode($result);
|
1381 |
}
|
1382 |
else {
|
1383 |
-
|
1384 |
-
|
1385 |
-
|
1386 |
-
|
1387 |
-
|
1388 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1389 |
}
|
|
|
1390 |
}
|
1391 |
-
$
|
1392 |
-
|
1393 |
-
);
|
1394 |
-
$
|
|
|
1395 |
for ($i = 0; $i < count($metric); $i++) {
|
1396 |
-
|
|
|
|
|
|
|
|
|
1397 |
}
|
1398 |
-
|
1399 |
-
|
1400 |
-
}
|
1401 |
-
}
|
1402 |
-
}
|
1403 |
-
|
1404 |
-
public function get_data_compact($metric, $dimension, $start_date, $end_date, $timezone) {
|
1405 |
-
$profileId = $this->get_profile_id();
|
1406 |
-
$metric_sort = $metric;
|
1407 |
-
$analytics = $this->analytics_member;
|
1408 |
-
// Get the results from the Core Reporting API and print the results.
|
1409 |
-
// Calls the Core Reporting API and queries for the number of sessions
|
1410 |
-
// for the last seven days.
|
1411 |
-
if ($dimension == 'date') {
|
1412 |
-
$metric = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration,ga:pageviewsPerSession';
|
1413 |
}
|
1414 |
-
|
1415 |
-
|
1416 |
-
|
1417 |
-
'dimensions' => 'ga:' . $dimension,
|
1418 |
-
'sort' => 'ga:' . $dimension,
|
1419 |
-
)
|
1420 |
-
);
|
1421 |
}
|
1422 |
-
|
1423 |
-
|
1424 |
-
$
|
1425 |
-
|
1426 |
-
|
|
|
|
|
|
|
|
|
1427 |
}
|
1428 |
-
$
|
1429 |
-
|
1430 |
-
|
1431 |
-
|
1432 |
-
$
|
1433 |
-
|
1434 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1435 |
}
|
1436 |
|
1437 |
-
foreach ($rows as $row) {
|
1438 |
-
if ($dimension == 'date') {
|
1439 |
-
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1440 |
-
}
|
1441 |
-
$data[$j] = array(
|
1442 |
-
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $row[0]
|
1443 |
-
);
|
1444 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1445 |
-
$metric_val = floatval($row[$i + 1]);
|
1446 |
|
1447 |
-
|
1448 |
-
|
1449 |
-
|
1450 |
-
|
1451 |
-
|
1452 |
-
|
1453 |
-
|
1454 |
-
|
1455 |
-
|
1456 |
-
|
1457 |
-
$val["No"] = ($j+1);
|
1458 |
-
$data[$j] = $val;
|
1459 |
-
}
|
1460 |
-
}
|
1461 |
}
|
1462 |
else {
|
1463 |
-
|
1464 |
-
$
|
1465 |
-
|
1466 |
-
|
1467 |
-
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))] = 0;
|
1468 |
-
$empty[0]['No'] = 1;
|
1469 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1470 |
-
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1471 |
-
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1472 |
}
|
1473 |
-
|
1474 |
-
|
|
|
|
|
|
|
|
|
1475 |
}
|
|
|
1476 |
if ($data_sum != '') {
|
1477 |
-
|
1478 |
}
|
1479 |
-
|
1480 |
-
set_transient('gawd-compact-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1481 |
return json_encode($result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1482 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1483 |
|
1484 |
-
|
1485 |
-
|
1486 |
-
|
1487 |
-
|
1488 |
-
|
1489 |
-
|
1490 |
-
$results = $analytics->data_ga->get(
|
1491 |
-
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1492 |
-
'dimensions' => 'ga:' . $dimension,
|
1493 |
-
'sort' => 'ga:' . $dimension,
|
1494 |
-
)
|
1495 |
);
|
1496 |
-
$
|
|
|
1497 |
|
1498 |
-
|
1499 |
-
foreach ($rows as $row) {
|
1500 |
-
$data += floatval($row[1]);
|
1501 |
}
|
1502 |
-
|
1503 |
-
|
1504 |
-
|
1505 |
-
|
1506 |
-
|
1507 |
-
|
1508 |
-
|
1509 |
-
|
1510 |
-
|
1511 |
-
|
1512 |
-
$this->gawd_user_data['gawd_id'] = $first_profile['id'];
|
1513 |
-
}
|
1514 |
}
|
1515 |
-
|
1516 |
}
|
1517 |
-
|
1518 |
-
|
1519 |
-
|
1520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1521 |
}
|
1522 |
-
|
1523 |
-
|
1524 |
-
$this->gawd_user_data = get_option('gawd_user_data');
|
1525 |
-
return isset($this->gawd_user_data['accountId']) ? $this->gawd_user_data['accountId'] : '';
|
1526 |
}
|
1527 |
-
|
1528 |
-
|
1529 |
-
|
1530 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1531 |
}
|
1532 |
-
|
1533 |
-
|
1534 |
-
|
1535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1536 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1537 |
|
1538 |
-
|
1539 |
-
|
1540 |
-
|
1541 |
-
|
1542 |
-
|
1543 |
-
|
|
|
|
|
|
|
|
|
1544 |
|
1545 |
-
|
1546 |
-
|
1547 |
-
|
1548 |
-
|
1549 |
-
|
1550 |
-
|
1551 |
-
|
1552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1553 |
|
1554 |
-
|
1555 |
-
|
1556 |
-
|
1557 |
-
|
1558 |
|
1559 |
-
|
1560 |
-
|
1561 |
-
|
1562 |
-
|
1563 |
-
|
|
|
|
|
|
|
1564 |
}
|
1565 |
-
|
1566 |
-
|
1567 |
-
|
1568 |
-
|
1569 |
-
|
1570 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1571 |
}
|
1572 |
-
|
1573 |
-
|
1574 |
-
$
|
1575 |
-
|
1576 |
-
$time_on_page = $hours.':'.$mins.':'.$secs;
|
1577 |
-
if($dimension == 'pagePath' || $dimension == 'PagePath'){
|
1578 |
-
$data[] = array(
|
1579 |
-
'No' => floatval($key + 1),
|
1580 |
-
'Page Path' => $row[0],
|
1581 |
-
'Pageviews' => intval($row[1]),
|
1582 |
-
'Unique Pageviews' => intval($row[2]),
|
1583 |
-
'Avg Time On Page' => $time_on_page,
|
1584 |
-
'Entrances' => intval($row[4]),
|
1585 |
-
'Bounce Rate' => floatval($row[5]),
|
1586 |
-
'Exit Rate' => ($row[6]),
|
1587 |
-
'Page Value' => intval($row[7]),
|
1588 |
-
'Avg Page Load Time' => intval($row[8])
|
1589 |
-
);
|
1590 |
-
}
|
1591 |
-
else{
|
1592 |
-
$data[] = array(
|
1593 |
-
'No' => floatval($key + 1),
|
1594 |
-
'Landing Page' => $row[0],
|
1595 |
-
'Sessions' => intval($row[1]),
|
1596 |
-
'Percent New Sessions' => ($row[2]),
|
1597 |
-
'New Users' => intval($row[3]),
|
1598 |
-
'Bounce Rate' => ($row[4]),
|
1599 |
-
'Pageviews Per Session' => floatval($row[5]),
|
1600 |
-
'Avg Session Duration' => intval($row[6]),
|
1601 |
-
'Transactions' => intval($row[7]),
|
1602 |
-
'Transaction Revenue' => intval($row[8]),
|
1603 |
-
'Transactions Per Session' => intval($row[9])
|
1604 |
-
);
|
1605 |
-
}
|
1606 |
}
|
1607 |
-
|
|
|
1608 |
}
|
1609 |
-
|
1610 |
-
|
1611 |
-
|
1612 |
-
);
|
1613 |
-
$empty[0]['No'] = 1;
|
1614 |
-
for ($i = 0; $i < count($metric); $i++) {
|
1615 |
-
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1616 |
-
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i],3))))] = 0;
|
1617 |
-
}
|
1618 |
-
|
1619 |
-
return json_encode(array('data_sum' => $data_sum, 'chart_data' => $empty));
|
1620 |
}
|
|
|
1621 |
if ($data_sum != '') {
|
1622 |
-
|
1623 |
}
|
1624 |
-
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1625 |
set_transient('gawd-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1626 |
return json_encode($result);
|
|
|
|
|
|
|
|
|
1627 |
}
|
1628 |
-
|
1629 |
-
|
1630 |
-
$goals = $this->get_management_goals();
|
1631 |
-
if ('no_goals_exist' != $goals) {
|
1632 |
-
$analytics = $this->analytics_member;
|
1633 |
-
$profileId = $this->get_profile_id();
|
1634 |
-
$metric = array();
|
1635 |
-
$all_metric = '';
|
1636 |
-
$counter = 1;
|
1637 |
-
foreach ($goals as $goal) {
|
1638 |
-
$all_metric .= 'ga:goal' . $goal['id'] . 'Completions,';
|
1639 |
-
if($counter <= 10){
|
1640 |
-
$metrics[0][] = 'ga:goal' . $goal['id'] . 'Completions';
|
1641 |
-
}
|
1642 |
-
else{
|
1643 |
-
$metrics[1][] = 'ga:goal' . $goal['id'] . 'Completions';
|
1644 |
-
}
|
1645 |
-
$counter++;
|
1646 |
-
}
|
1647 |
-
$rows = array();
|
1648 |
-
foreach($metrics as $metric){
|
1649 |
-
$metric = implode(',',$metric);
|
1650 |
-
$results = $analytics->data_ga->get(
|
1651 |
-
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1652 |
-
'dimensions' => 'ga:' . $dimension,
|
1653 |
-
'sort' => 'ga:' . $dimension,
|
1654 |
-
)
|
1655 |
-
);
|
1656 |
-
|
1657 |
-
$temp_rows = $results->getRows();
|
1658 |
-
if(empty($temp_rows)){
|
1659 |
-
continue;
|
1660 |
-
}
|
1661 |
-
|
1662 |
-
foreach($temp_rows as $key=>$value){
|
1663 |
-
if(!isset($rows[$key])){
|
1664 |
-
$rows[$key] = $value;
|
1665 |
-
}
|
1666 |
-
else{
|
1667 |
-
unset($value[0]);
|
1668 |
-
$rows[$key] = array_merge($rows[$key],$value);
|
1669 |
-
}
|
1670 |
-
}
|
1671 |
-
|
1672 |
-
}
|
1673 |
-
$all_metric = explode(',', $all_metric);
|
1674 |
-
if ($rows) {
|
1675 |
-
$j = 0;
|
1676 |
-
$data_sum = array();
|
1677 |
-
foreach ($rows as $row) {
|
1678 |
-
if ($dimension == 'date') {
|
1679 |
-
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1680 |
-
}
|
1681 |
-
$data[$j] = array(
|
1682 |
-
preg_replace('!\s+!',' ',trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))))=> $row[0]
|
1683 |
-
);
|
1684 |
-
$data[$j]['No'] = floatval($j + 1);
|
1685 |
-
for ($i = 0; $i < count($goals); $i++) {
|
1686 |
-
$data[$j][preg_replace('!\s+!',' ',trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] = floatval($row[$i + 1]);
|
1687 |
-
if (isset($data_sum[preg_replace('!\s+!',' ',trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))])) {
|
1688 |
-
$data_sum[preg_replace('!\s+!',' ',trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] += floatval($row[$i + 1]);
|
1689 |
-
} else {
|
1690 |
-
if (substr($all_metric[$i], 3) != 'percentNewSessions' && substr($all_metric[$i], 3) != 'bounceRate') {
|
1691 |
-
$data_sum[preg_replace('!\s+!',' ',trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] = floatval($row[$i + 1]);
|
1692 |
-
}
|
1693 |
-
}
|
1694 |
-
}
|
1695 |
-
$j++;
|
1696 |
-
}
|
1697 |
-
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1698 |
-
if (isset($same_dimension) && $same_dimension != null) {
|
1699 |
-
$dimension = $same_dimension;
|
1700 |
-
}
|
1701 |
-
$result = $data;
|
1702 |
-
if ($data_sum != '') {
|
1703 |
-
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1704 |
-
}
|
1705 |
-
set_transient('gawd-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1706 |
-
return json_encode($result);
|
1707 |
-
} else {
|
1708 |
-
return $goals;
|
1709 |
-
}
|
1710 |
-
} else {
|
1711 |
-
return json_encode(array('error_message' => 'No goals exist'));
|
1712 |
-
}
|
1713 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1714 |
|
1715 |
-
|
1716 |
-
|
1717 |
-
$profileId = $this->get_profile_id();
|
1718 |
-
$metrics = 'rt:activeUsers';
|
1719 |
-
$dimensions = 'rt:pagePath,rt:source,rt:keyword,rt:trafficType,rt:country,rt:pageTitle,rt:deviceCategory';
|
1720 |
-
$managequota = 'u' . get_current_user_id() . 's' . get_current_blog_id();
|
1721 |
-
|
1722 |
-
try {
|
1723 |
-
$data = $analytics->data_realtime->get('ga:' . $profileId, $metrics, array('dimensions' => $dimensions, 'quotaUser' => $managequota . 'p' . $profileId));
|
1724 |
-
} catch (Exception $e) {
|
1725 |
-
|
1726 |
-
$myFile = GAWD_UPLOAD_DIR."/logfile.txt"; ;
|
1727 |
-
$fh = fopen($myFile, 'a');
|
1728 |
-
fwrite($fh, $e->getMessage(). "----gawd_realtime_data function".PHP_EOL);
|
1729 |
-
fclose($fh);
|
1730 |
|
1731 |
-
|
1732 |
-
|
1733 |
-
|
1734 |
-
|
1735 |
-
|
1736 |
-
}
|
1737 |
-
$expiration = 5 ;
|
1738 |
-
if ($data->getRows() != '') {
|
1739 |
-
$i = 0;
|
1740 |
-
$gawd_data = $data;
|
1741 |
-
foreach ($data->getRows() as $row) {
|
1742 |
-
$gawd_data[$i] = $row;
|
1743 |
-
$i++;
|
1744 |
-
}
|
1745 |
-
set_transient('gawd-real-' . $profileId, json_encode($gawd_data), $expiration);
|
1746 |
-
echo json_encode($gawd_data);
|
1747 |
-
wp_die();
|
1748 |
-
}
|
1749 |
-
else {
|
1750 |
-
return 0;
|
1751 |
-
}
|
1752 |
}
|
1753 |
|
1754 |
-
|
|
|
1755 |
|
1756 |
-
|
1757 |
-
|
1758 |
-
}
|
1759 |
-
else if($force_new_instance){
|
1760 |
-
self::$instance = new self();
|
1761 |
-
}
|
1762 |
|
1763 |
-
|
1764 |
-
|
1765 |
|
1766 |
-
|
1767 |
-
|
1768 |
-
|
1769 |
-
|
1770 |
-
|
1771 |
-
|
1772 |
-
|
1773 |
-
|
1774 |
-
</div>
|
1775 |
-
<?php
|
1776 |
-
}
|
1777 |
|
1778 |
}
|
2 |
|
3 |
require_once(GAWD_DIR . '/google/vendor/autoload.php');
|
4 |
|
5 |
+
class GAWD_google_client
|
6 |
+
{
|
7 |
+
|
8 |
+
private static $instance;
|
9 |
+
private $google_client;
|
10 |
+
public $analytics_member;
|
11 |
+
private $gawd_user_data;
|
12 |
+
private static $error_message = '';
|
13 |
+
|
14 |
+
protected function __construct()
|
15 |
+
{
|
16 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
17 |
+
try {
|
18 |
+
|
19 |
+
$this->google_client = new Google_Client();
|
20 |
+
$this->set_google_client();
|
21 |
+
$this->analytics_member = new Google_Service_Analytics($this->google_client);
|
22 |
+
|
23 |
+
} catch (Google_Service_Exception $e) {
|
24 |
+
$errors = $e->getErrors();
|
25 |
+
self::$error_message = $errors[0]["message"];
|
26 |
+
} catch (Exception $e) {
|
27 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
28 |
+
$fh = fopen($myFile, 'a');
|
29 |
+
fwrite($fh, $e->getMessage() . "----constructor function" . PHP_EOL);
|
30 |
+
fclose($fh);
|
31 |
+
self::$error_message = $e->getMessage();
|
32 |
+
}
|
33 |
+
}
|
34 |
+
|
35 |
+
/**
|
36 |
+
* Sets the google class member.
|
37 |
+
*/
|
38 |
+
private function set_google_client()
|
39 |
+
{
|
40 |
+
|
41 |
+
$access_token = isset($this->gawd_user_data['access_token']) ? $this->gawd_user_data['access_token'] : '';
|
42 |
+
if ($access_token != '') {
|
43 |
+
$this->google_client->setAccessToken($access_token);
|
44 |
+
|
45 |
+
if ($this->google_client->isAccessTokenExpired()) {
|
46 |
+
$refresh_token = $this->gawd_user_data['refresh_token'];
|
47 |
+
|
48 |
+
$this->google_client->setClientId(GAWD::get_instance()->get_project_client_id());
|
49 |
+
$this->google_client->setClientSecret(GAWD::get_instance()->get_project_client_secret());
|
50 |
+
$this->google_client->setRedirectUri(GAWD::get_instance()->redirect_uri);
|
51 |
+
// $this->google_client->setAuthConfigFile(GAWD_DIR . '/client_secrets.json');
|
52 |
+
$this->google_client->refreshToken($refresh_token);
|
53 |
+
}
|
54 |
}
|
55 |
+
}
|
56 |
+
|
57 |
+
public static function create_authentication_url()
|
58 |
+
{
|
59 |
+
$client = new Google_Client();
|
60 |
+
// $client->setAuthConfigFile(GAWD_DIR . '/client_secrets.json');
|
61 |
+
$client->setClientId(GAWD::get_instance()->get_project_client_id());
|
62 |
+
$client->setClientSecret(GAWD::get_instance()->get_project_client_secret());
|
63 |
+
$client->setRedirectUri(GAWD::get_instance()->redirect_uri);
|
64 |
+
$client->addScope(array(Google_Service_Analytics::ANALYTICS_EDIT, Google_Service_Analytics::ANALYTICS_READONLY));
|
65 |
+
$client->setApprovalPrompt('force');
|
66 |
+
$client->setAccessType('offline');
|
67 |
+
return "'" . $client->createAuthUrl() . "'";
|
68 |
+
}
|
69 |
+
|
70 |
+
public static function authenticate($code)
|
71 |
+
{
|
72 |
+
|
73 |
+
$client = new Google_Client();
|
74 |
+
// $client->setAuthConfigFile(GAWD_DIR . '/client_secrets.json');
|
75 |
+
$client->setClientId(GAWD::get_instance()->get_project_client_id());
|
76 |
+
$client->setClientSecret(GAWD::get_instance()->get_project_client_secret());
|
77 |
+
$client->setRedirectUri(GAWD::get_instance()->redirect_uri);
|
78 |
+
if (isset($code) && $code != '') {
|
79 |
+
try {
|
80 |
+
$client->authenticate($code);
|
81 |
+
|
82 |
+
if ($client->isAccessTokenExpired()) {
|
83 |
+
return false;
|
84 |
+
}
|
85 |
+
else {
|
86 |
+
$access_token = $client->getAccessToken();
|
87 |
+
$refresh_token = $client->getRefreshToken();
|
88 |
+
update_option('gawd_user_data', array(
|
89 |
+
'access_token' => $access_token,
|
90 |
+
'refresh_token' => $refresh_token
|
91 |
+
)
|
92 |
+
);//TODO WHY 1?
|
93 |
|
94 |
+
$gawd_client = self::get_instance();
|
95 |
+
delete_transient('gawd_user_profiles');
|
96 |
+
$profiles = $gawd_client->get_profiles();
|
|
|
97 |
|
98 |
+
/* if ($profiles instanceof Google_Service_Exception) {
|
|
|
|
|
99 |
|
100 |
+
delete_option('gawd_user_data');
|
101 |
+
$errors = $profiles->getErrors();
|
102 |
+
return $errors[0]["message"];
|
103 |
+
}*/
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
update_option('gawd_user_data', array(
|
107 |
+
'access_token' => $access_token,
|
108 |
+
'refresh_token' => $refresh_token,
|
109 |
+
'gawd_profiles' => $profiles,
|
110 |
+
)
|
111 |
+
);//TODO WHY 2?
|
112 |
+
return true;
|
113 |
+
}
|
114 |
+
} catch (Google_Service_Exception $e) {
|
115 |
+
delete_option('gawd_user_data');
|
116 |
+
$errors = $e->getErrors();
|
117 |
+
return $errors[0]["message"];
|
118 |
+
} catch (Exception $e) {
|
119 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
120 |
+
$fh = fopen($myFile, 'a');
|
121 |
+
fwrite($fh, $e->getMessage() . "----authenticate function" . PHP_EOL);
|
122 |
+
fclose($fh);
|
123 |
+
return $e->getMessage();
|
124 |
+
}
|
125 |
}
|
126 |
|
127 |
+
}
|
128 |
|
129 |
+
/*
|
130 |
+
* force call to Google API if $data == true
|
131 |
+
*
|
132 |
+
* */
|
133 |
+
public function get_management_accounts($data = false)
|
134 |
+
{
|
135 |
+
$accounts_light = array();
|
|
|
136 |
|
137 |
+
$accounts = get_option('gawd_management_accounts');
|
138 |
+
if ($accounts == false) {
|
139 |
+
$accounts = array();
|
140 |
+
}
|
141 |
+
try {
|
142 |
+
if ($data == true) {
|
143 |
+
$accounts = $this->analytics_member->management_accounts->listManagementAccounts()->getItems();
|
144 |
+
update_option('gawd_management_accounts', $accounts);
|
145 |
+
}
|
146 |
+
foreach ($accounts as $account) {
|
147 |
+
$edit_flag = FALSE;
|
148 |
+
$permissions = $account['permissions']['effective'];
|
149 |
+
foreach ($permissions as $permission) {
|
150 |
+
if ($permission == 'EDIT') {
|
151 |
+
$edit_flag = TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
}
|
153 |
}
|
154 |
+
$accounts_light[] = array(
|
155 |
+
'name' => $account['name'],
|
156 |
+
'id' => $account['id'],
|
157 |
+
'edit_permissions' => $edit_flag
|
158 |
+
);
|
159 |
+
/*if ($edit_flag == TRUE) {
|
160 |
+
$accounts_light[] = array(
|
161 |
+
'name' => $account['name'],
|
162 |
+
'id' => $account['id']
|
163 |
+
);
|
164 |
+
}*/
|
165 |
+
}
|
166 |
+
} catch (Google_Service_Exception $e) {
|
167 |
+
//return $e->getErrors()[0]["message"];
|
168 |
+
} catch (Exception $e) {
|
169 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
170 |
+
$fh = fopen($myFile, 'a');
|
171 |
+
fwrite($fh, $e->getMessage() . "----get_management_accounts function" . PHP_EOL);
|
172 |
+
fclose($fh);
|
173 |
+
//return $e->getMessage();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
}
|
175 |
+
return $accounts_light;
|
176 |
+
}
|
177 |
+
|
178 |
+
public function property_exists()
|
179 |
+
{
|
180 |
+
try {
|
181 |
+
$web_properties = $this->analytics_member->management_webproperties->listManagementWebproperties('~all')->getItems();
|
182 |
+
} catch (Google_Service_Exception $e) {
|
183 |
+
return 'no_matches';
|
184 |
+
|
185 |
+
} catch (Exception $e) {
|
186 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
187 |
+
$fh = fopen($myFile, 'a');
|
188 |
+
fwrite($fh, $e->getMessage() . "----property_exists function" . PHP_EOL);
|
189 |
+
fclose($fh);
|
190 |
+
return 'no_matches';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
}
|
192 |
+
$exact_properties = array();
|
193 |
+
$site_url = get_site_url() . '/';
|
194 |
|
195 |
+
foreach ($web_properties as $web_property) {
|
196 |
+
$current_url = $web_property['websiteUrl'];
|
197 |
+
if (($current_url == $site_url) || (($current_url . '/') == $site_url)) {
|
198 |
+
$exact_properties[] = $web_property;
|
199 |
+
}
|
200 |
+
}
|
201 |
+
if (!empty($exact_properties)) {
|
202 |
+
return $exact_properties;
|
203 |
+
}
|
204 |
+
else {
|
205 |
+
return 'no_matches';
|
206 |
+
}
|
207 |
+
}
|
208 |
+
|
209 |
+
public function get_default_profiles()
|
210 |
+
{
|
211 |
+
$gawd_user_data = get_option('gawd_user_data');
|
212 |
+
$accountId = $this->get_default_accountId();
|
213 |
+
$webPropertyId = $this->get_default_webPropertyId();
|
214 |
+
$webProperty = $this->analytics_member->management_webproperties->get($accountId, $webPropertyId);
|
215 |
+
$webPropertyName = $webProperty['name'];
|
216 |
+
$profiles = $this->analytics_member->management_profiles->listManagementProfiles($accountId, $webPropertyId)->getItems();
|
217 |
+
$profiles_light = array();
|
218 |
+
foreach ($profiles as $profile) {
|
219 |
+
$profiles_light[] = array(
|
220 |
+
'id' => $profile['id'],
|
221 |
+
'name' => $profile['name'],
|
222 |
+
'webPropertyName' => $webPropertyName
|
223 |
+
);
|
224 |
+
}
|
225 |
+
return $profiles_light;
|
226 |
+
}
|
227 |
+
|
228 |
+
public function add_webproperty($accountId, $name)
|
229 |
+
{
|
230 |
+
|
231 |
+
$analytics = $this->analytics_member;
|
232 |
+
$websiteUrl = get_site_url();
|
233 |
+
try {
|
234 |
+
$property = new Google_Service_Analytics_Webproperty();
|
235 |
+
$property->setName($name);
|
236 |
+
$property->setWebsiteUrl($websiteUrl);
|
237 |
+
$analytics->management_webproperties->insert($accountId, $property);
|
238 |
+
} catch (apiServiceException $e) {
|
239 |
+
print 'There was an Analytics API service error '
|
240 |
+
. $e->getCode() . ':' . $e->getMessage();
|
241 |
+
} catch (apiException $e) {
|
242 |
+
print 'There was a general API error '
|
243 |
+
. $e->getCode() . ':' . $e->getMessage();
|
244 |
+
} catch (Google_Service_Exception $e) {
|
245 |
+
return 'There was a general API error '
|
246 |
+
. $e->getCode() . ':' . $e->getMessage();
|
247 |
+
} catch (Exception $e) {
|
248 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
249 |
+
$fh = fopen($myFile, 'a');
|
250 |
+
fwrite($fh, $e->getMessage() . "----add_webproperty function" . PHP_EOL);
|
251 |
+
fclose($fh);
|
252 |
+
echo $e->getCode() . ':' . $e->getMessage();
|
253 |
}
|
254 |
|
255 |
+
$web_properties = $this->analytics_member->management_webproperties->listManagementWebproperties($accountId)->getItems();
|
256 |
+
foreach ($web_properties as $web_property) {
|
257 |
+
if ($web_property['name'] == $name) {
|
258 |
+
$profile = new Google_Service_Analytics_Profile();
|
259 |
+
$profile->setName('All Web Site Data');
|
260 |
|
|
|
|
|
261 |
try {
|
262 |
+
$analytics->management_profiles->insert($accountId, $web_property['id'], $profile);
|
|
|
|
|
|
|
263 |
} catch (apiServiceException $e) {
|
264 |
+
print 'There was an Analytics API service error '
|
265 |
+
. $e->getCode() . ':' . $e->getMessage();
|
266 |
} catch (apiException $e) {
|
267 |
+
print 'There was a general API error '
|
268 |
+
. $e->getCode() . ':' . $e->getMessage();
|
269 |
}
|
270 |
+
$current_profiles = $this->analytics_member->management_profiles->listManagementProfiles($accountId, $web_property['id'])->getItems();
|
271 |
+
try {
|
272 |
+
$property = new Google_Service_Analytics_Webproperty();
|
273 |
+
$property->setName($name);
|
274 |
+
$property->setWebsiteUrl($websiteUrl);
|
275 |
+
$property->setDefaultProfileId($current_profiles[0]['id']);
|
276 |
+
$analytics->management_webproperties->update($accountId, $web_property['id'], $property);
|
277 |
+
} catch (apiServiceException $e) {
|
278 |
+
print 'There was an Analytics API service error '
|
279 |
+
. $e->getCode() . ':' . $e->getMessage();
|
280 |
+
} catch (apiException $e) {
|
281 |
+
print 'There was a general API error '
|
282 |
+
. $e->getCode() . ':' . $e->getMessage();
|
283 |
+
} catch (Google_Service_Exception $e) {
|
284 |
+
return 'There was a general API error '
|
285 |
+
. $e->getCode() . ':' . $e->getMessage();
|
286 |
+
} catch (Exception $e) {
|
287 |
+
echo $e->getCode() . ':' . $e->getMessage();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
288 |
}
|
289 |
+
}
|
290 |
}
|
291 |
|
292 |
+
}
|
293 |
+
|
294 |
+
/**
|
295 |
+
* Get all the management profiles of the authenticated user.
|
296 |
+
*
|
297 |
+
* @return array
|
298 |
+
*/
|
299 |
+
public function get_profiles()
|
300 |
+
{
|
301 |
+
$profiles_light = get_transient('gawd_user_profiles') ? get_transient('gawd_user_profiles') : '';
|
302 |
+
if ($profiles_light && $profiles_light != '') {
|
303 |
+
return $profiles_light;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
}
|
305 |
+
$profiles_light = array();
|
306 |
+
$gawd_user_data = get_option('gawd_user_data');
|
307 |
+
try {
|
308 |
+
|
309 |
+
if ($this->analytics_member && $this->analytics_member->management_webproperties) {
|
310 |
+
$web_properties = $this->analytics_member->management_webproperties->listManagementWebproperties('~all')->getItems();
|
311 |
+
$profiles = $this->analytics_member->management_profiles->listManagementProfiles('~all', '~all')->getItems();
|
312 |
+
$profiles_count = count($profiles);
|
313 |
+
$web_properties_count = count($web_properties);
|
314 |
+
for ($i = 0; $i < $web_properties_count; $i++) {
|
315 |
+
for ($j = 0; $j < $profiles_count; $j++) {
|
316 |
+
if ($web_properties[$i]['id'] == $profiles[$j]['webPropertyId']) {
|
317 |
+
$profiles_light[$web_properties[$i]['name']][] = array(
|
318 |
+
'id' => $profiles[$j]['id'],
|
319 |
+
'name' => $profiles[$j]['name'],
|
320 |
+
'webPropertyId' => $profiles[$j]['webPropertyId'],
|
321 |
+
'websiteUrl' => $profiles[$j]['websiteUrl'],
|
322 |
+
'accountId' => $profiles[$j]['accountId']
|
323 |
+
);
|
324 |
}
|
325 |
+
}
|
326 |
}
|
327 |
+
}
|
328 |
+
else {
|
329 |
+
return 'Something went wrong';
|
330 |
+
}
|
331 |
+
if (!isset($gawd_user_data['gawd_id']) || $gawd_user_data['gawd_id'] == '' || $gawd_user_data['gawd_id'] == NULL) {
|
332 |
+
if (!empty($profiles_light)) {
|
333 |
+
$first_profiles = reset($profiles_light);
|
334 |
+
$first_profile = $first_profiles[0];
|
335 |
+
$gawd_user_data['gawd_id'] = $first_profile['id'];
|
336 |
+
$gawd_user_data['webPropertyId'] = $first_profile['webPropertyId'];
|
337 |
+
$gawd_user_data['accountId'] = $first_profile['accountId'];
|
338 |
+
$gawd_user_data['web_property_name'] = $web_properties[0]['name'];
|
339 |
}
|
340 |
+
}
|
341 |
+
|
342 |
+
$gawd_user_data['gawd_profiles'] = $profiles_light;
|
343 |
+
update_option('gawd_user_data', $gawd_user_data);
|
344 |
+
set_transient('gawd_user_profiles', $profiles_light, 60);
|
345 |
+
return $profiles_light;
|
346 |
+
} catch (Google_Service_Exception $e) {
|
347 |
+
return $e;
|
348 |
+
} catch (Exception $e) {
|
349 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
350 |
+
$fh = fopen($myFile, 'a');
|
351 |
+
fwrite($fh, $e->getMessage() . "----get_profiles function" . PHP_EOL);
|
352 |
+
fclose($fh);
|
353 |
+
return $e;
|
354 |
+
}
|
355 |
+
}
|
356 |
+
|
357 |
+
public function get_custom_dimensions($default = '')
|
358 |
+
{
|
359 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
360 |
+
if ($default == 'default') {
|
361 |
+
$webPropertyId = $this->get_default_webPropertyId();
|
362 |
+
$accountId = $this->get_default_accountId();
|
363 |
+
}
|
364 |
+
else {
|
365 |
+
$webPropertyId = $this->get_profile_webPropertyId();
|
366 |
+
$accountId = $this->get_profile_accountId();
|
367 |
+
$transient = get_transient('gawd-custom-dimensions-' . $webPropertyId);
|
368 |
+
if ($transient) {
|
369 |
+
if ($transient != 'no_custom_dimensions_exist') {
|
370 |
+
return json_decode($transient, true);
|
371 |
}
|
372 |
else {
|
373 |
+
return $transient;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
}
|
375 |
+
}
|
376 |
}
|
377 |
+
try {
|
378 |
+
$all_dimensions = $this->analytics_member->management_customDimensions->listManagementCustomDimensions($accountId, $webPropertyId)->getItems();
|
379 |
+
|
380 |
+
} catch (Exception $e) {
|
381 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
382 |
+
$fh = fopen($myFile, 'a');
|
383 |
+
fwrite($fh, $e->getMessage() . "----get_custom_dimensions function" . PHP_EOL);
|
384 |
+
fclose($fh);
|
385 |
+
}
|
386 |
+
if (0 == sizeof($all_dimensions)) {
|
387 |
+
if ($default == 'default') {
|
388 |
+
update_option('gawd_custom_dimensions', "no_custom_dimensions_exist");
|
389 |
+
}
|
390 |
+
set_transient('gawd-custom-dimensions-' . $webPropertyId, "no_custom_dimensions_exist", 12 * HOUR_IN_SECONDS);
|
391 |
+
return "no_custom_dimensions_exist";
|
392 |
+
}
|
393 |
+
else {
|
394 |
+
foreach ($all_dimensions as $dimension) {
|
395 |
+
$dimensions_light[] = array(
|
396 |
+
'name' => $dimension['name'],
|
397 |
+
'id' => $dimension['id']
|
398 |
+
);
|
399 |
+
}
|
400 |
+
$supported_dimensions = array("Logged in", "Post type", "Author", "Category", "Tags", "Published Month", "Published Year");
|
401 |
+
$dimensions = array();
|
402 |
+
foreach ($dimensions_light as $dimension) {
|
403 |
+
foreach ($supported_dimensions as $supported_dimension) {
|
404 |
+
if (trim(strtolower($dimension['name'])) == strtolower($supported_dimension)) {
|
405 |
+
$dimension['name'] = $supported_dimension;
|
406 |
+
$dimension['id'] = substr($dimension['id'], -1);
|
407 |
+
$dimensions[] = $dimension;
|
408 |
+
}
|
409 |
}
|
410 |
+
}
|
411 |
+
if ($default == 'default') {
|
412 |
update_option('gawd_custom_dimensions', $dimensions);
|
413 |
+
}
|
414 |
+
set_transient('gawd-custom-dimensions-' . $webPropertyId, json_encode($dimensions_light), 12 * HOUR_IN_SECONDS);
|
415 |
+
return $dimensions_light;
|
|
|
|
|
416 |
}
|
417 |
+
}
|
418 |
|
419 |
+
public function get_custom_dimensions_tracking()
|
420 |
+
{
|
421 |
+
$all_dimensions = get_option('gawd_custom_dimensions');
|
422 |
+
if ($all_dimensions) {
|
423 |
+
return $all_dimensions;
|
424 |
}
|
425 |
+
$all_dimensions = $this->get_custom_dimensions('default');
|
426 |
+
if ($all_dimensions == 'no_custom_dimensions_exist') {
|
427 |
+
return 'no_custom_dimensions_exist';
|
428 |
+
}
|
429 |
+
$supported_dimensions = array("Logged in", "Post type", "Author", "Category", "Tags", "Published Month", "Published Year");
|
430 |
+
$dimensions = array();
|
431 |
+
foreach ($all_dimensions as $dimension) {
|
432 |
+
foreach ($supported_dimensions as $supported_dimension) {
|
433 |
+
if (trim(strtolower($dimension['name'])) == strtolower($supported_dimension)) {
|
434 |
+
$dimension['id'] = substr($dimension['id'], -1);
|
435 |
+
$dimension['name'] = $supported_dimension;
|
436 |
+
$dimensions[] = $dimension;
|
437 |
}
|
438 |
+
}
|
439 |
+
}
|
440 |
+
update_option('gawd_custom_dimensions', $dimensions);
|
441 |
+
if ($dimensions) {
|
442 |
+
return $dimensions;
|
443 |
+
}
|
444 |
+
else {
|
445 |
+
return "no_custom_dimensions_exist";
|
446 |
}
|
447 |
+
}
|
448 |
+
|
449 |
+
public static function gawd_cd_logged_in()
|
450 |
+
{
|
451 |
+
$value = var_export(is_user_logged_in(), true);
|
452 |
+
$value = $value == 'true' ? 'yes' : 'no';
|
453 |
+
return $value;
|
454 |
+
}
|
455 |
+
|
456 |
+
public static function gawd_cd_post_type()
|
457 |
+
{
|
458 |
+
if (is_singular()) {
|
459 |
+
$post_type = get_post_type(get_the_ID());
|
460 |
+
|
461 |
+
if ($post_type) {
|
462 |
+
return $post_type;
|
463 |
+
}
|
464 |
+
}
|
465 |
+
}
|
466 |
|
467 |
+
public static function gawd_cd_author()
|
468 |
+
{
|
469 |
+
if (is_singular()) {
|
470 |
+
if (have_posts()) {
|
471 |
+
while (have_posts()) {
|
472 |
+
the_post();
|
|
|
|
|
|
|
|
|
473 |
}
|
474 |
+
}
|
475 |
+
$name = get_the_author_meta('user_nicename');
|
476 |
+
$value = trim($name);
|
477 |
+
return $value;
|
478 |
}
|
479 |
+
}
|
480 |
|
481 |
+
public static function gawd_cd_category()
|
482 |
+
{
|
483 |
+
if (is_single()) {
|
484 |
+
$categories = get_the_category(get_the_ID());
|
|
|
|
|
|
|
|
|
485 |
|
486 |
+
if ($categories) {
|
487 |
+
foreach ($categories as $category) {
|
488 |
+
$category_names[] = $category->slug;
|
489 |
}
|
490 |
+
|
491 |
+
return implode(',', $category_names);
|
492 |
+
}
|
493 |
}
|
494 |
+
}
|
495 |
|
496 |
+
public static function gawd_cd_tags()
|
497 |
+
{
|
498 |
+
if (is_single()) {
|
499 |
+
$tag_names = 'untagged';
|
500 |
|
501 |
+
$tags = get_the_tags(get_the_ID());
|
502 |
|
503 |
+
if ($tags) {
|
504 |
+
$tag_names = implode(',', wp_list_pluck($tags, 'name'));
|
505 |
+
}
|
506 |
|
507 |
+
return $tag_names;
|
|
|
508 |
}
|
509 |
+
}
|
510 |
|
511 |
|
512 |
+
public static function gawd_cd_published_month()
|
513 |
+
{
|
514 |
+
if (is_singular()) {
|
515 |
+
return get_the_date('M-Y');
|
|
|
|
|
|
|
|
|
|
|
516 |
}
|
517 |
+
}
|
518 |
|
519 |
+
public static function gawd_cd_published_year()
|
520 |
+
{
|
521 |
+
if (is_singular()) {
|
522 |
+
return get_the_date('Y');
|
523 |
+
}
|
524 |
+
}
|
525 |
+
|
526 |
+
public function get_management_filters()
|
527 |
+
{
|
528 |
+
$analytics = $this->analytics_member;
|
529 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
530 |
+
$accountId = $this->get_profile_accountId();
|
531 |
+
$profileId = $this->get_profile_id();
|
532 |
+
$webPropertyId = $this->get_profile_webPropertyId();
|
533 |
+
if ((isset($accountId) && $accountId != '') && (isset($webPropertyId) && $webPropertyId != '') && (isset($profileId) && $profileId != '')) {
|
534 |
+
try {
|
535 |
+
$view_filters = $analytics->management_profileFilterLinks->listManagementProfileFilterLinks($accountId, $webPropertyId, $profileId);
|
536 |
+
$filters = $view_filters->getItems();
|
537 |
+
foreach ($filters as $filter) {
|
538 |
+
if (!isset($filter['modelData']['filterRef'])) {
|
539 |
+
continue;
|
|
|
540 |
}
|
541 |
+
$filter_info = $analytics->management_filters->get($accountId, $filter['modelData']['filterRef']['id']);
|
542 |
+
$all_filters[] = array(
|
543 |
+
'name' => $filter['modelData']['filterRef']['name'],
|
544 |
+
'id' => $filter['modelData']['filterRef']['id'],
|
545 |
+
'type' => $filter_info['excludeDetails']['field'],
|
546 |
+
'value' => $filter_info['excludeDetails']['expressionValue'],
|
547 |
+
'view' => $filter['modelData']['profileRef']['name']
|
548 |
+
);
|
549 |
+
}
|
550 |
+
if (isset($all_filters)) {
|
551 |
+
return $all_filters;
|
552 |
+
}
|
553 |
+
} catch (Exception $e) {
|
554 |
+
|
555 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
556 |
+
$fh = fopen($myFile, 'a');
|
557 |
+
fwrite($fh, $e->getMessage() . "----get_management_filters function" . PHP_EOL);
|
558 |
+
fclose($fh);
|
559 |
+
|
560 |
+
$error = array('error_message' => 'Error');
|
561 |
+
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this ')) {
|
562 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
563 |
+
}
|
564 |
+
return json_encode($error);
|
565 |
}
|
566 |
}
|
567 |
+
}
|
568 |
+
|
569 |
+
public function get_management_goals()
|
570 |
+
{
|
571 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
572 |
+
$profileId = $this->get_profile_id();
|
573 |
+
$accountId = $this->get_profile_accountId();
|
574 |
+
$webPropertyId = $this->get_profile_webPropertyId();
|
575 |
+
$goals = array();
|
576 |
+
if (isset($profileId) && $profileId != '' && isset($webPropertyId) && $webPropertyId != '') {
|
577 |
+
try {
|
578 |
+
$goals = $this->analytics_member->management_goals->listManagementGoals($accountId, $webPropertyId, $profileId)->getItems();
|
579 |
+
} catch (Exception $e) {
|
580 |
+
|
581 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
582 |
+
$fh = fopen($myFile, 'a');
|
583 |
+
fwrite($fh, $e->getMessage() . "----get_management_goals function" . PHP_EOL);
|
584 |
+
fclose($fh);
|
585 |
|
586 |
+
}
|
587 |
+
if (0 == sizeof($goals)) {
|
588 |
+
return "no_goals_exist";
|
589 |
+
}
|
590 |
+
else {
|
591 |
+
foreach ($goals as $goal) {
|
592 |
+
$goals_light[] = array(
|
593 |
+
'name' => $goal['name'],
|
594 |
+
'id' => $goal['id']
|
595 |
+
);
|
596 |
+
}
|
597 |
+
return $goals_light;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
598 |
}
|
599 |
}
|
600 |
+
}
|
601 |
+
|
602 |
+
public function get_default_goals()
|
603 |
+
{
|
604 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
605 |
+
$accountId = $this->get_default_accountId();
|
606 |
+
$webPropertyId = $this->get_default_webPropertyId();
|
607 |
+
$goals = array();
|
608 |
+
try {
|
609 |
+
$goals = $this->analytics_member->management_goals->listManagementGoals($accountId, $webPropertyId, '~all')->getItems();
|
610 |
+
} catch (Exception $e) {
|
611 |
+
|
612 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
613 |
+
$fh = fopen($myFile, 'a');
|
614 |
+
fwrite($fh, $e->getMessage() . "----get_default_goals function" . PHP_EOL);
|
615 |
+
fclose($fh);
|
616 |
|
617 |
+
}
|
618 |
+
if (0 == sizeof($goals)) {
|
619 |
+
return "no_goals_exist";
|
620 |
+
}
|
621 |
+
else {
|
622 |
+
$profiles = array();
|
623 |
+
foreach ($goals as $goal) {
|
624 |
+
$flag = false;
|
625 |
+
foreach ($profiles as $profile) {
|
626 |
+
if ($profile == $goal['profileId']) {
|
627 |
+
$flag = true;
|
628 |
+
}
|
|
|
|
|
629 |
}
|
630 |
+
if ($flag == false) {
|
631 |
+
$profiles[] = $goal['profileId'];
|
632 |
+
}
|
633 |
+
}
|
634 |
+
$goals_light = array();
|
635 |
+
$caseSensitive = '';
|
636 |
+
foreach ($profiles as $profile) {
|
637 |
+
foreach ($goals as $goal) {
|
638 |
+
if ($goal['profileId'] == $profile) {
|
639 |
+
if ($goal['type'] == 'URL_DESTINATION') {
|
640 |
+
$type = 'Destination';
|
641 |
+
if ($goal["modelData"]['urlDestinationDetails']['matchType'] == 'EXACT') {
|
642 |
+
$match_type = 'Equals';
|
643 |
+
}
|
644 |
+
elseif ($goal["modelData"]['urlDestinationDetails']['matchType'] == 'HEAD') {
|
645 |
+
$match_type = 'Begin with';
|
646 |
+
}
|
647 |
+
else {
|
648 |
+
$match_type = 'Regular expresion';
|
649 |
+
}
|
650 |
+
$value = $goal["modelData"]['urlDestinationDetails']['url'];
|
651 |
+
$caseSensitive = $goal["modelData"]['urlDestinationDetails']['caseSensitive'];
|
652 |
}
|
653 |
+
elseif ($goal['type'] == 'VISIT_TIME_ON_SITE') {
|
654 |
+
$type = 'Duration';
|
655 |
+
if ($goal["modelData"]['visitTimeOnSiteDetails']['comparisonType'] == 'GREATER_THAN') {
|
656 |
+
$match_type = 'Greater than';
|
657 |
+
}
|
658 |
+
$value = $goal["modelData"]['visitTimeOnSiteDetails']['comparisonValue'];
|
659 |
+
$hours = strlen(floor($value / 3600)) < 2 ? '0' . floor($value / 3600) : floor($value / 3600);
|
660 |
+
$mins = strlen(floor($value / 60 % 60)) < 2 ? '0' . floor($value / 60 % 60) : floor($value / 60 % 60);
|
661 |
+
$secs = strlen(floor($value % 60)) < 2 ? '0' . floor($value % 60) : floor($value % 60);
|
662 |
+
$value = $hours . ':' . $mins . ':' . $secs;
|
663 |
+
}
|
664 |
+
else {
|
665 |
+
$type = 'Pages/Screens per session';
|
666 |
+
if ($goal["modelData"]['visitNumPagesDetails']['comparisonType'] == 'GREATER_THAN') {
|
667 |
+
$match_type = 'Greater than';
|
668 |
+
}
|
669 |
+
$value = $goal["modelData"]['visitNumPagesDetails']['comparisonValue'];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
670 |
}
|
671 |
+
|
672 |
+
$goals_light[$profile][] = array(
|
673 |
+
'name' => $goal['name'],
|
674 |
+
'id' => $goal['id'],
|
675 |
+
'type' => $type,
|
676 |
+
'match_type' => $match_type,
|
677 |
+
'profileID' => $goal['profileId'],
|
678 |
+
'caseSensitive' => $caseSensitive,
|
679 |
+
'value' => $value,
|
680 |
+
);
|
681 |
+
}
|
682 |
}
|
683 |
+
}
|
684 |
+
return $goals_light;
|
685 |
+
}
|
686 |
+
}
|
687 |
+
|
688 |
+
public function add_custom_dimension($name, $id)
|
689 |
+
{
|
690 |
+
|
691 |
+
$custom_dimension = new Google_Service_Analytics_CustomDimension();
|
692 |
+
$custom_dimension->setId($id);
|
693 |
+
$custom_dimension->setActive(TRUE);
|
694 |
+
$custom_dimension->setScope('Hit');
|
695 |
+
$custom_dimension->setName($name);
|
696 |
+
|
697 |
+
$accountId = $this->get_default_accountId();
|
698 |
+
$webPropertyId = $this->get_default_webPropertyId();
|
699 |
+
$analytics = $this->analytics_member;
|
700 |
+
delete_transient('gawd-custom-dimensions-' . $webPropertyId);
|
701 |
+
try {
|
702 |
+
$analytics->management_customDimensions->insert($accountId, $webPropertyId, $custom_dimension);
|
703 |
+
} catch (apiServiceException $e) {
|
704 |
+
echo 'There was an Analytics API service error '
|
705 |
+
. $e->getCode() . ':' . $e->getMessage();
|
706 |
+
} catch (apiException $e) {
|
707 |
+
echo 'There was a general API error '
|
708 |
+
. $e->getCode() . ':' . $e->getMessage();
|
709 |
+
} catch (Exception $e) {
|
710 |
+
|
711 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
712 |
+
$fh = fopen($myFile, 'a');
|
713 |
+
fwrite($fh, $e->getMessage() . "----add_custom_dimension function" . PHP_EOL);
|
714 |
+
fclose($fh);
|
715 |
+
|
716 |
+
if (strpos($e->getMessage(), 'not found.') !== false) {
|
717 |
+
echo '<div class="notice notice-error"><p>Something went wrong.</p></div>';
|
718 |
+
return 'error';
|
719 |
+
}
|
720 |
+
}
|
721 |
+
}
|
722 |
+
|
723 |
+
public function add_goal($gawd_goal_profile, $goal_max_id, $gawd_goal_type, $gawd_goal_name, $gawd_goal_comparison = "GREATER_THAN", $gawd_goal_value, $url_case_sensitve = 'false')
|
724 |
+
{
|
725 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
726 |
+
/* This request creates a new Goal. */
|
727 |
+
// Construct the body of the request.
|
728 |
+
$goal = new Google_Service_Analytics_Goal();
|
729 |
+
$goal->setId($goal_max_id); //ID
|
730 |
+
$goal->setActive(True); //ACTIVE/INACTIVE
|
731 |
+
$goal->setType($gawd_goal_type); //URL_DESTINATION, VISIT_TIME_ON_SITE, VISIT_NUM_PAGES, AND EVENT
|
732 |
+
$goal->setName($gawd_goal_name); //NAME
|
733 |
+
// Construct the time on site details.
|
734 |
+
if ($gawd_goal_type == 'VISIT_TIME_ON_SITE') {
|
735 |
+
$details = new Google_Service_Analytics_GoalVisitTimeOnSiteDetails();
|
736 |
+
$details->setComparisonType($gawd_goal_comparison); //VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
737 |
+
$details->setComparisonValue($gawd_goal_value);
|
738 |
+
$goal->setVisitTimeOnSiteDetails($details);
|
739 |
+
}
|
740 |
+
elseif ($gawd_goal_type == 'URL_DESTINATION') {
|
741 |
+
if ($url_case_sensitve != '') {
|
742 |
+
$url_case_sensitve = true;
|
743 |
+
}
|
744 |
+
$details = new Google_Service_Analytics_GoalUrlDestinationDetails();
|
745 |
+
$details->setCaseSensitive($url_case_sensitve);
|
746 |
+
$details->setFirstStepRequired('false');
|
747 |
+
$details->setMatchType($gawd_goal_comparison);
|
748 |
+
$details->setUrl($gawd_goal_value);
|
749 |
+
$goal->setUrlDestinationDetails($details);
|
750 |
+
}
|
751 |
+
elseif ($gawd_goal_type == 'VISIT_NUM_PAGES') {
|
752 |
+
$details = new Google_Service_Analytics_GoalVisitNumPagesDetails();
|
753 |
+
$details->setComparisonType($gawd_goal_comparison); //VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
754 |
+
$details->setComparisonValue($gawd_goal_value);
|
755 |
+
$goal->setVisitNumPagesDetails($details);
|
756 |
+
}
|
757 |
+
elseif ($gawd_goal_type == 'EVENT') {
|
758 |
+
/* $details = new Google_Service_Analytics_GoalEventDetails();
|
759 |
+
$details = new Google_Service_Analytics_GoalEventDetailsEventConditions();
|
760 |
+
$detailssetComparisonType
|
761 |
+
//$details->setEventConditions($gawd_goal_comparison);//VISIT_TIME_ON_SITE -------- LESS_THAN/ GREATER_THAN------
|
762 |
+
//$details->setUseEventValue($gawd_goal_value); */
|
763 |
+
// $goal->setEventDetails($details);
|
764 |
}
|
765 |
|
766 |
+
//Set the time on site details.
|
767 |
+
$this->analytics_member;
|
768 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
769 |
+
$accountId = $this->get_default_accountId();
|
770 |
+
$webPropertyId = $this->get_default_webPropertyId();
|
771 |
+
$profileId = $gawd_goal_profile;
|
772 |
+
$analytics = $this->analytics_member;
|
773 |
+
try {
|
774 |
+
$analytics->management_goals->insert($accountId, $webPropertyId, $profileId, $goal);
|
775 |
+
} catch (apiServiceException $e) {
|
776 |
+
echo 'There was an Analytics API service error '
|
777 |
+
. $e->getCode() . ':' . $e->getMessage();
|
778 |
+
} catch (apiException $e) {
|
779 |
+
echo 'There was a general API error '
|
780 |
+
. $e->getCode() . ':' . $e->getMessage();
|
781 |
+
} catch (Exception $e) {
|
782 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
783 |
+
$fh = fopen($myFile, 'a');
|
784 |
+
fwrite($fh, $e->getMessage() . "----add goal function" . PHP_EOL);
|
785 |
+
fclose($fh);
|
786 |
+
$error = array('error_message' => $e->getMessage());
|
787 |
+
if (strpos($e->getMessage(), 'User does not have permission to perform this operation')) {
|
788 |
+
$error['error_message'] = 'User does not have permission to perform this operation';
|
789 |
+
}
|
790 |
+
return json_encode($error);
|
791 |
+
}
|
792 |
+
}
|
793 |
+
|
794 |
+
public function add_filter($name, $type, $value)
|
795 |
+
{
|
796 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
797 |
+
$accountId = $this->get_profile_accountId();
|
798 |
+
$profileId = $this->get_profile_id();
|
799 |
+
$webPropertyId = $this->get_profile_webPropertyId();
|
800 |
+
$analytics = $this->analytics_member;
|
801 |
+
$condition = $type == 'GEO_IP_ADDRESS' ? 'EQUAL' : 'MATCHES';
|
802 |
+
/**
|
803 |
+
* Note: This code assumes you have an authorized Analytics service object.
|
804 |
+
* See the Filters Developer Guide for details.
|
805 |
+
*/
|
806 |
+
/**
|
807 |
+
* This request creates a new filter.
|
808 |
+
*/
|
809 |
+
try {
|
810 |
+
// Construct the filter expression object.
|
811 |
+
$details = new Google_Service_Analytics_FilterExpression();
|
812 |
+
$details->setField($type);
|
813 |
+
$details->setMatchType($type);
|
814 |
+
$details->setExpressionValue($value);
|
815 |
+
$details->setCaseSensitive(false);
|
816 |
+
// Construct the filter and set the details.
|
817 |
+
$filter = new Google_Service_Analytics_Filter();
|
818 |
+
$filter->setName($name);
|
819 |
+
$filter->setType("EXCLUDE");
|
820 |
+
$filter->setExcludeDetails($details);
|
821 |
+
|
822 |
+
$insertedFilter = $analytics->management_filters->insert($accountId, $filter);
|
823 |
+
$analyticsFilterRef = new Google_Service_Analytics_FilterRef();
|
824 |
+
$analyticsFilterRef->setId($insertedFilter->id);
|
825 |
+
$filterData = new Google_Service_Analytics_ProfileFilterLink();
|
826 |
+
$filterData->setFilterRef($analyticsFilterRef);
|
827 |
+
// Add view to inserted filter
|
828 |
+
$res = $analytics->management_profileFilterLinks->insert($accountId, $webPropertyId, $profileId, $filterData);
|
829 |
+
|
830 |
+
} catch (apiServiceException $e) {
|
831 |
+
echo 'There was an Analytics API service error '
|
832 |
+
. $e->getCode() . ':' . $e->getMessage();
|
833 |
+
} catch (apiException $e) {
|
834 |
+
echo 'There was a general API error '
|
835 |
+
. $e->getCode() . ':' . $e->getMessage();
|
836 |
+
} catch (Exception $e) {
|
837 |
+
|
838 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
839 |
+
$fh = fopen($myFile, 'a');
|
840 |
+
fwrite($fh, $e->getMessage() . "----add_filter function" . PHP_EOL);
|
841 |
+
fclose($fh);
|
842 |
+
|
843 |
+
echo '<script>window.location.href="' . admin_url() . 'admin.php?page=gawd_settings&errorMsg=1#gawd_filters_tab";</script>';
|
844 |
+
}
|
845 |
+
}
|
846 |
+
|
847 |
+
public function get_country_data($metric, $dimension, $start_date, $end_date, $country_filter, $geo_type, $timezone)
|
848 |
+
{
|
849 |
+
$profileId = $this->get_profile_id();
|
850 |
+
$analytics = $this->analytics_member;
|
851 |
+
$metric = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration';
|
852 |
+
|
853 |
+
try {
|
854 |
+
$results = $analytics->data_ga->get(
|
855 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
856 |
+
'dimensions' => 'ga:' . $dimension,
|
857 |
+
'sort' => 'ga:' . $dimension,
|
858 |
+
'filters' => 'ga:' . $geo_type . '==' . $country_filter
|
859 |
+
)
|
860 |
+
);
|
861 |
+
} catch (Exception $e) {
|
862 |
+
|
863 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
864 |
+
$fh = fopen($myFile, 'a');
|
865 |
+
fwrite($fh, $e->getMessage() . "----get_country_data function" . PHP_EOL);
|
866 |
+
fclose($fh);
|
867 |
+
|
868 |
+
$error = array('error_message' => 'Error');
|
869 |
+
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
870 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
871 |
+
}
|
872 |
+
return json_encode($error);
|
873 |
+
}
|
874 |
+
$rows = $results->getRows();
|
875 |
+
$metric = explode(',', $metric);
|
876 |
+
if ($rows) {
|
877 |
+
$data_sum = array();
|
878 |
+
foreach ($results->getTotalsForAllResults() as $key => $value) {
|
879 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($key, 3))))] = $value;
|
880 |
+
}
|
881 |
+
$j = 0;
|
882 |
+
foreach ($rows as $row) {
|
883 |
+
$data[$j] = array(
|
884 |
+
ucfirst($dimension) => $row[0]
|
885 |
+
);
|
886 |
+
$data[$j]['No'] = floatval($j + 1);
|
887 |
+
for ($i = 0; $i < count($metric); $i++) {
|
888 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = floatval($row[$i + 1]);
|
889 |
}
|
890 |
+
$j++;
|
891 |
+
}
|
892 |
+
}
|
893 |
+
else {
|
894 |
+
$empty[0] = array(
|
895 |
+
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => 0
|
896 |
+
);
|
897 |
+
$empty[0]['No'] = 1;
|
898 |
+
for ($i = 0; $i < count($metric); $i++) {
|
899 |
+
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
900 |
+
}
|
901 |
|
902 |
+
return json_encode($empty);
|
903 |
+
}
|
904 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
905 |
+
$result = $data;
|
906 |
+
if ($data_sum != '') {
|
907 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
908 |
+
}
|
909 |
+
set_transient('gawd-country-' . $profileId . '-' . $country_filter . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
910 |
+
return json_encode($result);
|
911 |
+
}
|
912 |
+
|
913 |
+
public function get_post_page_data($metric, $dimension, $start_date, $end_date, $filter, $timezone, $chart)
|
914 |
+
{
|
915 |
+
$profileId = $this->get_profile_id();
|
916 |
+
$analytics = $this->analytics_member;
|
917 |
+
$metric = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration,ga:pageviewsPerSession';
|
918 |
+
if ($chart == 'pie') {
|
919 |
+
$diff = date_diff(date_create($start_date), date_create($end_date));
|
920 |
+
if (intval($diff->format("%a")) > 7) {
|
921 |
+
$dimension = 'week';
|
922 |
+
}
|
923 |
+
if (intval($diff->format("%a")) > 60) {
|
924 |
+
$dimension = 'month';
|
925 |
+
}
|
926 |
+
}
|
927 |
+
// Get the results from the Core Reporting API and print the results.
|
928 |
+
// Calls the Core Reporting API and queries for the number of sessions
|
929 |
+
// for the last seven days.
|
930 |
+
|
931 |
+
$filter_type = 'pagePath';
|
932 |
+
|
933 |
+
try {
|
934 |
+
$results = $analytics->data_ga->get(
|
935 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
936 |
+
'dimensions' => 'ga:' . $dimension,
|
937 |
+
'sort' => 'ga:' . $dimension,
|
938 |
+
'filters' => 'ga:' . $filter_type . '=~' . $filter
|
939 |
+
)
|
940 |
+
);
|
941 |
+
} catch (Exception $e) {
|
942 |
+
|
943 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
944 |
+
$fh = fopen($myFile, 'a');
|
945 |
+
fwrite($fh, $e->getMessage() . "----get_post_page_data function" . PHP_EOL);
|
946 |
+
fclose($fh);
|
947 |
+
|
948 |
+
$error = array('error_message' => 'Error');
|
949 |
+
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
950 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
951 |
+
}
|
952 |
+
return json_encode($error);
|
953 |
+
}
|
954 |
|
955 |
+
$rows = $results->getRows();
|
956 |
+
$metric = explode(',', $metric);
|
957 |
+
if ($rows) {
|
958 |
+
$j = 0;
|
959 |
+
$data_sum = array();
|
960 |
+
/*if ($dimension == 'week') {
|
961 |
+
$date = $start_date;
|
962 |
+
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
963 |
+
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
964 |
+
$end_date = date("M d,Y");
|
965 |
+
}
|
966 |
+
foreach ($rows as $row) {
|
967 |
+
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
968 |
+
|
969 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
970 |
+
// echo $date;
|
971 |
+
}
|
972 |
+
else {
|
973 |
+
|
974 |
+
if (strtotime($date) != strtotime($end_date)) {
|
975 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
976 |
+
} else {
|
977 |
+
break;
|
978 |
+
}
|
979 |
+
}
|
980 |
+
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
981 |
+
$data[$j]['No'] = floatval($j + 1);
|
982 |
+
for ($i = 0; $i < count($metric); $i++) {
|
983 |
+
$val = $i + 1;
|
984 |
+
$metric_val = floatval($row[$val]);
|
985 |
+
if(substr($metric[$i], 3) == 'bounceRate'){
|
986 |
+
$metric_val = $metric_val;
|
987 |
+
}
|
988 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
989 |
+
}
|
990 |
+
$j++;
|
991 |
+
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
992 |
+
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
993 |
+
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
994 |
+
$_end_date = $end_date;
|
995 |
+
}
|
996 |
+
}
|
997 |
+
}*/
|
998 |
+
if ($dimension == 'week' || $dimension == 'month') {
|
999 |
+
$date = $start_date;
|
1000 |
+
if ($dimension == 'week') {
|
1001 |
+
$_end_date = date("l", strtotime($date)) == 'Saturday' ? date("M d,Y", strtotime($date)) : date("M d,Y", strtotime('next Saturday ' . $date));
|
1002 |
+
}
|
1003 |
+
elseif ($dimension == 'month') {
|
1004 |
+
$_end_date = date("M t,Y", strtotime($date));
|
1005 |
+
if (strtotime($_end_date) > strtotime(date('Y-m-d'))) {
|
1006 |
+
$_end_date = date("M d,Y", strtotime('-1 day ' . date('Y-m-d')));
|
1007 |
}
|
1008 |
}
|
1009 |
+
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
1010 |
+
$end_date = date("M d,Y");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1011 |
}
|
1012 |
+
foreach ($rows as $row) {
|
1013 |
+
if ($dimension == 'hour') {
|
1014 |
+
$dimension_value = date("M d,Y", strtotime($row[0])) . ' ' . $row[1] . ':00';
|
1015 |
+
}
|
1016 |
+
else {
|
1017 |
+
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
1018 |
|
1019 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
1020 |
+
// echo $date;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1021 |
}
|
1022 |
+
else {
|
1023 |
+
if ($dimension == 'month') {
|
1024 |
+
//continue;
|
1025 |
+
}
|
1026 |
+
if (strtotime($date) != strtotime($end_date)) {
|
1027 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
1028 |
+
}
|
1029 |
+
else {
|
1030 |
+
break;
|
1031 |
+
}
|
1032 |
+
}
|
1033 |
+
}
|
1034 |
+
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
1035 |
+
$data[$j]['No'] = floatval($j + 1);
|
1036 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1037 |
+
$val = $i + 1;
|
1038 |
+
if ($dimension == 'hour') {
|
1039 |
+
$val = $i + 2;
|
1040 |
+
}
|
1041 |
+
$metric_val = floatval($row[$val]);
|
1042 |
+
if (substr($metric[$i], 3) == 'bounceRate') {
|
1043 |
+
$metric_val = $metric_val;
|
1044 |
+
}
|
1045 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
1046 |
+
}
|
1047 |
|
1048 |
+
$j++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1049 |
|
1050 |
+
if (isset($break) && $break) {
|
1051 |
+
break;
|
1052 |
+
}
|
|
|
|
|
|
|
|
|
1053 |
|
1054 |
+
if ($dimension == 'week') {
|
1055 |
+
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
1056 |
+
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
1057 |
+
}
|
1058 |
+
elseif ($dimension == 'month') {
|
1059 |
+
$date = date("M d,Y", strtotime('+1 day ' . $_end_date));
|
1060 |
+
$_end_date = date("M t,Y", strtotime($date));
|
1061 |
+
}
|
1062 |
+
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
1063 |
+
$_end_date = date("M d,Y", strtotime($end_date));
|
1064 |
+
$break = true;
|
1065 |
+
}
|
1066 |
+
}
|
1067 |
+
}
|
1068 |
+
else {
|
1069 |
+
foreach ($rows as $row) {
|
1070 |
|
1071 |
+
if ($dimension == 'date') {
|
1072 |
+
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1073 |
+
}
|
1074 |
+
$data[$j] = array(
|
1075 |
+
$dimension => $row[0]
|
1076 |
+
);
|
1077 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1078 |
+
$data[$j][substr($metric[$i], 3)] = floatval($row[$i + 1]);
|
1079 |
+
if (isset($data_sum[substr($metric[$i], 3)])) {
|
1080 |
+
$data_sum[substr($metric[$i], 3)] += floatval($row[$i + 1]);
|
1081 |
+
}
|
1082 |
+
else {
|
1083 |
+
if (substr($metric[$i], 3) != 'percentNewSessions' && substr($metric[$i], 3) != 'bounceRate') {
|
1084 |
+
$data_sum[substr($metric[$i], 3)] = floatval($row[$i + 1]);
|
1085 |
+
}
|
1086 |
+
}
|
1087 |
+
}
|
1088 |
+
$j++;
|
1089 |
}
|
1090 |
+
}
|
1091 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1092 |
+
if (isset($same_dimension) && $same_dimension != null) {
|
1093 |
+
$dimension = $same_dimension;
|
1094 |
+
}
|
1095 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1096 |
+
set_transient('gawd-page-post-' . $profileId . '-' . $filter . '-' . '-' . $dimension . '-' . $start_date . '-' . $end_date . '-' . $chart, json_encode($result), $expiration);
|
1097 |
+
return json_encode($result);
|
1098 |
}
|
1099 |
+
else {
|
1100 |
+
$empty[0][$dimension] = date("Y-m-d");
|
1101 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1102 |
+
$empty[0][trim(str_replace(" ", "", preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1103 |
+
}
|
1104 |
+
return json_encode($empty);
|
1105 |
+
}
|
1106 |
+
}
|
1107 |
+
|
1108 |
+
public function get_data($metric, $dimension, $start_date, $end_date, $filter_type, $timezone, $same_dimension = null)
|
1109 |
+
{
|
1110 |
+
if (function_exists('lcfirst') === false) {
|
1111 |
+
function lcfirst($str)
|
1112 |
+
{
|
1113 |
+
$str[0] = strtolower($str[0]);
|
1114 |
+
return $str;
|
1115 |
+
}
|
1116 |
+
}
|
1117 |
+
$dimension = lcfirst($dimension);
|
1118 |
|
1119 |
+
$metric = lcfirst($metric);
|
1120 |
+
$profileId = $this->get_profile_id();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1121 |
|
1122 |
+
$analytics = $this->analytics_member;
|
1123 |
+
$selected_metric = $metric;
|
1124 |
+
if (strpos($selected_metric, 'ga:') > -1) {
|
1125 |
+
$selected_metric = substr($selected_metric, 3);
|
1126 |
+
}
|
1127 |
+
if (strpos($metric, 'ga:') === false) {
|
1128 |
+
$metric = 'ga:' . $metric;
|
1129 |
+
}
|
1130 |
+
if ($dimension == 'interestInMarketCategory' || $dimension == 'interestAffinityCategory' || $dimension == 'interestOtherCategory' || $dimension == 'country' || $dimension == 'language' || $dimension == 'userType' || $dimension == 'sessionDurationBucket' || $dimension == 'userAgeBracket' || $dimension == 'userGender' || $dimension == 'mobileDeviceInfo' || $dimension == 'deviceCategory' || $dimension == 'operatingSystem' || $dimension == 'browser' || $dimension == 'date' || $dimension == "source") {
|
1131 |
+
$metrics = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration,ga:pageviewsPerSession';
|
1132 |
|
1133 |
+
if (strpos($metrics, $metric) !== false) {
|
1134 |
+
$metric = $metrics;
|
1135 |
+
}
|
1136 |
+
}
|
1137 |
+
elseif ($dimension == 'siteSpeed') {
|
1138 |
+
$dimension = 'date';
|
1139 |
+
$metrics = 'ga:avgPageLoadTime,ga:avgRedirectionTime,ga:avgServerResponseTime,ga:avgPageDownloadTime';
|
1140 |
+
if (strpos($metrics, $metric) !== false) {
|
1141 |
+
$metric = $metrics;
|
1142 |
+
}
|
1143 |
+
}
|
1144 |
+
elseif ($dimension == 'eventLabel' || $dimension == 'eventAction' || $dimension == 'eventCategory') {
|
1145 |
+
$metrics = 'ga:eventsPerSessionWithEvent,ga:sessionsWithEvent,ga:avgEventValue,ga:eventValue,ga:uniqueEvents,ga:totalEvents';
|
1146 |
+
if (strpos($metrics, $metric) !== false) {
|
1147 |
+
$metric = $metrics;
|
1148 |
+
}
|
1149 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1150 |
|
1151 |
+
$dimension = $dimension == 'date' ? $filter_type != '' ? $filter_type : 'date' : $dimension;
|
1152 |
+
if ($same_dimension == 'sales_performance' && ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour')) {
|
1153 |
+
$metrics = 'ga:transactionRevenue, ga:transactionsPerSession';
|
1154 |
+
if (strpos($metrics, $metric) !== false) {
|
1155 |
+
$metric = $metrics;
|
1156 |
+
}
|
1157 |
+
}
|
1158 |
+
elseif ($same_dimension == 'adsense' && ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour')) {
|
1159 |
+
$metrics = 'ga:adsenseRevenue,ga:adsenseAdsClicks';
|
1160 |
+
if (strpos($metrics, $metric) !== false) {
|
1161 |
+
$metric = $metrics;
|
1162 |
+
}
|
1163 |
+
}
|
1164 |
+
elseif ($same_dimension == 'siteSpeed' && ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour')) {
|
1165 |
+
$metrics = 'ga:avgPageLoadTime,ga:avgRedirectionTime,ga:avgServerResponseTime,ga:avgPageDownloadTime';
|
1166 |
+
if (strpos($metrics, $metric) !== false) {
|
1167 |
+
$metric = $metrics;
|
1168 |
+
}
|
1169 |
}
|
1170 |
|
1171 |
+
if ($same_dimension == 'week' || $same_dimension == 'month' || $same_dimension == 'hour') {
|
1172 |
+
$metrics = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration';
|
1173 |
+
if (strpos($metrics, $metric) !== false) {
|
1174 |
+
$metric = $metrics;
|
1175 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1176 |
}
|
1177 |
|
1178 |
+
/* if(!is_array($metric)){
|
1179 |
+
if (strpos($metric, 'ga') === false) {
|
1180 |
+
$metric = 'ga:' . $metric;
|
1181 |
+
}
|
1182 |
+
} */
|
1183 |
+
// Get the results from the Core Reporting API and print the results.
|
1184 |
+
// Calls the Core Reporting API and queries for the number of sessions
|
1185 |
+
// for the last seven days.
|
1186 |
+
if ($dimension == 'hour') {
|
1187 |
+
|
1188 |
+
$gawd_dimension = array(
|
1189 |
+
'dimensions' => 'ga:date, ga:hour',
|
1190 |
+
'sort' => 'ga:date',
|
1191 |
+
);
|
1192 |
+
}
|
1193 |
+
else {
|
1194 |
+
if ($dimension != 'sessionDurationBucket') {
|
1195 |
+
$gawd_dimension = array(
|
1196 |
+
'dimensions' => 'ga:' . $dimension,
|
1197 |
+
'sort' => '-ga:' . $selected_metric,
|
1198 |
+
);
|
1199 |
+
}
|
1200 |
+
else {
|
1201 |
+
$gawd_dimension = array(
|
1202 |
+
'dimensions' => 'ga:' . $dimension,
|
1203 |
+
'sort' => 'ga:' . $dimension,
|
1204 |
+
);
|
1205 |
}
|
1206 |
+
}
|
1207 |
+
if (isset($profileId) && $profileId != '') {
|
1208 |
+
try {
|
1209 |
+
$results = $analytics->data_ga->get(
|
1210 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, $gawd_dimension
|
1211 |
+
);
|
1212 |
|
1213 |
+
} catch (Exception $e) {
|
1214 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
1215 |
+
$fh = fopen($myFile, 'a');
|
1216 |
+
fwrite($fh, $e->getMessage() . "----get_data function" . PHP_EOL);
|
1217 |
+
fclose($fh);
|
1218 |
+
$error = array('error_message' => 'Error');
|
1219 |
+
if (strpos($e->getMessage(), 'Selected dimensions and metrics cannot be queried together')) {
|
1220 |
+
$error['error_message'] = 'Selected dimensions and metrics cannot be queried together';
|
1221 |
}
|
1222 |
+
else if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
1223 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
1224 |
}
|
1225 |
+
return json_encode($error);
|
1226 |
+
}
|
1227 |
|
1228 |
+
$metric = explode(',', $metric);
|
1229 |
+
$rows = $results->getRows();
|
1230 |
+
if ($rows) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1231 |
|
1232 |
+
$j = 0;
|
1233 |
+
$data_sum = array();
|
1234 |
+
foreach ($results->getTotalsForAllResults() as $key => $value) {
|
1235 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($key, 3))))] = $value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1236 |
}
|
1237 |
|
1238 |
+
if ($dimension == 'week' || $dimension == 'month' || $dimension == 'hour') {
|
|
|
|
|
|
|
|
|
|
|
1239 |
|
1240 |
+
$date = $start_date;
|
1241 |
+
if ($dimension == 'week') {
|
1242 |
+
$_end_date = date("l", strtotime($date)) == 'Saturday' ? date("M d,Y", strtotime($date)) : date("M d,Y", strtotime('next Saturday ' . $date));
|
1243 |
}
|
1244 |
+
elseif ($dimension == 'month') {
|
1245 |
+
$_end_date = date("M t,Y", strtotime($date));
|
1246 |
+
if (strtotime($_end_date) > strtotime(date('Y-m-d'))) {
|
1247 |
+
$_end_date = date("M d,Y", strtotime('-1 day ' . date('Y-m-d')));
|
1248 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1249 |
}
|
1250 |
+
if (strtotime($end_date) > strtotime(date('Y-m-d'))) {
|
1251 |
+
$end_date = date("M d,Y");
|
|
|
|
|
|
|
1252 |
}
|
1253 |
+
foreach ($rows as $row) {
|
1254 |
+
if ($dimension == 'hour') {
|
1255 |
+
$dimension_value = date("M d,Y", strtotime($row[0])) . ' ' . $row[1] . ':00';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1256 |
}
|
1257 |
+
else {
|
1258 |
+
if (strtotime($_end_date) <= strtotime(date('Y-m-d'))) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1259 |
|
1260 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
1261 |
+
// echo $date;
|
1262 |
+
}
|
1263 |
+
else {
|
1264 |
+
if ($dimension == 'month') {
|
1265 |
+
//continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
1266 |
}
|
1267 |
+
if (strtotime($date) != strtotime($end_date)) {
|
1268 |
+
$dimension_value = date("M d,Y", strtotime($date)) . '-' . $_end_date;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1269 |
}
|
1270 |
+
else {
|
1271 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1272 |
}
|
1273 |
+
}
|
1274 |
}
|
1275 |
+
$data[] = array(trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $dimension_value);
|
1276 |
+
$data[$j]['No'] = floatval($j + 1);
|
1277 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1278 |
+
$val = $i + 1;
|
1279 |
+
if ($dimension == 'hour') {
|
1280 |
+
$val = $i + 2;
|
1281 |
+
}
|
1282 |
+
$metric_val = floatval($row[$val]);
|
1283 |
+
if (substr($metric[$i], 3) == 'bounceRate') {
|
1284 |
+
$metric_val = $metric_val;
|
1285 |
+
}
|
1286 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
|
|
1287 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1288 |
|
1289 |
+
$j++;
|
1290 |
|
1291 |
+
if (isset($break) && $break) {
|
1292 |
+
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1293 |
}
|
1294 |
+
|
1295 |
+
if ($dimension == 'week') {
|
1296 |
+
$date = date("M d,Y", strtotime('next Sunday ' . $_end_date));
|
1297 |
+
$_end_date = date("M d,Y", strtotime('next Saturday ' . $date));
|
1298 |
+
}
|
1299 |
+
elseif ($dimension == 'month') {
|
1300 |
+
$date = date("M d,Y", strtotime('+1 day ' . $_end_date));
|
1301 |
+
$_end_date = date("M t,Y", strtotime($date));
|
|
|
|
|
|
|
|
|
1302 |
}
|
1303 |
+
if (isset($_end_date) && (strtotime($_end_date) > strtotime($end_date))) {
|
1304 |
+
$_end_date = date("M d,Y", strtotime($end_date));
|
1305 |
+
$break = true;
|
1306 |
}
|
1307 |
+
}
|
|
|
1308 |
}
|
1309 |
else {
|
1310 |
+
foreach ($rows as $row) {
|
1311 |
+
if (strtolower($dimension) == 'date') {
|
1312 |
+
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1313 |
+
}
|
1314 |
+
elseif (strtolower($dimension) == 'sessiondurationbucket') {
|
1315 |
+
if ($row[0] >= 0 && $row[0] <= 10) {
|
1316 |
+
$row[0] = '0-10';
|
1317 |
+
}
|
1318 |
+
elseif ($row[0] >= 11 && $row[0] <= 30) {
|
1319 |
+
$row[0] = '11-30';
|
1320 |
+
}
|
1321 |
+
elseif ($row[0] >= 31 && $row[0] <= 40) {
|
1322 |
+
$row[0] = '31-40';
|
1323 |
+
}
|
1324 |
+
elseif ($row[0] >= 41 && $row[0] <= 60) {
|
1325 |
+
$row[0] = '41-60';
|
1326 |
+
}
|
1327 |
+
elseif ($row[0] >= 61 && $row[0] <= 180) {
|
1328 |
+
$row[0] = '61-180';
|
1329 |
+
}
|
1330 |
+
elseif ($row[0] >= 181 && $row[0] <= 600) {
|
1331 |
+
$row[0] = '181-600';
|
1332 |
+
}
|
1333 |
+
elseif ($row[0] >= 601 && $row[0] <= 1800) {
|
1334 |
+
$row[0] = '601-1800';
|
1335 |
+
}
|
1336 |
+
elseif ($row[0] >= 1801) {
|
1337 |
+
$row[0] = '1801';
|
1338 |
+
}
|
1339 |
+
}
|
1340 |
+
elseif (strpos($dimension, 'dimension') > -1) {
|
1341 |
+
$dimension_data = $this->get_custom_dimensions();
|
1342 |
+
foreach ($dimension_data as $key => $value) {
|
1343 |
+
if ($dimension == substr($value['id'], 3)) {
|
1344 |
+
$dimension = $value['name'];
|
1345 |
}
|
1346 |
+
}
|
1347 |
}
|
1348 |
+
$data[$j]['No'] = floatval($j + 1);
|
1349 |
+
$dimension_data = ctype_digit($row[0]) ? intval($row[0]) : $row[0];
|
1350 |
+
$dimension_data = strpos($dimension_data, 'T') ? substr($dimension_data, 0, strpos($dimension_data, 'T')) : $dimension_data;
|
1351 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))] = $dimension_data;
|
1352 |
+
|
1353 |
for ($i = 0; $i < count($metric); $i++) {
|
1354 |
+
$metric_val = floatval($row[$i + 1]);
|
1355 |
+
if (substr($metric[$i], 3) == 'avgSessionDuration') {
|
1356 |
+
$metric_val = ceil($row[$i + 1]);
|
1357 |
+
}
|
1358 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
1359 |
}
|
1360 |
+
$j++;
|
1361 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1362 |
}
|
1363 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1364 |
+
if (isset($same_dimension) && $same_dimension != null) {
|
1365 |
+
$dimension = $filter_type == 'date' || $filter_type == '' || $filter_type == 'Date' ? $same_dimension : $same_dimension . '_' . $filter_type;
|
|
|
|
|
|
|
|
|
1366 |
}
|
1367 |
+
if ($dimension == "daysToTransaction") {
|
1368 |
+
foreach ($data as $key => $row) {
|
1369 |
+
$daysToTransaction[$key] = $row['Days To Transaction'];
|
1370 |
+
}
|
1371 |
+
array_multisort($daysToTransaction, SORT_ASC, $data);
|
1372 |
+
foreach ($data as $j => $val) {
|
1373 |
+
$val["No"] = ($j + 1);
|
1374 |
+
$data[$j] = $val;
|
1375 |
+
}
|
1376 |
}
|
1377 |
+
elseif ($dimension == "sessionDurationBucket") {
|
1378 |
+
$_data = array();
|
1379 |
+
//$j = 1;
|
1380 |
+
foreach ($data as $val) {
|
1381 |
+
if (isset($_data[$val["Session Duration Bucket"]])) {
|
1382 |
+
$_data[$val["Session Duration Bucket"]]["Users"] += floatval($val["Users"]);
|
1383 |
+
$_data[$val["Session Duration Bucket"]]["Sessions"] += floatval($val["Sessions"]);
|
1384 |
+
$_data[$val["Session Duration Bucket"]]["Percent New Sessions"] += floatval($val["Percent New Sessions"]);
|
1385 |
+
$_data[$val["Session Duration Bucket"]]["Bounce Rate"] += floatval($val["Bounce Rate"]);
|
1386 |
+
$_data[$val["Session Duration Bucket"]]["Pageviews"] += floatval($val["Pageviews"]);
|
1387 |
+
$_data[$val["Session Duration Bucket"]]["Avg Session Duration"] += $val["Avg Session Duration"];
|
1388 |
+
}
|
1389 |
+
else {
|
1390 |
+
// $val["No"] = $j;
|
1391 |
+
// $j++;
|
1392 |
+
$_data[$val["Session Duration Bucket"]] = $val;
|
1393 |
+
$_data[$val["Session Duration Bucket"]]["order"] = intval($val["Session Duration Bucket"]);
|
1394 |
}
|
1395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1396 |
|
1397 |
+
}
|
1398 |
+
$data = array_values($_data);
|
1399 |
+
foreach ($data as $key => $row) {
|
1400 |
+
$yyy[$key] = $row['order'];
|
1401 |
+
}
|
1402 |
+
array_multisort($yyy, SORT_ASC, $data);
|
1403 |
+
foreach ($data as $j => $val) {
|
1404 |
+
$val["No"] = ($j + 1);
|
1405 |
+
$data[$j] = $val;
|
1406 |
+
}
|
|
|
|
|
|
|
|
|
1407 |
}
|
1408 |
else {
|
1409 |
+
if (strpos($dimension, 'dimension') === false) {
|
1410 |
+
$dimension = $dimension == 'siteSpeed' || $dimension == 'sales_performance' ? 'Date' : $dimension;
|
1411 |
+
foreach ($data as $key => $row) {
|
1412 |
+
$new_data[$key] = $row[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))];
|
|
|
|
|
|
|
|
|
|
|
1413 |
}
|
1414 |
+
array_multisort($new_data, SORT_ASC, $data);
|
1415 |
+
foreach ($data as $j => $val) {
|
1416 |
+
$val["No"] = ($j + 1);
|
1417 |
+
$data[$j] = $val;
|
1418 |
+
}
|
1419 |
+
}
|
1420 |
}
|
1421 |
+
$result = $data;
|
1422 |
if ($data_sum != '') {
|
1423 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1424 |
}
|
1425 |
+
set_transient('gawd-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
|
|
1426 |
return json_encode($result);
|
1427 |
+
}
|
1428 |
+
else {
|
1429 |
+
if (strpos($dimension, 'dimension') > -1) {
|
1430 |
+
$dimension_data = $this->get_custom_dimensions();
|
1431 |
+
foreach ($dimension_data as $key => $value) {
|
1432 |
+
if ($dimension == substr($value['id'], 3)) {
|
1433 |
+
$dimension = $value['name'];
|
1434 |
+
}
|
1435 |
+
}
|
1436 |
+
}
|
1437 |
+
$empty[0] = array(
|
1438 |
+
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => 0
|
1439 |
+
);
|
1440 |
+
$empty[0]['No'] = 1;
|
1441 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1442 |
+
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1443 |
+
}
|
1444 |
+
|
1445 |
+
return json_encode(array('chart_data' => $empty));
|
1446 |
+
}
|
1447 |
+
}
|
1448 |
+
}
|
1449 |
+
|
1450 |
+
public function get_data_compact($metric, $dimension, $start_date, $end_date, $timezone)
|
1451 |
+
{
|
1452 |
+
$profileId = $this->get_profile_id();
|
1453 |
+
$metric_sort = $metric;
|
1454 |
+
$analytics = $this->analytics_member;
|
1455 |
+
// Get the results from the Core Reporting API and print the results.
|
1456 |
+
// Calls the Core Reporting API and queries for the number of sessions
|
1457 |
+
// for the last seven days.
|
1458 |
+
if ($dimension == 'date') {
|
1459 |
+
$metric = 'ga:users,ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviews,ga:avgSessionDuration,ga:pageviewsPerSession';
|
1460 |
}
|
1461 |
+
try {
|
1462 |
+
$results = $analytics->data_ga->get(
|
1463 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1464 |
+
'dimensions' => 'ga:' . $dimension,
|
1465 |
+
'sort' => 'ga:' . $dimension,
|
1466 |
+
)
|
1467 |
+
);
|
1468 |
+
} catch (Exception $e) {
|
1469 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
1470 |
+
$fh = fopen($myFile, 'a');
|
1471 |
+
fwrite($fh, $e->getMessage() . "----get_data_compact function" . PHP_EOL);
|
1472 |
+
fclose($fh);
|
1473 |
+
}
|
1474 |
+
$rows = $results->getRows();
|
1475 |
+
$metric = explode(',', $metric);
|
1476 |
+
if ($rows) {
|
1477 |
+
$j = 0;
|
1478 |
+
$data_sum = array();
|
1479 |
+
foreach ($results->getTotalsForAllResults() as $key => $value) {
|
1480 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($key, 3))))] = $value;
|
1481 |
+
}
|
1482 |
|
1483 |
+
foreach ($rows as $row) {
|
1484 |
+
if ($dimension == 'date') {
|
1485 |
+
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1486 |
+
}
|
1487 |
+
$data[$j] = array(
|
1488 |
+
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => $row[0]
|
|
|
|
|
|
|
|
|
|
|
1489 |
);
|
1490 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1491 |
+
$metric_val = floatval($row[$i + 1]);
|
1492 |
|
1493 |
+
$data[$j][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = $metric_val;
|
|
|
|
|
1494 |
}
|
1495 |
+
$j++;
|
1496 |
+
}
|
1497 |
+
if ($dimension == "country") {
|
1498 |
+
foreach ($data as $key => $row) {
|
1499 |
+
$country[$key] = $row[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric_sort, 3))))];
|
1500 |
+
}
|
1501 |
+
array_multisort($country, SORT_DESC, $data);
|
1502 |
+
foreach ($data as $j => $val) {
|
1503 |
+
$val["No"] = ($j + 1);
|
1504 |
+
$data[$j] = $val;
|
|
|
|
|
1505 |
}
|
1506 |
+
}
|
1507 |
}
|
1508 |
+
else {
|
1509 |
+
$data_sum = array();
|
1510 |
+
$empty[0] = array(
|
1511 |
+
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => 0
|
1512 |
+
);
|
1513 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))] = 0;
|
1514 |
+
$empty[0]['No'] = 1;
|
1515 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1516 |
+
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1517 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1518 |
+
}
|
1519 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $empty);
|
1520 |
+
return json_encode($result);
|
1521 |
}
|
1522 |
+
if ($data_sum != '') {
|
1523 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
|
|
|
|
1524 |
}
|
1525 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1526 |
+
set_transient('gawd-compact-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1527 |
+
return json_encode($result);
|
1528 |
+
}
|
1529 |
+
|
1530 |
+
public function get_data_alert($metric, $dimension, $start_date, $end_date, $gawd_alert_view)
|
1531 |
+
{
|
1532 |
+
$profileId = $gawd_alert_view == '' ? $this->get_profile_id() : $gawd_alert_view;
|
1533 |
+
$analytics = $this->analytics_member;
|
1534 |
+
// Get the results from the Core Reporting API and print the results.
|
1535 |
+
// Calls the Core Reporting API and queries for the number of sessions
|
1536 |
+
// for the last seven days.
|
1537 |
+
$results = $analytics->data_ga->get(
|
1538 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1539 |
+
'dimensions' => 'ga:' . $dimension,
|
1540 |
+
'sort' => 'ga:' . $dimension,
|
1541 |
+
)
|
1542 |
+
);
|
1543 |
+
$rows = $results->getRows();
|
1544 |
+
|
1545 |
+
$data = '';
|
1546 |
+
foreach ($rows as $row) {
|
1547 |
+
$data += floatval($row[1]);
|
1548 |
}
|
1549 |
+
return ($data);
|
1550 |
+
}
|
1551 |
+
|
1552 |
+
public function get_profile_id()
|
1553 |
+
{
|
1554 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
1555 |
+
$profiles_light = get_transient('gawd_user_profiles') ? get_transient('gawd_user_profiles') : (isset($this->gawd_user_data['gawd_profiles']) ? $this->gawd_user_data['gawd_profiles'] : array());
|
1556 |
+
if (!isset($this->gawd_user_data['gawd_id']) || $this->gawd_user_data['gawd_id'] == '') {
|
1557 |
+
if (!empty($profiles_light)) {
|
1558 |
+
$first_profiles = reset($profiles_light);
|
1559 |
+
$first_profile = $first_profiles[0];
|
1560 |
+
$this->gawd_user_data['gawd_id'] = $first_profile['id'];
|
1561 |
+
}
|
1562 |
+
}
|
1563 |
+
return (isset($this->gawd_user_data['gawd_id']) ? $this->gawd_user_data['gawd_id'] : null);
|
1564 |
+
}
|
1565 |
+
|
1566 |
+
public function get_profile_webPropertyId()
|
1567 |
+
{
|
1568 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
1569 |
+
return (isset($this->gawd_user_data['webPropertyId']) ? $this->gawd_user_data['webPropertyId'] : null);
|
1570 |
+
}
|
1571 |
+
|
1572 |
+
public function get_profile_accountId()
|
1573 |
+
{
|
1574 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
1575 |
+
return isset($this->gawd_user_data['accountId']) ? $this->gawd_user_data['accountId'] : '';
|
1576 |
+
}
|
1577 |
+
|
1578 |
+
public function get_default_webPropertyId()
|
1579 |
+
{
|
1580 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
1581 |
+
return (isset($this->gawd_user_data['default_webPropertyId']) ? $this->gawd_user_data['default_webPropertyId'] : null);
|
1582 |
+
}
|
1583 |
+
|
1584 |
+
public function get_default_accountId()
|
1585 |
+
{
|
1586 |
+
$this->gawd_user_data = get_option('gawd_user_data');
|
1587 |
+
return (isset($this->gawd_user_data['default_accountId']) ? $this->gawd_user_data['default_accountId'] : null);
|
1588 |
+
}
|
1589 |
+
|
1590 |
+
public function get_page_data($dimension, $start_date, $end_date, $timezone)
|
1591 |
+
{
|
1592 |
+
$analytics = $this->analytics_member;
|
1593 |
+
$profileId = $this->get_profile_id();
|
1594 |
+
$metric = $dimension == 'pagePath' || $dimension == 'PagePath' ? 'ga:pageviews,ga:uniquePageviews,ga:avgTimeOnPage,ga:entrances,ga:bounceRate,ga:exitRate,ga:pageValue,ga:avgPageLoadTime' : 'ga:sessions,ga:percentNewSessions,ga:newUsers,ga:bounceRate,ga:pageviewsPerSession,ga:avgSessionDuration,ga:transactions,ga:transactionRevenue,ga:transactionsPerSession';
|
1595 |
+
$sorts = explode(',', $metric);
|
1596 |
+
$sort = '-' . $sorts[0];
|
1597 |
+
|
1598 |
+
try {
|
1599 |
+
$results = $analytics->data_ga->get(
|
1600 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1601 |
+
'dimensions' => 'ga:' . $dimension,
|
1602 |
+
'sort' => $sort,
|
1603 |
+
)
|
1604 |
+
);
|
1605 |
+
} catch (Exception $e) {
|
1606 |
+
|
1607 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
1608 |
+
$fh = fopen($myFile, 'a');
|
1609 |
+
fwrite($fh, $e->getMessage() . "----get_page_data function" . PHP_EOL);
|
1610 |
+
fclose($fh);
|
1611 |
+
|
1612 |
+
$error = array('error_message' => 'Error');
|
1613 |
+
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
1614 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
1615 |
+
}
|
1616 |
+
return json_encode($error);
|
1617 |
}
|
1618 |
+
$rows = $results->getRows();
|
1619 |
+
$metric = explode(',', $metric);
|
1620 |
+
if ($rows) {
|
1621 |
+
$data_sum = array();
|
1622 |
+
foreach ($results->getTotalsForAllResults() as $key => $value) {
|
1623 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($key, 3))))] = $value;
|
1624 |
+
}
|
1625 |
+
foreach ($rows as $key => $row) {
|
1626 |
+
$hours = strlen(floor($row[3] / 3600)) < 2 ? '0' . floor($row[3] / 3600) : floor($row[3] / 3600);
|
1627 |
+
$mins = strlen(floor($row[3] / 60 % 60)) < 2 ? '0' . floor($row[3] / 60 % 60) : floor($row[3] / 60 % 60);
|
1628 |
+
$secs = strlen(floor($row[3] % 60)) < 2 ? '0' . floor($row[3] % 60) : floor($row[3] % 60);
|
1629 |
+
$time_on_page = $hours . ':' . $mins . ':' . $secs;
|
1630 |
+
if ($dimension == 'pagePath' || $dimension == 'PagePath') {
|
1631 |
+
$data[] = array(
|
1632 |
+
'No' => floatval($key + 1),
|
1633 |
+
'Page Path' => $row[0],
|
1634 |
+
'Pageviews' => intval($row[1]),
|
1635 |
+
'Unique Pageviews' => intval($row[2]),
|
1636 |
+
'Avg Time On Page' => $time_on_page,
|
1637 |
+
'Entrances' => intval($row[4]),
|
1638 |
+
'Bounce Rate' => floatval($row[5]),
|
1639 |
+
'Exit Rate' => ($row[6]),
|
1640 |
+
'Page Value' => intval($row[7]),
|
1641 |
+
'Avg Page Load Time' => intval($row[8])
|
1642 |
+
);
|
1643 |
+
}
|
1644 |
+
else {
|
1645 |
+
$data[] = array(
|
1646 |
+
'No' => floatval($key + 1),
|
1647 |
+
'Landing Page' => $row[0],
|
1648 |
+
'Sessions' => intval($row[1]),
|
1649 |
+
'Percent New Sessions' => ($row[2]),
|
1650 |
+
'New Users' => intval($row[3]),
|
1651 |
+
'Bounce Rate' => ($row[4]),
|
1652 |
+
'Pageviews Per Session' => floatval($row[5]),
|
1653 |
+
'Avg Session Duration' => intval($row[6]),
|
1654 |
+
'Transactions' => intval($row[7]),
|
1655 |
+
'Transaction Revenue' => intval($row[8]),
|
1656 |
+
'Transactions Per Session' => intval($row[9])
|
1657 |
+
);
|
1658 |
+
}
|
1659 |
+
}
|
1660 |
|
1661 |
+
}
|
1662 |
+
else {
|
1663 |
+
$empty[0] = array(
|
1664 |
+
trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension))) => 0
|
1665 |
+
);
|
1666 |
+
$empty[0]['No'] = 1;
|
1667 |
+
for ($i = 0; $i < count($metric); $i++) {
|
1668 |
+
$empty[0][trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1669 |
+
$data_sum[trim(ucfirst(preg_replace('/([A-Z])/', ' $1', substr($metric[$i], 3))))] = 0;
|
1670 |
+
}
|
1671 |
|
1672 |
+
return json_encode(array('data_sum' => $data_sum, 'chart_data' => $empty));
|
1673 |
+
}
|
1674 |
+
if ($data_sum != '') {
|
1675 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1676 |
+
}
|
1677 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1678 |
+
set_transient('gawd-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1679 |
+
return json_encode($result);
|
1680 |
+
}
|
1681 |
+
|
1682 |
+
public function get_goal_data($dimension, $start_date, $end_date, $timezone, $same_dimension)
|
1683 |
+
{
|
1684 |
+
$goals = $this->get_management_goals();
|
1685 |
+
if ('no_goals_exist' != $goals) {
|
1686 |
+
$analytics = $this->analytics_member;
|
1687 |
+
$profileId = $this->get_profile_id();
|
1688 |
+
$metric = array();
|
1689 |
+
$all_metric = '';
|
1690 |
+
$counter = 1;
|
1691 |
+
foreach ($goals as $goal) {
|
1692 |
+
$all_metric .= 'ga:goal' . $goal['id'] . 'Completions,';
|
1693 |
+
if ($counter <= 10) {
|
1694 |
+
$metrics[0][] = 'ga:goal' . $goal['id'] . 'Completions';
|
1695 |
+
}
|
1696 |
+
else {
|
1697 |
+
$metrics[1][] = 'ga:goal' . $goal['id'] . 'Completions';
|
1698 |
+
}
|
1699 |
+
$counter++;
|
1700 |
+
}
|
1701 |
+
$rows = array();
|
1702 |
+
foreach ($metrics as $metric) {
|
1703 |
+
$metric = implode(',', $metric);
|
1704 |
+
$results = $analytics->data_ga->get(
|
1705 |
+
'ga:' . $profileId, $start_date, $end_date, $metric, array(
|
1706 |
+
'dimensions' => 'ga:' . $dimension,
|
1707 |
+
'sort' => 'ga:' . $dimension,
|
1708 |
+
)
|
1709 |
+
);
|
1710 |
|
1711 |
+
$temp_rows = $results->getRows();
|
1712 |
+
if (empty($temp_rows)) {
|
1713 |
+
continue;
|
1714 |
+
}
|
1715 |
|
1716 |
+
foreach ($temp_rows as $key => $value) {
|
1717 |
+
if (!isset($rows[$key])) {
|
1718 |
+
$rows[$key] = $value;
|
1719 |
+
}
|
1720 |
+
else {
|
1721 |
+
unset($value[0]);
|
1722 |
+
$rows[$key] = array_merge($rows[$key], $value);
|
1723 |
+
}
|
1724 |
}
|
1725 |
+
|
1726 |
+
}
|
1727 |
+
$all_metric = explode(',', $all_metric);
|
1728 |
+
if ($rows) {
|
1729 |
+
$j = 0;
|
1730 |
+
$data_sum = array();
|
1731 |
+
foreach ($rows as $row) {
|
1732 |
+
if ($dimension == 'date') {
|
1733 |
+
$row[0] = date('Y-m-d', strtotime($row[0]));
|
1734 |
+
}
|
1735 |
+
$data[$j] = array(
|
1736 |
+
preg_replace('!\s+!', ' ', trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $dimension)))) => $row[0]
|
1737 |
+
);
|
1738 |
+
$data[$j]['No'] = floatval($j + 1);
|
1739 |
+
for ($i = 0; $i < count($goals); $i++) {
|
1740 |
+
$data[$j][preg_replace('!\s+!', ' ', trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] = floatval($row[$i + 1]);
|
1741 |
+
if (isset($data_sum[preg_replace('!\s+!', ' ', trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))])) {
|
1742 |
+
$data_sum[preg_replace('!\s+!', ' ', trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] += floatval($row[$i + 1]);
|
1743 |
}
|
1744 |
+
else {
|
1745 |
+
if (substr($all_metric[$i], 3) != 'percentNewSessions' && substr($all_metric[$i], 3) != 'bounceRate') {
|
1746 |
+
$data_sum[preg_replace('!\s+!', ' ', trim(ucfirst(preg_replace('/([A-Z])/', ' $1', $goals[$i]['name']))))] = floatval($row[$i + 1]);
|
1747 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1748 |
}
|
1749 |
+
}
|
1750 |
+
$j++;
|
1751 |
}
|
1752 |
+
$expiration = strtotime(date("Y-m-d 23:59:59")) - strtotime(gmdate("Y-m-d H:i:s") . '+' . $timezone . ' hours');
|
1753 |
+
if (isset($same_dimension) && $same_dimension != null) {
|
1754 |
+
$dimension = $same_dimension;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1755 |
}
|
1756 |
+
$result = $data;
|
1757 |
if ($data_sum != '') {
|
1758 |
+
$result = array('data_sum' => $data_sum, 'chart_data' => $data);
|
1759 |
}
|
|
|
1760 |
set_transient('gawd-' . $profileId . '-' . $dimension . '-' . $start_date . '-' . $end_date, json_encode($result), $expiration);
|
1761 |
return json_encode($result);
|
1762 |
+
}
|
1763 |
+
else {
|
1764 |
+
return $goals;
|
1765 |
+
}
|
1766 |
}
|
1767 |
+
else {
|
1768 |
+
return json_encode(array('error_message' => 'No goals exist'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1769 |
}
|
1770 |
+
}
|
1771 |
+
|
1772 |
+
public function gawd_realtime_data()
|
1773 |
+
{
|
1774 |
+
$analytics = $this->analytics_member;
|
1775 |
+
$profileId = $this->get_profile_id();
|
1776 |
+
$metrics = 'rt:activeUsers';
|
1777 |
+
$dimensions = 'rt:pagePath,rt:source,rt:keyword,rt:trafficType,rt:country,rt:pageTitle,rt:deviceCategory';
|
1778 |
+
$managequota = 'u' . get_current_user_id() . 's' . get_current_blog_id();
|
1779 |
+
|
1780 |
+
try {
|
1781 |
+
$data = $analytics->data_realtime->get('ga:' . $profileId, $metrics, array('dimensions' => $dimensions, 'quotaUser' => $managequota . 'p' . $profileId));
|
1782 |
+
} catch (Exception $e) {
|
1783 |
+
|
1784 |
+
$myFile = GAWD_UPLOAD_DIR . "/logfile.txt";;
|
1785 |
+
$fh = fopen($myFile, 'a');
|
1786 |
+
fwrite($fh, $e->getMessage() . "----gawd_realtime_data function" . PHP_EOL);
|
1787 |
+
fclose($fh);
|
1788 |
+
|
1789 |
+
$error = array('error_message' => 'Error');
|
1790 |
+
if (strpos($e->getMessage(), 'User does not have sufficient permissions for this profile')) {
|
1791 |
+
$error['error_message'] = 'User does not have sufficient permissions for this profile';
|
1792 |
+
}
|
1793 |
+
return json_encode($error);
|
1794 |
+
}
|
1795 |
+
$expiration = 5;
|
1796 |
+
if ($data->getRows() != '') {
|
1797 |
+
$i = 0;
|
1798 |
+
$gawd_data = $data;
|
1799 |
+
foreach ($data->getRows() as $row) {
|
1800 |
+
$gawd_data[$i] = $row;
|
1801 |
+
$i++;
|
1802 |
+
}
|
1803 |
+
set_transient('gawd-real-' . $profileId, json_encode($gawd_data), $expiration);
|
1804 |
+
echo json_encode($gawd_data);
|
1805 |
+
wp_die();
|
1806 |
+
}
|
1807 |
+
else {
|
1808 |
+
return 0;
|
1809 |
+
}
|
1810 |
+
}
|
1811 |
|
1812 |
+
public static function get_instance($force_new_instance = false)
|
1813 |
+
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1814 |
|
1815 |
+
if (null === self::$instance && !$force_new_instance) {
|
1816 |
+
self::$instance = new self();
|
1817 |
+
}
|
1818 |
+
else if ($force_new_instance) {
|
1819 |
+
self::$instance = new self();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1820 |
}
|
1821 |
|
1822 |
+
return self::$instance;
|
1823 |
+
}
|
1824 |
|
1825 |
+
public static function has_error()
|
1826 |
+
{
|
|
|
|
|
|
|
|
|
1827 |
|
1828 |
+
return self::$error_message == '' ? false : true;
|
1829 |
+
}
|
1830 |
|
1831 |
+
public static function get_error_notice()
|
1832 |
+
{
|
1833 |
+
?>
|
1834 |
+
<div class="notice notice-error">
|
1835 |
+
<p>Google Analytics WD: <?php echo self::$error_message; ?></p>
|
1836 |
+
</div>
|
1837 |
+
<?php
|
1838 |
+
}
|
|
|
|
|
|
|
1839 |
|
1840 |
}
|
assets/logo.png
ADDED
Binary file
|
gawd_class.php
CHANGED
@@ -49,7 +49,8 @@ class GAWD
|
|
49 |
add_action('load-post.php', array($this, 'gawd_add_custom_box'));
|
50 |
}
|
51 |
$gawd_frontend_roles = isset($gawd_settings['gawd_frontend_roles']) ? $gawd_settings['gawd_frontend_roles'] : array();
|
52 |
-
|
|
|
53 |
add_action('wp_enqueue_scripts', array($this, 'gawd_front_scripts'));
|
54 |
add_action('admin_bar_menu', array($this, 'report_adminbar'), 999);
|
55 |
}
|
@@ -583,6 +584,16 @@ class GAWD
|
|
583 |
$gawd_settings['show_report_page'] = 'on';
|
584 |
update_option('gawd_settings', $gawd_settings);
|
585 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
586 |
self::add_dashboard_menu();
|
587 |
}
|
588 |
|
@@ -631,7 +642,10 @@ class GAWD
|
|
631 |
$default_date_format = (isset($options['default_date_format']) && $options['default_date_format'] != '') ? $options['default_date_format'] : 'ymd_with_week';
|
632 |
$enable_hover_tooltip = (isset($options['enable_hover_tooltip']) && $options['enable_hover_tooltip'] != '') ? $options['enable_hover_tooltip'] : '';
|
633 |
$screen = get_current_screen();
|
634 |
-
|
|
|
|
|
|
|
635 |
wp_enqueue_script('common');
|
636 |
/* wp_enqueue_script('wp-lists'); */
|
637 |
wp_enqueue_script('postbox');
|
@@ -948,9 +962,11 @@ class GAWD
|
|
948 |
delete_option('gawd_user_status');
|
949 |
delete_option('gawd_account_status');
|
950 |
delete_option('gawd_management_accounts');
|
|
|
951 |
delete_transient('gawd_user_profiles');
|
952 |
|
953 |
|
|
|
954 |
// echo '<script>window.location.href="' . $deactivate_url . '";</script>';
|
955 |
}
|
956 |
if (get_option('gawd_credentials')) {
|
@@ -1410,6 +1426,8 @@ class GAWD
|
|
1410 |
}
|
1411 |
if (isset($_POST['gawd_settings_logout']) && $_POST['gawd_settings_logout'] == 1) {
|
1412 |
delete_option('gawd_user_data');
|
|
|
|
|
1413 |
$redirect_url = admin_url() . 'admin.php?page=gawd_settings';
|
1414 |
echo '<script>window.location.href="' . $redirect_url . '";</script>';
|
1415 |
}
|
49 |
add_action('load-post.php', array($this, 'gawd_add_custom_box'));
|
50 |
}
|
51 |
$gawd_frontend_roles = isset($gawd_settings['gawd_frontend_roles']) ? $gawd_settings['gawd_frontend_roles'] : array();
|
52 |
+
$gawd_user_status = get_option('gawd_user_status');
|
53 |
+
if ((isset($gawd_settings['gawd_tracking_enable']) && $gawd_settings['gawd_tracking_enable'] == 'on') && (in_array($roles, $gawd_frontend_roles) || current_user_can('manage_options')) && ( isset( $gawd_user_data['refresh_token'] ) && ( $gawd_user_data['refresh_token'] != '' ) ) && isset($gawd_user_data['default_webPropertyId']) && $gawd_user_status == '1') {
|
54 |
add_action('wp_enqueue_scripts', array($this, 'gawd_front_scripts'));
|
55 |
add_action('admin_bar_menu', array($this, 'report_adminbar'), 999);
|
56 |
}
|
584 |
$gawd_settings['show_report_page'] = 'on';
|
585 |
update_option('gawd_settings', $gawd_settings);
|
586 |
}
|
587 |
+
|
588 |
+
//logout issue
|
589 |
+
if(GAWD_VERSION == '1.1.2' || GAWD_VERSION == '5.1.2'){
|
590 |
+
|
591 |
+
if(get_option('gawd_user_data') == false){
|
592 |
+
delete_option('gawd_user_status');
|
593 |
+
delete_option('gawd_account_status');
|
594 |
+
}
|
595 |
+
|
596 |
+
}
|
597 |
self::add_dashboard_menu();
|
598 |
}
|
599 |
|
642 |
$default_date_format = (isset($options['default_date_format']) && $options['default_date_format'] != '') ? $options['default_date_format'] : 'ymd_with_week';
|
643 |
$enable_hover_tooltip = (isset($options['enable_hover_tooltip']) && $options['enable_hover_tooltip'] != '') ? $options['enable_hover_tooltip'] : '';
|
644 |
$screen = get_current_screen();
|
645 |
+
|
646 |
+
$gawd_user_status = get_option('gawd_user_status');
|
647 |
+
$include_scripts_in_wp_admin_pages = ((strpos($screen->post_type, 'page') !== false || strpos($screen->post_type, 'post') !== false || strpos($screen->base, 'dashboard') !== false || strpos($screen->base, 'edit') !== false) && $gawd_user_status == '1');
|
648 |
+
if (strpos($screen->base, 'gawd') !== false || $include_scripts_in_wp_admin_pages) {
|
649 |
wp_enqueue_script('common');
|
650 |
/* wp_enqueue_script('wp-lists'); */
|
651 |
wp_enqueue_script('postbox');
|
962 |
delete_option('gawd_user_status');
|
963 |
delete_option('gawd_account_status');
|
964 |
delete_option('gawd_management_accounts');
|
965 |
+
delete_option('gawd_bk_notice_status');
|
966 |
delete_transient('gawd_user_profiles');
|
967 |
|
968 |
|
969 |
+
|
970 |
// echo '<script>window.location.href="' . $deactivate_url . '";</script>';
|
971 |
}
|
972 |
if (get_option('gawd_credentials')) {
|
1426 |
}
|
1427 |
if (isset($_POST['gawd_settings_logout']) && $_POST['gawd_settings_logout'] == 1) {
|
1428 |
delete_option('gawd_user_data');
|
1429 |
+
delete_option('gawd_user_status');
|
1430 |
+
delete_option('gawd_account_status');
|
1431 |
$redirect_url = admin_url() . 'admin.php?page=gawd_settings';
|
1432 |
echo '<script>window.location.href="' . $redirect_url . '";</script>';
|
1433 |
}
|
google-analytics-wd.php
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
* Plugin Name: Google Analytics
|
5 |
* Plugin URI: https://web-dorado.com/products/wordpress-google-analytics-plugin.html
|
6 |
* Description: Google Analytics WD is a user-friendly all in one plugin, which allows to manage and monitor your website analytics from WordPress dashboard.
|
7 |
-
* Version: 1.1.
|
8 |
* Author: WebDorado
|
9 |
* Author URI: https://web-dorado.com
|
10 |
* License: GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
|
@@ -26,7 +26,7 @@ if (!defined('GAWD_INC')) {
|
|
26 |
}
|
27 |
|
28 |
if (!defined('GAWD_VERSION')) {
|
29 |
-
define('GAWD_VERSION', '1.1.
|
30 |
}
|
31 |
|
32 |
|
@@ -229,4 +229,61 @@ function gawd_php_version_admin_notice() {
|
|
229 |
|
230 |
</div>
|
231 |
<?php
|
|
|
|
|
232 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
* Plugin Name: Google Analytics
|
5 |
* Plugin URI: https://web-dorado.com/products/wordpress-google-analytics-plugin.html
|
6 |
* Description: Google Analytics WD is a user-friendly all in one plugin, which allows to manage and monitor your website analytics from WordPress dashboard.
|
7 |
+
* Version: 1.1.2
|
8 |
* Author: WebDorado
|
9 |
* Author URI: https://web-dorado.com
|
10 |
* License: GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
|
26 |
}
|
27 |
|
28 |
if (!defined('GAWD_VERSION')) {
|
29 |
+
define('GAWD_VERSION', '1.1.2');
|
30 |
}
|
31 |
|
32 |
|
229 |
|
230 |
</div>
|
231 |
<?php
|
232 |
+
|
233 |
+
|
234 |
}
|
235 |
+
if (!function_exists('wd_bp_install_notice')) {
|
236 |
+
$wd_bp_plugin_url = GAWD_URL;
|
237 |
+
function wd_bp_script_style() {
|
238 |
+
global $wd_bp_plugin_url;
|
239 |
+
wp_enqueue_script('wd_bck_install', $wd_bp_plugin_url . '/inc/js/wd_bp_install.js', array('jquery'));
|
240 |
+
wp_enqueue_style('wd_bck_install', $wd_bp_plugin_url . '/inc/css/wd_bp_install.css');
|
241 |
+
}
|
242 |
+
add_action('admin_enqueue_scripts', 'wd_bp_script_style');
|
243 |
+
|
244 |
+
/**
|
245 |
+
* Show notice to install backup plugin
|
246 |
+
*/
|
247 |
+
function wd_bp_install_notice() {
|
248 |
+
global $wd_bp_plugin_url;
|
249 |
+
$prefix = 'gawd';
|
250 |
+
$meta_value = get_option('gawd_bk_notice_status');
|
251 |
+
if ($meta_value === '' || $meta_value === false) {
|
252 |
+
ob_start();
|
253 |
+
?>
|
254 |
+
<div class="notice notice-info" id="wd_bp_notice_cont">
|
255 |
+
<p>
|
256 |
+
<img id="wd_bp_logo_notice" src="<?php echo $wd_bp_plugin_url . '/assets/logo.png'; ?>">
|
257 |
+
<?php _e("Hey! Install brand new FREE", $prefix) ?>
|
258 |
+
<a href="https://wordpress.org/plugins/backup-wd/" title="<?php _e("More details", $prefix) ?>"
|
259 |
+
target="_blank"><?php _e("Backup WD", $prefix) ?></a>
|
260 |
+
<?php _e("plugin to keep your images and website safe.", $prefix) ?>
|
261 |
+
<a class="button button-primary"
|
262 |
+
href="<?php echo esc_url(wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=backup-wd'), 'install-plugin_backup-wd')); ?>">
|
263 |
+
<span onclick="wd_bp_notice_install()"><?php _e("Install", $prefix); ?></span>
|
264 |
+
</a>
|
265 |
+
</p>
|
266 |
+
<button type="button" class="wd_bp_notice_dissmiss notice-dismiss"><span class="screen-reader-text"></span>
|
267 |
+
</button>
|
268 |
+
</div>
|
269 |
+
<script>wd_bp_url = '<?php echo add_query_arg(array('action' => 'wd_bp_dismiss',), admin_url('admin-ajax.php')); ?>'</script>
|
270 |
+
<?php
|
271 |
+
echo ob_get_clean();
|
272 |
+
}
|
273 |
+
}
|
274 |
+
|
275 |
+
if (!is_dir(plugin_dir_path(__DIR__) . 'backup-wd')) {
|
276 |
+
add_action('admin_notices', 'wd_bp_install_notice');
|
277 |
+
}
|
278 |
+
|
279 |
+
/**
|
280 |
+
* Add usermeta to db
|
281 |
+
*
|
282 |
+
* empty: notice,
|
283 |
+
* 1 : never show again
|
284 |
+
*/
|
285 |
+
function wd_bp_install_notice_status() {
|
286 |
+
update_option('gawd_bk_notice_status', '1', 'no');
|
287 |
+
}
|
288 |
+
add_action('wp_ajax_wd_bp_dismiss', 'wd_bp_install_notice_status');
|
289 |
+
}
|
google/vendor/autoload.php
CHANGED
@@ -4,4 +4,4 @@
|
|
4 |
|
5 |
require_once __DIR__ . '/composer/autoload_real.php';
|
6 |
|
7 |
-
return
|
4 |
|
5 |
require_once __DIR__ . '/composer/autoload_real.php';
|
6 |
|
7 |
+
return ComposerAutoloaderInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::getLoader();
|
google/vendor/composer/autoload_real.php
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
|
3 |
// autoload_real.php @generated by Composer
|
4 |
|
5 |
-
class
|
6 |
{
|
7 |
private static $loader;
|
8 |
|
@@ -19,9 +19,9 @@ class ComposerAutoloaderInit414fe7d6bd384b75fb7b7a29cfd20ec4
|
|
19 |
return self::$loader;
|
20 |
}
|
21 |
|
22 |
-
spl_autoload_register(array('
|
23 |
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
|
24 |
-
spl_autoload_unregister(array('
|
25 |
|
26 |
$includePaths = require __DIR__ . '/include_paths.php';
|
27 |
array_push($includePaths, get_include_path());
|
@@ -31,7 +31,7 @@ class ComposerAutoloaderInit414fe7d6bd384b75fb7b7a29cfd20ec4
|
|
31 |
if ($useStaticLoader) {
|
32 |
require_once __DIR__ . '/autoload_static.php';
|
33 |
|
34 |
-
call_user_func(\Composer\Autoload\
|
35 |
} else {
|
36 |
$map = require __DIR__ . '/autoload_namespaces.php';
|
37 |
foreach ($map as $namespace => $path) {
|
@@ -52,19 +52,19 @@ class ComposerAutoloaderInit414fe7d6bd384b75fb7b7a29cfd20ec4
|
|
52 |
$loader->register(true);
|
53 |
|
54 |
if ($useStaticLoader) {
|
55 |
-
$includeFiles = Composer\Autoload\
|
56 |
} else {
|
57 |
$includeFiles = require __DIR__ . '/autoload_files.php';
|
58 |
}
|
59 |
foreach ($includeFiles as $fileIdentifier => $file) {
|
60 |
-
|
61 |
}
|
62 |
|
63 |
return $loader;
|
64 |
}
|
65 |
}
|
66 |
|
67 |
-
function
|
68 |
{
|
69 |
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
70 |
require $file;
|
2 |
|
3 |
// autoload_real.php @generated by Composer
|
4 |
|
5 |
+
class ComposerAutoloaderInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4
|
6 |
{
|
7 |
private static $loader;
|
8 |
|
19 |
return self::$loader;
|
20 |
}
|
21 |
|
22 |
+
spl_autoload_register(array('ComposerAutoloaderInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4', 'loadClassLoader'), true, true);
|
23 |
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
|
24 |
+
spl_autoload_unregister(array('ComposerAutoloaderInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4', 'loadClassLoader'));
|
25 |
|
26 |
$includePaths = require __DIR__ . '/include_paths.php';
|
27 |
array_push($includePaths, get_include_path());
|
31 |
if ($useStaticLoader) {
|
32 |
require_once __DIR__ . '/autoload_static.php';
|
33 |
|
34 |
+
call_user_func(\Composer\Autoload\ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::getInitializer($loader));
|
35 |
} else {
|
36 |
$map = require __DIR__ . '/autoload_namespaces.php';
|
37 |
foreach ($map as $namespace => $path) {
|
52 |
$loader->register(true);
|
53 |
|
54 |
if ($useStaticLoader) {
|
55 |
+
$includeFiles = Composer\Autoload\ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::$files;
|
56 |
} else {
|
57 |
$includeFiles = require __DIR__ . '/autoload_files.php';
|
58 |
}
|
59 |
foreach ($includeFiles as $fileIdentifier => $file) {
|
60 |
+
composerRequireGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4($fileIdentifier, $file);
|
61 |
}
|
62 |
|
63 |
return $loader;
|
64 |
}
|
65 |
}
|
66 |
|
67 |
+
function composerRequireGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4($fileIdentifier, $file)
|
68 |
{
|
69 |
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
70 |
require $file;
|
google/vendor/composer/autoload_static.php
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
|
5 |
namespace Composer\Autoload;
|
6 |
|
7 |
-
class
|
8 |
{
|
9 |
public static $files = array (
|
10 |
'e7ef766f7bcc15ba09cd6308370a95d7' => __DIR__ . '/..' . '/react/promise/src/React/Promise/functions.php',
|
@@ -166,10 +166,10 @@ class ComposerStaticInit414fe7d6bd384b75fb7b7a29cfd20ec4
|
|
166 |
public static function getInitializer(ClassLoader $loader)
|
167 |
{
|
168 |
return \Closure::bind(function () use ($loader) {
|
169 |
-
$loader->prefixLengthsPsr4 =
|
170 |
-
$loader->prefixDirsPsr4 =
|
171 |
-
$loader->prefixesPsr0 =
|
172 |
-
$loader->classMap =
|
173 |
|
174 |
}, null, ClassLoader::class);
|
175 |
}
|
4 |
|
5 |
namespace Composer\Autoload;
|
6 |
|
7 |
+
class ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4
|
8 |
{
|
9 |
public static $files = array (
|
10 |
'e7ef766f7bcc15ba09cd6308370a95d7' => __DIR__ . '/..' . '/react/promise/src/React/Promise/functions.php',
|
166 |
public static function getInitializer(ClassLoader $loader)
|
167 |
{
|
168 |
return \Closure::bind(function () use ($loader) {
|
169 |
+
$loader->prefixLengthsPsr4 = ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::$prefixLengthsPsr4;
|
170 |
+
$loader->prefixDirsPsr4 = ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::$prefixDirsPsr4;
|
171 |
+
$loader->prefixesPsr0 = ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::$prefixesPsr0;
|
172 |
+
$loader->classMap = ComposerStaticInitGAWD_414fe7d6bd384b75fb7b7a29cfd20ec4::$classMap;
|
173 |
|
174 |
}, null, ClassLoader::class);
|
175 |
}
|
inc/css/wd_bp_install.css
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@media only screen and (max-width: 500px) {
|
2 |
+
body #wd_backup_logo {
|
3 |
+
max-width: 100%;
|
4 |
+
}
|
5 |
+
body #wd_bp_notice_cont p {
|
6 |
+
padding-right: 25px !important;
|
7 |
+
}
|
8 |
+
}
|
9 |
+
|
10 |
+
#wd_bp_logo_notice {
|
11 |
+
width: 40px;
|
12 |
+
float: left;
|
13 |
+
margin-right: 10px;
|
14 |
+
}
|
15 |
+
|
16 |
+
#wd_bp_notice_cont {
|
17 |
+
position: relative;
|
18 |
+
}
|
19 |
+
|
20 |
+
.wds_backup_install p {
|
21 |
+
padding-top: 0px;
|
22 |
+
margin-top: 0px;
|
23 |
+
}
|
24 |
+
|
25 |
+
#wd_bp_notice_cont a {
|
26 |
+
margin: 0 5px;
|
27 |
+
}
|
28 |
+
|
29 |
+
#wd_bp_notice_cont .dashicons-dismiss:before {
|
30 |
+
content: "\f153";
|
31 |
+
background: 0 0;
|
32 |
+
color: #72777c;
|
33 |
+
display: block;
|
34 |
+
font: 400 16px/20px dashicons;
|
35 |
+
speak: none;
|
36 |
+
height: 20px;
|
37 |
+
text-align: center;
|
38 |
+
width: 20px;
|
39 |
+
-webkit-font-smoothing: antialiased;
|
40 |
+
-moz-osx-font-smoothing: grayscale;
|
41 |
+
}
|
42 |
+
|
43 |
+
.wd_bp_notice_dissmiss {
|
44 |
+
margin-top: 5px;
|
45 |
+
}
|
46 |
+
|
inc/js/wd_bp_install.js
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
jQuery(document).ready(function () {
|
2 |
+
/*Ajax hide notice forever */
|
3 |
+
jQuery(".wd_bp_notice_dissmiss").on("click", function () {
|
4 |
+
jQuery("#wd_bp_notice_cont").hide();
|
5 |
+
jQuery.post(wd_bp_url);
|
6 |
+
})
|
7 |
+
});
|
8 |
+
|
9 |
+
// Set option status 1 - never show again during install btn click in notice
|
10 |
+
function wd_bp_notice_install() {
|
11 |
+
jQuery.post(wd_bp_url);
|
12 |
+
}
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ Donate link: https://web-dorado.com/products/wordpress-google-analytics-plugin.h
|
|
4 |
Tags: google analytics, statistics, tracking, analytics dashboard, ga, google analytics dashboard, analytics, stats, googleanalytics, ecommerce tracking, custom reports, custom dimensions
|
5 |
Requires at least: 3.9
|
6 |
Tested up to: 4.8
|
7 |
-
Stable tag: 1.1.
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
@@ -103,6 +103,12 @@ Upgrade to Google Analytics WD Pro for premium features:
|
|
103 |
|
104 |
== Changelog ==
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
= 1.1.1 =
|
107 |
* Fixed: Reports for pages/posts in metabox
|
108 |
* Fixed: Multiple requests to Web-Dorado API for updates (Pro)
|
4 |
Tags: google analytics, statistics, tracking, analytics dashboard, ga, google analytics dashboard, analytics, stats, googleanalytics, ecommerce tracking, custom reports, custom dimensions
|
5 |
Requires at least: 3.9
|
6 |
Tested up to: 4.8
|
7 |
+
Stable tag: 1.1.2
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
103 |
|
104 |
== Changelog ==
|
105 |
|
106 |
+
= 1.1.2 =
|
107 |
+
* Fixed: Conflict with some other plugins using Google API PHP library
|
108 |
+
* Fixed: Unable to authorize again after logout
|
109 |
+
* Fixed: JS errors on page/post editor if user not authenticated
|
110 |
+
* Change: Introducing Backup WD plugin
|
111 |
+
|
112 |
= 1.1.1 =
|
113 |
* Fixed: Reports for pages/posts in metabox
|
114 |
* Fixed: Multiple requests to Web-Dorado API for updates (Pro)
|