PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.1.4
ShareThis Dashboard for Google Analytics v3.1.4
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / class / class-ga-stats.php
googleanalytics / class Last commit date
controller 4 years ago core 4 years ago class-ga-admin.php 3 years ago class-ga-autoloader.php 4 years ago class-ga-frontend.php 3 years ago class-ga-helper.php 3 years ago class-ga-hook.php 4 years ago class-ga-notice.php 4 years ago class-ga-sharethis.php 4 years ago class-ga-stats.php 3 years ago class-ga-template.php 4 years ago
class-ga-stats.php
1089 lines
1 <?php
2 /**
3 * Google Analytics hook class.
4 *
5 * @package GoogleAnalytics
6 */
7
8 /**
9 * Ga_Stats class
10 *
11 * Preparing request and parsing response from Google Analytics Reporting Api
12 *
13 * @author wle@adips.com
14 * @version 1.0
15 */
16 class Ga_Stats {
17
18 /**
19 * Profile object.
20 *
21 * @var array Profile.
22 */
23 private $profile = array();
24
25 /**
26 * Primary class constructor.
27 *
28 * @access public
29 * @since 7.0.0
30 */
31 public function __construct() {
32 }
33
34 /**
35 * Preparing query to get Analytics data
36 *
37 * @param string $query Query type.
38 * @param int $id_view The Analytics view ID from which to retrieve data.
39 * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'.
40 * @param string $metric A metric expression.
41 * @param bool $old Use old query style.
42 *
43 * @return array Request query
44 */
45 public static function get_query( $query, $id_view, $date_range = null, $metric = null, $old = false ) {
46 if ( 'main_chart' === $query ) {
47 return $old ? self::main_chart_query_old( $id_view, $date_range, $metric ) : self::main_chart_query(
48 $id_view,
49 $date_range,
50 $metric
51 );
52 } elseif ( 'gender' === $query ) {
53 return self::gender_chart_query( $id_view, $date_range, $metric );
54 } elseif ( 'device' === $query ) {
55 return self::device_chart_query( $id_view, $date_range, $metric );
56 } elseif ( 'age' === $query ) {
57 return self::age_chart_query( $id_view, $date_range, $metric );
58 } elseif ( 'boxes' === $query ) {
59 return self::boxes_query( $id_view );
60 } elseif ( 'dashboard_boxes' === $query ) {
61 return $old ? self::dashboard_boxes_query_old( $id_view, $date_range ) :
62 self::dashboard_boxes_query( $id_view, $date_range );
63 } elseif ( 'sources' === $query ) {
64 return self::sources_query( $id_view, $date_range );
65 } else {
66 return array();
67 }
68 }
69
70 /**
71 * Preparing query for top traffic sources table
72 *
73 * @param int $id_view The Analytics view ID from which to retrieve data.
74 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
75 *
76 * @return array Sources query
77 */
78 public static function sources_query( $id_view, $date_ranges ) {
79 $reports_requests = array();
80
81 $ts = filter_input( INPUT_GET, 'ts', FILTER_SANITIZE_STRING );
82
83 if ( false === empty( $ts ) ) {
84 $reports_requests[] = array(
85 'viewId' => $id_view,
86 'dateRanges' => $date_ranges,
87 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
88 'includeEmptyRows' => true,
89 'pageSize' => 10,
90 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
91 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
92 );
93 } else {
94 $reports_requests[] = array(
95 'viewId' => $id_view,
96 'dateRanges' => $date_ranges,
97 'metrics' => self::set_metrics(
98 array(
99 'ga:pageviews',
100 'ga:uniquePageviews',
101 'ga:timeOnPage',
102 'ga:bounces',
103 'ga:entrances',
104 'ga:exits',
105 )
106 ),
107 'includeEmptyRows' => true,
108 'pageSize' => 10,
109 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
110 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
111 );
112 }
113
114 $query = array(
115 'reportRequests' => $reports_requests,
116 );
117
118 return $query;
119 }
120
121 /**
122 * Preparing query for dashboard boxes
123 *
124 * @param int $id_view The Analytics view ID from which to retrieve data.
125 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
126 *
127 * @return array Dashboard boxes query
128 */
129 public static function dashboard_boxes_query( $id_view, $date_ranges ) {
130 $reports_requests = array();
131
132 $ts = filter_input( INPUT_GET, 'ts', FILTER_SANITIZE_STRING );
133
134 if ( false === empty( $ts ) ) {
135 $reports_requests[] = array(
136 'viewId' => $id_view,
137 'dateRanges' => $date_ranges,
138 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
139 'includeEmptyRows' => true,
140 'pageSize' => 10,
141 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
142 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
143 );
144 } else {
145 $reports_requests[] = array(
146 'viewId' => $id_view,
147 'dateRanges' => $date_ranges,
148 'metrics' => self::set_metrics(
149 array(
150 'ga:pageviews',
151 'ga:uniquePageviews',
152 'ga:timeOnPage',
153 'ga:bounces',
154 'ga:entrances',
155 'ga:exits',
156 )
157 ),
158 'includeEmptyRows' => true,
159 'pageSize' => 10,
160 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
161 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
162 );
163 }
164 $query = array(
165 'reportRequests' => $reports_requests,
166 );
167
168 return $query;
169 }
170
171 /**
172 * Preparing query for dashboard boxes
173 *
174 * @param int $id_view The Analytics view ID from which to retrieve data.
175 * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'.
176 *
177 * @return array Dashboard boxes query
178 * @deprecated
179 */
180 public static function dashboard_boxes_query_old( $id_view, $date_range ) {
181 $reports_requests = array();
182
183 $th = filter_input( INPUT_GET, 'th', FILTER_SANITIZE_STRING );
184 $ts = filter_input( INPUT_GET, 'ts', FILTER_SANITIZE_STRING );
185
186 $days_ago = false === empty( $th ) ? '30daysAgo' : '7daysAgo';
187
188 if ( false === empty( $ts ) ) {
189 $reports_requests[] = array(
190 'viewId' => $id_view,
191 'dateRanges' => self::set_date_ranges( $days_ago, 'yesterday' ),
192 'metrics' => self::set_metrics( array( 'ga:pageviews' ) ),
193 'includeEmptyRows' => true,
194 'pageSize' => 10,
195 'dimensions' => self::set_dimensions( 'ga:sourceMedium' ),
196 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
197 );
198 } else {
199 $reports_requests[] = array(
200 'viewId' => $id_view,
201 'dateRanges' => self::set_date_ranges( $days_ago, 'yesterday' ),
202 'metrics' => self::set_metrics(
203 array(
204 'ga:pageviews',
205 'ga:uniquePageviews',
206 'ga:timeOnPage',
207 'ga:bounces',
208 'ga:entrances',
209 'ga:exits',
210 )
211 ),
212 'includeEmptyRows' => true,
213 'pageSize' => 10,
214 'dimensions' => self::set_dimensions( 'ga:pagePath' ),
215 'orderBys' => self::set_order_bys( 'ga:pageviews', 'DESCENDING' ),
216 );
217 }
218 $query = array(
219 'reportRequests' => $reports_requests,
220 );
221
222 return $query;
223 }
224
225 /**
226 * Preparing query for stats boxes
227 *
228 * @param int $id_view The Analytics view ID from which to retrieve data.
229 *
230 * @return array Boxes query
231 */
232 public static function boxes_query( $id_view ) {
233 $th = filter_input( INPUT_GET, 'th', FILTER_SANITIZE_STRING );
234
235 $range = false === empty( $th ) ? '30daysAgo' : '7daysAgo';
236 $range_s_prev = false === empty( $th ) ? '60daysAgo' : '14daysAgo';
237 $range_e_prev = false === empty( $th ) ? '31daysAgo' : '8daysAgo';
238 $reports_requests = array();
239 $reports_requests[] = array(
240 'viewId' => $id_view,
241 'dateRanges' => self::set_date_ranges( $range, 'yesterday', $range_s_prev, $range_e_prev ),
242 'metrics' => self::set_metrics(
243 array(
244 'ga:users',
245 'ga:pageviews',
246 'ga:pageviewsPerSession',
247 'ga:BounceRate',
248 )
249 ),
250 'includeEmptyRows' => true,
251 'dimensions' => self::set_dimensions( 'ga:date' ),
252 );
253 $query = array(
254 'reportRequests' => $reports_requests,
255 );
256
257 return $query;
258 }
259
260 /**
261 * Preparing query for chart
262 *
263 * @param int $id_view The Analytics view ID from which to retrieve data.
264 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
265 * @param string $metric A metric expression.
266 *
267 * @return array Chart query
268 */
269 public static function main_chart_query( $id_view, $date_ranges = null, $metric = null ) {
270 if ( true === empty( $metric ) ) {
271 $metric = 'ga:pageviews';
272 } else {
273 $metric = 'ga:' . $metric;
274 }
275
276 $reports_requests = array();
277 $reports_requests[] = array(
278 'viewId' => $id_view,
279 'dateRanges' => $date_ranges,
280 'metrics' => self::set_metrics( $metric ),
281 'includeEmptyRows' => true,
282 'dimensions' => self::set_dimensions( 'ga:date' ),
283 );
284 $query = array(
285 'reportRequests' => $reports_requests,
286 );
287
288 return $query;
289 }
290
291 /**
292 * Preparing query for chart
293 *
294 * @param int $id_view The Analytics view ID from which to retrieve data.
295 * @param string $date_range The start date for the query in the format YYYY-MM-DD or '7daysAgo'.
296 * @param string $metric A metric expression.
297 *
298 * @return array Chart query
299 * @deprecated
300 */
301 public static function main_chart_query_old( $id_view, $date_range = null, $metric = null ) {
302 if ( empty( $date_range ) ) {
303 $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
304 } else {
305 $date_ranges = self::set_date_ranges( $date_range, 'yesterday', '14daysAgo', '8daysAgo' );
306 }
307
308 if ( empty( $metric ) ) {
309 $metric = 'ga:pageviews';
310 } else {
311 $metric = 'ga:' . $metric;
312 }
313
314 $reports_requests = array();
315 $reports_requests[] = array(
316 'viewId' => $id_view,
317 'dateRanges' => $date_ranges,
318 'metrics' => self::set_metrics( $metric ),
319 'includeEmptyRows' => true,
320 'dimensions' => self::set_dimensions( 'ga:date' ),
321 );
322 $query = array(
323 'reportRequests' => $reports_requests,
324 );
325
326 return $query;
327 }
328
329 /**
330 * Preparing query for gender chart
331 *
332 * @param int $id_view The Analytics view ID from which to retrieve data.
333 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
334 * @param string $metric A metric expression.
335 *
336 * @return array Chart query
337 */
338 public static function gender_chart_query( $id_view, $date_ranges = null, $metric = null ) {
339 if ( true === empty( $date_ranges ) ) {
340 $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
341 }
342
343 $reports_requests = array();
344 $reports_requests[] = array(
345 'viewId' => $id_view,
346 'dateRanges' => $date_ranges,
347 'metrics' => self::set_metrics( 'ga:sessions' ),
348 'includeEmptyRows' => true,
349 'dimensions' => self::set_dimensions( 'ga:userGender' ),
350 );
351 $query = array(
352 'reportRequests' => $reports_requests,
353 );
354
355 return $query;
356 }
357
358 /**
359 * Preparing query for device chart.
360 *
361 * @param int $id_view The Analytics view ID from which to retrieve data.
362 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
363 * @param string $metric A metric expression.
364 *
365 * @return array Chart query
366 * @since 2.5.2
367 */
368 public static function device_chart_query( $id_view, $date_ranges = null, $metric = null ) {
369 return array(
370 'reportRequests' => array(
371 array(
372 'viewId' => $id_view,
373 'dateRanges' => $date_ranges,
374 'metrics' => self::set_metrics( 'ga:sessions' ),
375 'includeEmptyRows' => true,
376 'dimensions' => self::set_dimensions( 'ga:deviceCategory' ),
377 ),
378 ),
379 );
380 }
381
382 /**
383 * Preparing query for age chart
384 *
385 * @param int $id_view The Analytics view ID from which to retrieve data.
386 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
387 * @param string $metric A metric expression.
388 *
389 * @return array Chart query
390 */
391 public static function age_chart_query( $id_view, $date_ranges = null, $metric = null ) {
392 if ( true === empty( $date_ranges ) ) {
393 $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
394 }
395
396 $reports_requests = array();
397 $reports_requests[] = array(
398 'viewId' => $id_view,
399 'dateRanges' => $date_ranges,
400 'metrics' => self::set_metrics( 'ga:sessions' ),
401 'includeEmptyRows' => true,
402 'dimensions' => self::set_dimensions( 'ga:userAgeBracket' ),
403 );
404 $query = array(
405 'reportRequests' => $reports_requests,
406 );
407
408 return $query;
409 }
410
411 /**
412 * Setting order for requests
413 *
414 * @param string $name The field which to sort by. The default sort order is ascending. Example: ga:browser.
415 * @param string $sort The sorting order for the field. 'ASCENDING' or 'DESCENDING'.
416 *
417 * @return array OrderBys
418 */
419 public static function set_order_bys( $name, $sort ) {
420 $order = array();
421 $order[] = array(
422 'fieldName' => $name,
423 'sortOrder' => $sort,
424 );
425
426 return $order;
427 }
428
429 /**
430 * Setting metrics for requests
431 *
432 * @param mixed $expression A metric expression or array of expressions.
433 *
434 * @return array Metrics
435 */
436 public static function set_metrics( $expression ) {
437 $metrics = array();
438 if ( is_array( $expression ) ) {
439 foreach ( $expression as $exp ) {
440 $metrics[] = array(
441 'expression' => $exp,
442 );
443 }
444 } else {
445 $metrics[] = array(
446 'expression' => $expression,
447 );
448 }
449
450 return $metrics;
451 }
452
453 /**
454 * Setting dimensions for requests
455 *
456 * @param string $name Name of the dimension to fetch, for example ga:browser.
457 *
458 * @return array Dimensions
459 */
460 public static function set_dimensions( $name ) {
461 $dimensions = array();
462 $dimensions[] = array(
463 'name' => $name,
464 );
465
466 return $dimensions;
467 }
468
469 /**
470 * Setting date ranges for requests
471 *
472 * @param string $start_date The start date for the query in the format YYYY-MM-DD.
473 * @param string $end_date The end date for the query in the format YYYY-MM-DD.
474 * @param string $prev_start_date The start date (second range) for the query in the format YYYY-MM-DD.
475 * @param string $prev_end_date The start date (second range) for the query in the format YYYY-MM-DD.
476 *
477 * @return array Date ranges
478 */
479 public static function set_date_ranges( $start_date, $end_date, $prev_start_date = '', $prev_end_date = '' ) {
480 $date_danges = array();
481 $date_danges[] = array(
482 'startDate' => $start_date,
483 'endDate' => $end_date,
484 );
485 if ( false === empty( $prev_start_date ) && false === empty( $prev_end_date ) ) {
486 $date_danges[] = array(
487 'startDate' => $prev_start_date,
488 'endDate' => $prev_end_date,
489 );
490 }
491
492 return $date_danges;
493 }
494
495 /**
496 * Preparing response for data received from analytics
497 *
498 * @param array $data Analytics response.
499 *
500 * @return array Response rows
501 */
502 public static function prepare_response( $data ) {
503 $data = self::get_reports_from_response( $data );
504 self::handle_more_reports( $data );
505 $report = self::get_single_report( $data );
506 self::get_report_column_header( $report );
507 $report_data = self::get_report_data( $report );
508 self::get_totals( $report_data );
509 self::get_row_count( $report_data );
510 $rows = self::get_rows( $report_data );
511
512 return $rows;
513 }
514
515 /**
516 * Get dimensions from response row
517 *
518 * @param array $row Analytics response row.
519 *
520 * @return array|bool Dimensions
521 */
522 public static function get_dimensions( $row ) {
523 if ( false === empty( $row['dimensions'] ) ) {
524 return $row['dimensions'];
525 }
526
527 return false;
528 }
529
530 /**
531 * Get metrics from response row
532 *
533 * @param array $row Analytics response row.
534 *
535 * @return array|bool Metrics
536 */
537 public static function get_metrics( $row ) {
538 if ( false === empty( $row['metrics'] ) ) {
539 return $row['metrics'];
540 }
541
542 return false;
543 }
544
545 /**
546 * Get row from response report data
547 *
548 * @param array $report_data Analytics response report data.
549 *
550 * @return array|bool Rows
551 */
552 public static function get_rows( $report_data ) {
553 if ( false === empty( $report_data['rows'] ) ) {
554 return $report_data['rows'];
555 }
556
557 return false;
558 }
559
560 /**
561 * Get row count from response report data
562 *
563 * @param array $report_data Analytics response report data.
564 *
565 * @return array|bool Row count
566 */
567 public static function get_row_count( $report_data ) {
568 if ( false === empty( $report_data['rowCount'] ) ) {
569 return $report_data['rowCount'];
570 }
571
572 return false;
573 }
574
575 /**
576 * Get totals from response report data
577 *
578 * @param array $report_data Analytics response report data.
579 *
580 * @return array|bool Totals
581 */
582 public static function get_totals( $report_data ) {
583 if ( false === empty( $report_data['totals'] ) ) {
584 return $report_data['totals'];
585 }
586
587 return false;
588 }
589
590 /**
591 * Get reports from response data
592 *
593 * @param array $data Analytics response data.
594 *
595 * @return array|bool Reports
596 */
597 public static function get_reports_from_response( $data ) {
598 if ( false === empty( $data['reports'] ) ) {
599 return $data['reports'];
600 }
601
602 return false;
603 }
604
605 /**
606 * Show info for multiple data
607 *
608 * @param array $data Analytics response data.
609 */
610 public static function handle_more_reports( $data ) {
611 if ( count( $data ) > 1 ) {
612 echo 'more than one report';
613 }
614 }
615
616 /**
617 * Show info for multiple rows
618 *
619 * @param array $rows Analytics response rows.
620 */
621 public static function handle_more_rows( $rows ) {
622 if ( count( $rows ) > 1 ) {
623 echo 'more than one row';
624 }
625 }
626
627 /**
628 * Get single report from response data
629 *
630 * @param array $data Analytics response data.
631 *
632 * @return array|bool Report
633 */
634 public static function get_single_report( $data ) {
635 if ( false === empty( $data ) ) {
636 foreach ( $data as $report ) {
637 if ( false === empty( $report ) ) {
638 return $report;
639 }
640 }
641 }
642
643 return false;
644 }
645
646 /**
647 * Get single row from response data rows
648 *
649 * @param array $rows Analytics response data rows.
650 *
651 * @return array|bool Row
652 */
653 public static function get_single_row( $rows ) {
654 if ( false === empty( $rows ) ) {
655 foreach ( $rows as $row ) {
656 if ( false === empty( $row ) ) {
657 return $row;
658 }
659 }
660 }
661
662 return false;
663 }
664
665 /**
666 * Get column header from response data
667 *
668 * @param array $data Analytics response data.
669 *
670 * @return array Column header
671 */
672 public static function get_report_column_header( $data ) {
673 if ( false === empty( $data['columnHeader'] ) ) {
674 return $data['columnHeader'];
675 }
676
677 return false;
678 }
679
680 /**
681 * Get report data from response data
682 *
683 * @param array $data Analytics response data.
684 *
685 * @return array|bool data
686 */
687 public static function get_report_data( $data ) {
688 if ( false === empty( $data['data'] ) ) {
689 return $data['data'];
690 }
691
692 return false;
693 }
694
695 /**
696 * Get chart from response data
697 *
698 * @param array $response_data Analytics response data.
699 * @param int $period_in_days Period in days (default = 7).
700 *
701 * @return array chart data
702 */
703 public static function get_chart( $response_data, $period_in_days = 7 ) {
704 $chart_data = array();
705 if ( false === empty( $response_data ) ) {
706 $data = (
707 false === empty( $response_data['reports'] )
708 && false === empty( $response_data['reports'][0] )
709 && false === empty( $response_data['reports'][0]['data'] )
710 )
711 ? $response_data['reports'][0]['data'] : array();
712 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
713 if ( false === empty( $rows ) ) {
714 foreach ( $rows as $key => $row ) {
715 if ( $key < $period_in_days ) {
716 $chart_data[ $key ]['previous'] = false === empty( $row['metrics'][1]['values'][0] ) ? $row['metrics'][1]['values'][0] : 0;
717 $chart_data[ $key ]['previous-day'] = gmdate( 'M j', strtotime( $row['dimensions'][0] ) );
718 } else {
719 $chart_data[ $key - $period_in_days ]['day'] = gmdate( 'M j', strtotime( $row['dimensions'][0] ) );
720 $chart_data[ $key - $period_in_days ]['current'] = false === empty( $row['metrics'][0]['values'][0] ) ? $row['metrics'][0]['values'][0] : 0;
721
722 if (0 === $chart_data[ $key - $period_in_days ]['current']) {
723 $chart_data[ $key - $period_in_days ]['current'] = false === empty( $row['metrics'][1]['values'][0] ) ? $row['metrics'][1]['values'][0] : 0;
724 }
725
726 $chart_data['date'] = strtotime( $row['dimensions'][0] );
727 }
728 }
729 }
730 }
731
732 return $chart_data;
733 }
734
735 /**
736 * Get gender chart from response data
737 *
738 * @param array $response_data Analytics response data.
739 *
740 * @return array chart data
741 */
742 public static function get_gender_chart( $response_data ) {
743 $chart_data = array();
744 if ( false === empty( $response_data ) ) {
745 $data = ( false === empty( $response_data['reports'] ) && false === empty( $response_data['reports'][0] ) && false === empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
746 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
747 if ( false === empty( $rows ) ) {
748 foreach ( $rows as $key => $row ) {
749 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
750 }
751 }
752 }
753
754 return $chart_data;
755 }
756
757 /**
758 * Get device chart from response data.
759 *
760 * @param array $response_data Analytics response data array.
761 *
762 * @return array Chart data array.
763 */
764 public static function get_device_chart( $response_data ) {
765 $chart_data = array();
766 if ( false === empty( $response_data ) ) {
767 $data = ( false === empty( $response_data['reports'] ) && false === empty( $response_data['reports'][0] ) && false === empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
768 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
769 if ( false === empty( $rows ) ) {
770 foreach ( $rows as $row ) {
771 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
772 }
773 }
774 }
775
776 return $chart_data;
777 }
778
779 /**
780 * Get the value of metric data response.
781 *
782 * @param array $metrics Metrics array.
783 *
784 * @return mixed
785 */
786 private static function get_metric_value( $metrics ) {
787 if ( is_array( $metrics ) ) {
788 foreach ( $metrics as $metric ) {
789 $values[] = $metric['values'][0];
790 }
791 }
792
793 return $values[0];
794 }
795
796 /**
797 * Get gender chart from response data
798 *
799 * @param array $response_data Analytics response data.
800 *
801 * @return array chart data
802 */
803 public static function get_age_chart( $response_data ) {
804 $chart_data = array();
805 if ( false === empty( $response_data ) ) {
806 $data = ( false === empty( $response_data['reports'] ) && false === empty( $response_data['reports'][0] ) && false === empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
807 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
808 if ( false === empty( $rows ) ) {
809 foreach ( $rows as $key => $row ) {
810 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
811 }
812 }
813 }
814
815 return $chart_data;
816 }
817
818 /**
819 * Get dasboard chart from response data
820 *
821 * @param array $response_data Analytics response data.
822 *
823 * @return array dashboard chart data
824 */
825 public static function get_dashboard_chart( $response_data ) {
826 $chart_data = array();
827 if ( false === empty( $response_data ) ) {
828 $data = (
829 false === empty( $response_data['reports'] )
830 && false === empty( $response_data['reports'][0] )
831 && false === empty( $response_data['reports'][0]['data'] )
832 ) ? $response_data['reports'][0]['data'] : array();
833
834 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
835 if ( false === empty( $rows ) ) {
836 foreach ( $rows as $row ) {
837 $chart_data[] = array(
838 'day' => gmdate( 'M j', strtotime( $row['dimensions'][0] ) ),
839 'current' => false === empty( $row['metrics'][0]['values'][0] ) ? $row['metrics'][0]['values'][0] : 0,
840 );
841 }
842 }
843 }
844
845 return $chart_data;
846 }
847
848 /**
849 * Get boxes from response data
850 *
851 * @param array $data Analytics response data.
852 *
853 * @return array boxes data
854 */
855 public static function get_boxes( $data ) {
856 if ( false === empty( $data ) ) {
857 $data = self::get_reports_from_response( $data );
858 self::handle_more_reports( $data );
859 $report = self::get_single_report( $data );
860 self::get_report_column_header( $report );
861 $report_data = self::get_report_data( $report );
862 $totals = self::get_totals( $report_data );
863
864 return self::get_boxes_from_totals( $totals );
865 }
866 }
867
868 /**
869 * Get boxes from totals
870 *
871 * @param array $totals Analytics response totals.
872 *
873 * @return array|bool boxes data
874 */
875 public static function get_boxes_from_totals( $totals ) {
876 if ( false === empty( $totals ) ) {
877 $boxes_data = array();
878 foreach ( $totals as $key => $total ) {
879 if ( 0 === $key ) {
880 $boxes_data['Users']['current'] = $total['values'][0];
881 $boxes_data['Pageviews']['current'] = $total['values'][1];
882 $boxes_data['PageviewsPerSession']['current'] = $total['values'][2];
883 $boxes_data['BounceRate']['current'] = round( $total['values'][3], 2 );
884 } else {
885 $boxes_data['Users']['previous'] = $total['values'][0];
886 $boxes_data['Pageviews']['previous'] = $total['values'][1];
887 $boxes_data['PageviewsPerSession']['previous'] = $total['values'][2];
888 $boxes_data['BounceRate']['previous'] = round( $total['values'][3], 2 );
889 }
890 }
891
892 return self::prepare_boxes( $boxes_data );
893 }
894
895 return false;
896 }
897
898 /**
899 * Prepare boxes data
900 *
901 * @param array $boxes_data Boxes data.
902 *
903 * @return array boxes data
904 */
905 public static function prepare_boxes( $boxes_data ) {
906 $boxes_data['Users']['diff'] = ( $boxes_data['Users']['previous'] > 0 ) ? round( ( $boxes_data['Users']['current'] - $boxes_data['Users']['previous'] ) / $boxes_data['Users']['previous'] * 100, 2 ) : 100;
907 $boxes_data['Pageviews']['diff'] = ( $boxes_data['Pageviews']['previous'] > 0 ) ? round( ( $boxes_data['Pageviews']['current'] - $boxes_data['Pageviews']['previous'] ) / $boxes_data['Pageviews']['previous'] * 100, 2 ) : 100;
908 $boxes_data['PageviewsPerSession']['diff'] = ( $boxes_data['PageviewsPerSession']['previous'] > 0 ) ? round( ( $boxes_data['PageviewsPerSession']['current'] - $boxes_data['PageviewsPerSession']['previous'] ) / $boxes_data['PageviewsPerSession']['previous'] * 100, 2 ) : 100;
909 $boxes_data['BounceRate']['diff'] = ( $boxes_data['BounceRate']['previous'] > 0 ) ? round( ( $boxes_data['BounceRate']['current'] - $boxes_data['BounceRate']['previous'] ) / $boxes_data['BounceRate']['previous'] * 100, 2 ) : 100;
910 $boxes_data['Users']['diff'] = ( 0 === $boxes_data['Users']['previous'] && 0 === $boxes_data['Users']['current'] ) ? 0 : $boxes_data['Users']['diff'];
911 $boxes_data['Pageviews']['diff'] = ( 0 === $boxes_data['Pageviews']['previous'] && 0 === $boxes_data['Pageviews']['current'] ) ? 0 : $boxes_data['Pageviews']['diff'];
912 $boxes_data['PageviewsPerSession']['diff'] = ( 0 === $boxes_data['PageviewsPerSession']['previous'] && 0 === $boxes_data['PageviewsPerSession']['current'] ) ? 0 : $boxes_data['PageviewsPerSession']['diff'];
913 $boxes_data['BounceRate']['diff'] = ( 0 === $boxes_data['BounceRate']['previous'] && 0 === $boxes_data['BounceRate']['current'] ) ? 0 : $boxes_data['BounceRate']['diff'];
914 $boxes_data['Users']['label'] = 'Users';
915 $boxes_data['Pageviews']['label'] = 'Pageviews';
916 $boxes_data['PageviewsPerSession']['label'] = 'Pages / Session';
917 $boxes_data['BounceRate']['label'] = 'Bounce Rate';
918 $boxes_data['Users']['comparison'] = $boxes_data['Users']['current'] . ' vs ' . $boxes_data['Users']['previous'];
919 $boxes_data['Pageviews']['comparison'] = $boxes_data['Pageviews']['current'] . ' vs ' . $boxes_data['Pageviews']['previous'];
920 $boxes_data['PageviewsPerSession']['comparison'] = self::number_format_clean( $boxes_data['PageviewsPerSession']['current'], 2, '.', ',' ) . ' vs ' . self::number_format_clean( $boxes_data['PageviewsPerSession']['previous'], 2, '.', ',' );
921 $boxes_data['BounceRate']['comparison'] = self::number_format_clean( $boxes_data['BounceRate']['current'], 2, '.', ',' ) . '% vs ' . self::number_format_clean( $boxes_data['BounceRate']['previous'], 2, '.', ',' ) . '%';
922 $boxes_data['Users']['color'] = ( $boxes_data['Users']['diff'] > 0 ) ? 'green' : 'red';
923 $boxes_data['Pageviews']['color'] = ( $boxes_data['Pageviews']['diff'] > 0 ) ? 'green' : 'red';
924 $boxes_data['PageviewsPerSession']['color'] = ( $boxes_data['PageviewsPerSession']['diff'] > 0 ) ? 'green' : 'red';
925 $boxes_data['BounceRate']['color'] = ( $boxes_data['BounceRate']['diff'] > 0 ) ? 'red' : 'green';
926 $boxes_data['Users']['color'] = ( 0 !== $boxes_data['Users']['diff'] ) ? $boxes_data['Users']['color'] : 'black';
927 $boxes_data['Pageviews']['color'] = ( 0 !== $boxes_data['Pageviews']['diff'] ) ? $boxes_data['Pageviews']['color'] : 'black';
928 $boxes_data['PageviewsPerSession']['color'] = ( 0 !== $boxes_data['PageviewsPerSession']['diff'] ) ? $boxes_data['PageviewsPerSession']['color'] : 'black';
929 $boxes_data['BounceRate']['color'] = ( 0 !== $boxes_data['BounceRate']['diff'] ) ? $boxes_data['BounceRate']['color'] : 'black';
930
931 return $boxes_data;
932 }
933
934 /**
935 * Number format for boxes
936 *
937 * @param float $number Number to format.
938 * @param int $precision Precision.
939 * @param string $dec_point Decimal point.
940 * @param string $thousands_sep Thousands Separator.
941 *
942 * @return string clean number format
943 */
944 public static function number_format_clean( $number, $precision = 0, $dec_point = '.', $thousands_sep = ',' ) {
945 if ( 0 === $number ) {
946 return 0;
947 } else {
948 $format = number_format( $number, $precision, $dec_point, $thousands_sep );
949 if ( '.00' === substr( $format, 2 ) ) {
950 return substr( $format, 0, - 3 );
951 }
952
953 return $format;
954 }
955 }
956
957 /**
958 * Get sources from analytics response data
959 *
960 * @param array $data Analytics response data.
961 *
962 * @return array|bool sources data
963 */
964 public static function get_sources( $data ) {
965 if ( false === empty( $data ) ) {
966 $data = self::get_reports_from_response( $data );
967 self::handle_more_reports( $data );
968 $report = self::get_single_report( $data );
969 self::get_report_column_header( $report );
970 $report_data = self::get_report_data( $report );
971 $rows = self::get_rows( $report_data );
972 $totals = self::get_totals( $report_data );
973 $total_count = array();
974 if ( false === empty( $totals ) ) {
975 foreach ( $totals as $key => $total ) {
976 $total_count = $total['values'][0];
977 }
978 }
979 $sources = array(
980 'total' => $total_count,
981 'sum' => 0,
982 'rows' => array(),
983 );
984 if ( false === empty( $rows ) ) {
985 $i = 1;
986 foreach ( $rows as $row ) {
987 if ( false === empty( $row ) ) {
988 foreach ( $row as $key => $value ) {
989 if ( 'dimensions' === $key ) {
990 $sources['rows'][ $i ]['name'] = $value[0];
991 $sources['rows'][ $i ]['url'] = $value[0];
992 } elseif ( 'metrics' === $key ) {
993 $sources['rows'][ $i ]['number'] = $value[0]['values'][0];
994 $sources['rows'][ $i ]['percent'] = ( false === empty( $total_count ) ) ? round( $value[0]['values'][0] / $total_count * 100, 2 ) : 0;
995 $sources['sum'] += $value[0]['values'][0];
996 }
997 }
998 $i ++;
999 }
1000 }
1001 }
1002
1003 return $sources;
1004 }
1005
1006 return false;
1007 }
1008
1009 /**
1010 * Get dashboard boxes data from analytics response data
1011 *
1012 * @param array $data Analytics response data.
1013 *
1014 * @return array dashboard boxes data
1015 */
1016 public static function get_dashboard_boxes_data( $data ) {
1017 if ( false === empty( $data ) ) {
1018 $data = self::get_reports_from_response( $data );
1019 self::handle_more_reports( $data );
1020 $report = self::get_single_report( $data );
1021 self::get_report_column_header( $report );
1022 $report_data = self::get_report_data( $report );
1023 $totals = self::get_totals( $report_data );
1024 $boxes_data = array();
1025 $boxes_data['Sessions'] = array(
1026 'label' => 'Visits',
1027 'value' => $totals[0]['values'][0],
1028 );
1029 $boxes_data['Pageviews'] = array(
1030 'label' => 'Pageviews',
1031 'value' => $totals[0]['values'][1],
1032 );
1033 $boxes_data['pageviewsPerSession'] = array(
1034 'label' => 'Pages / Visit',
1035 'value' => self::number_format_clean( $totals[0]['values'][2], 2, '.', ',' ),
1036 );
1037 $boxes_data['BounceRate'] = array(
1038 'label' => 'Bounce Rate',
1039 'value' => self::number_format_clean( $totals[0]['values'][3], 2, '.', ',' ) . '%',
1040 );
1041 $boxes_data['avgTimeOnPage'] = array(
1042 'label' => 'Avg. Time on Site',
1043 'value' => gmdate( 'H:i:s', $totals[0]['values'][4] ),
1044 );
1045 $boxes_data['percentNewSessions'] = array(
1046 'label' => '% of New Visits',
1047 'value' => self::number_format_clean( $totals[0]['values'][5], 2, '.', ',' ),
1048 );
1049
1050 return $boxes_data;
1051 }
1052 }
1053
1054 /**
1055 * Get Empty Boxes Structure.
1056 *
1057 * @return array Array of empty boxes structure values.
1058 */
1059 public static function get_empty_boxes_structure() {
1060 $boxes_data = array();
1061 $boxes_data['Sessions'] = array(
1062 'label' => 'Visits',
1063 'value' => 0,
1064 );
1065 $boxes_data['Pageviews'] = array(
1066 'label' => 'Pageviews',
1067 'value' => 0,
1068 );
1069 $boxes_data['pageviewsPerSession'] = array(
1070 'label' => 'Pages / Visit',
1071 'value' => self::number_format_clean( 0, 2, '.', ',' ),
1072 );
1073 $boxes_data['BounceRate'] = array(
1074 'label' => 'Bounce Rate',
1075 'value' => self::number_format_clean( 0, 2, '.', ',' ) . '%',
1076 );
1077 $boxes_data['avgTimeOnPage'] = array(
1078 'label' => 'Avg. Time on Site',
1079 'value' => gmdate( 'H:i:s', 0 ),
1080 );
1081 $boxes_data['percentNewSessions'] = array(
1082 'label' => '% of New Visits',
1083 'value' => self::number_format_clean( 0, 2, '.', ',' ),
1084 );
1085
1086 return $boxes_data;
1087 }
1088 }
1089