PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.0.0
ShareThis Dashboard for Google Analytics v3.0.0
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 4 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 4 years ago class-ga-template.php 4 years ago
class-ga-stats.php
1084 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 $chart_data['date'] = strtotime( $row['dimensions'][0] );
722 }
723 }
724 }
725 }
726
727 return $chart_data;
728 }
729
730 /**
731 * Get gender chart from response data
732 *
733 * @param array $response_data Analytics response data.
734 *
735 * @return array chart data
736 */
737 public static function get_gender_chart( $response_data ) {
738 $chart_data = array();
739 if ( false === empty( $response_data ) ) {
740 $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();
741 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
742 if ( false === empty( $rows ) ) {
743 foreach ( $rows as $key => $row ) {
744 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
745 }
746 }
747 }
748
749 return $chart_data;
750 }
751
752 /**
753 * Get device chart from response data.
754 *
755 * @param array $response_data Analytics response data array.
756 *
757 * @return array Chart data array.
758 */
759 public static function get_device_chart( $response_data ) {
760 $chart_data = array();
761 if ( false === empty( $response_data ) ) {
762 $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();
763 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
764 if ( false === empty( $rows ) ) {
765 foreach ( $rows as $row ) {
766 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
767 }
768 }
769 }
770
771 return $chart_data;
772 }
773
774 /**
775 * Get the value of metric data response.
776 *
777 * @param array $metrics Metrics array.
778 *
779 * @return mixed
780 */
781 private static function get_metric_value( $metrics ) {
782 if ( is_array( $metrics ) ) {
783 foreach ( $metrics as $metric ) {
784 $values[] = $metric['values'][0];
785 }
786 }
787
788 return $values[0];
789 }
790
791 /**
792 * Get gender chart from response data
793 *
794 * @param array $response_data Analytics response data.
795 *
796 * @return array chart data
797 */
798 public static function get_age_chart( $response_data ) {
799 $chart_data = array();
800 if ( false === empty( $response_data ) ) {
801 $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();
802 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
803 if ( false === empty( $rows ) ) {
804 foreach ( $rows as $key => $row ) {
805 $chart_data[ $row['dimensions'][0] ] = self::get_metric_value( $row['metrics'] );
806 }
807 }
808 }
809
810 return $chart_data;
811 }
812
813 /**
814 * Get dasboard chart from response data
815 *
816 * @param array $response_data Analytics response data.
817 *
818 * @return array dashboard chart data
819 */
820 public static function get_dashboard_chart( $response_data ) {
821 $chart_data = array();
822 if ( false === empty( $response_data ) ) {
823 $data = (
824 false === empty( $response_data['reports'] )
825 && false === empty( $response_data['reports'][0] )
826 && false === empty( $response_data['reports'][0]['data'] )
827 ) ? $response_data['reports'][0]['data'] : array();
828
829 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
830 if ( false === empty( $rows ) ) {
831 foreach ( $rows as $row ) {
832 $chart_data[] = array(
833 'day' => gmdate( 'M j', strtotime( $row['dimensions'][0] ) ),
834 'current' => false === empty( $row['metrics'][0]['values'][0] ) ? $row['metrics'][0]['values'][0] : 0,
835 );
836 }
837 }
838 }
839
840 return $chart_data;
841 }
842
843 /**
844 * Get boxes from response data
845 *
846 * @param array $data Analytics response data.
847 *
848 * @return array boxes data
849 */
850 public static function get_boxes( $data ) {
851 if ( false === empty( $data ) ) {
852 $data = self::get_reports_from_response( $data );
853 self::handle_more_reports( $data );
854 $report = self::get_single_report( $data );
855 self::get_report_column_header( $report );
856 $report_data = self::get_report_data( $report );
857 $totals = self::get_totals( $report_data );
858
859 return self::get_boxes_from_totals( $totals );
860 }
861 }
862
863 /**
864 * Get boxes from totals
865 *
866 * @param array $totals Analytics response totals.
867 *
868 * @return array|bool boxes data
869 */
870 public static function get_boxes_from_totals( $totals ) {
871 if ( false === empty( $totals ) ) {
872 $boxes_data = array();
873 foreach ( $totals as $key => $total ) {
874 if ( 0 === $key ) {
875 $boxes_data['Users']['current'] = $total['values'][0];
876 $boxes_data['Pageviews']['current'] = $total['values'][1];
877 $boxes_data['PageviewsPerSession']['current'] = $total['values'][2];
878 $boxes_data['BounceRate']['current'] = round( $total['values'][3], 2 );
879 } else {
880 $boxes_data['Users']['previous'] = $total['values'][0];
881 $boxes_data['Pageviews']['previous'] = $total['values'][1];
882 $boxes_data['PageviewsPerSession']['previous'] = $total['values'][2];
883 $boxes_data['BounceRate']['previous'] = round( $total['values'][3], 2 );
884 }
885 }
886
887 return self::prepare_boxes( $boxes_data );
888 }
889
890 return false;
891 }
892
893 /**
894 * Prepare boxes data
895 *
896 * @param array $boxes_data Boxes data.
897 *
898 * @return array boxes data
899 */
900 public static function prepare_boxes( $boxes_data ) {
901 $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;
902 $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;
903 $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;
904 $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;
905 $boxes_data['Users']['diff'] = ( 0 === $boxes_data['Users']['previous'] && 0 === $boxes_data['Users']['current'] ) ? 0 : $boxes_data['Users']['diff'];
906 $boxes_data['Pageviews']['diff'] = ( 0 === $boxes_data['Pageviews']['previous'] && 0 === $boxes_data['Pageviews']['current'] ) ? 0 : $boxes_data['Pageviews']['diff'];
907 $boxes_data['PageviewsPerSession']['diff'] = ( 0 === $boxes_data['PageviewsPerSession']['previous'] && 0 === $boxes_data['PageviewsPerSession']['current'] ) ? 0 : $boxes_data['PageviewsPerSession']['diff'];
908 $boxes_data['BounceRate']['diff'] = ( 0 === $boxes_data['BounceRate']['previous'] && 0 === $boxes_data['BounceRate']['current'] ) ? 0 : $boxes_data['BounceRate']['diff'];
909 $boxes_data['Users']['label'] = 'Users';
910 $boxes_data['Pageviews']['label'] = 'Pageviews';
911 $boxes_data['PageviewsPerSession']['label'] = 'Pages / Session';
912 $boxes_data['BounceRate']['label'] = 'Bounce Rate';
913 $boxes_data['Users']['comparison'] = $boxes_data['Users']['current'] . ' vs ' . $boxes_data['Users']['previous'];
914 $boxes_data['Pageviews']['comparison'] = $boxes_data['Pageviews']['current'] . ' vs ' . $boxes_data['Pageviews']['previous'];
915 $boxes_data['PageviewsPerSession']['comparison'] = self::number_format_clean( $boxes_data['PageviewsPerSession']['current'], 2, '.', ',' ) . ' vs ' . self::number_format_clean( $boxes_data['PageviewsPerSession']['previous'], 2, '.', ',' );
916 $boxes_data['BounceRate']['comparison'] = self::number_format_clean( $boxes_data['BounceRate']['current'], 2, '.', ',' ) . '% vs ' . self::number_format_clean( $boxes_data['BounceRate']['previous'], 2, '.', ',' ) . '%';
917 $boxes_data['Users']['color'] = ( $boxes_data['Users']['diff'] > 0 ) ? 'green' : 'red';
918 $boxes_data['Pageviews']['color'] = ( $boxes_data['Pageviews']['diff'] > 0 ) ? 'green' : 'red';
919 $boxes_data['PageviewsPerSession']['color'] = ( $boxes_data['PageviewsPerSession']['diff'] > 0 ) ? 'green' : 'red';
920 $boxes_data['BounceRate']['color'] = ( $boxes_data['BounceRate']['diff'] > 0 ) ? 'red' : 'green';
921 $boxes_data['Users']['color'] = ( 0 !== $boxes_data['Users']['diff'] ) ? $boxes_data['Users']['color'] : 'black';
922 $boxes_data['Pageviews']['color'] = ( 0 !== $boxes_data['Pageviews']['diff'] ) ? $boxes_data['Pageviews']['color'] : 'black';
923 $boxes_data['PageviewsPerSession']['color'] = ( 0 !== $boxes_data['PageviewsPerSession']['diff'] ) ? $boxes_data['PageviewsPerSession']['color'] : 'black';
924 $boxes_data['BounceRate']['color'] = ( 0 !== $boxes_data['BounceRate']['diff'] ) ? $boxes_data['BounceRate']['color'] : 'black';
925
926 return $boxes_data;
927 }
928
929 /**
930 * Number format for boxes
931 *
932 * @param float $number Number to format.
933 * @param int $precision Precision.
934 * @param string $dec_point Decimal point.
935 * @param string $thousands_sep Thousands Separator.
936 *
937 * @return string clean number format
938 */
939 public static function number_format_clean( $number, $precision = 0, $dec_point = '.', $thousands_sep = ',' ) {
940 if ( 0 === $number ) {
941 return 0;
942 } else {
943 $format = number_format( $number, $precision, $dec_point, $thousands_sep );
944 if ( '.00' === substr( $format, 2 ) ) {
945 return substr( $format, 0, - 3 );
946 }
947
948 return $format;
949 }
950 }
951
952 /**
953 * Get sources from analytics response data
954 *
955 * @param array $data Analytics response data.
956 *
957 * @return array|bool sources data
958 */
959 public static function get_sources( $data ) {
960 if ( false === empty( $data ) ) {
961 $data = self::get_reports_from_response( $data );
962 self::handle_more_reports( $data );
963 $report = self::get_single_report( $data );
964 self::get_report_column_header( $report );
965 $report_data = self::get_report_data( $report );
966 $rows = self::get_rows( $report_data );
967 $totals = self::get_totals( $report_data );
968 $total_count = array();
969 if ( false === empty( $totals ) ) {
970 foreach ( $totals as $key => $total ) {
971 $total_count = $total['values'][0];
972 }
973 }
974 $sources = array(
975 'total' => $total_count,
976 'sum' => 0,
977 'rows' => array(),
978 );
979 if ( false === empty( $rows ) ) {
980 $i = 1;
981 foreach ( $rows as $row ) {
982 if ( false === empty( $row ) ) {
983 foreach ( $row as $key => $value ) {
984 if ( 'dimensions' === $key ) {
985 $sources['rows'][ $i ]['name'] = $value[0];
986 $sources['rows'][ $i ]['url'] = $value[0];
987 } elseif ( 'metrics' === $key ) {
988 $sources['rows'][ $i ]['number'] = $value[0]['values'][0];
989 $sources['rows'][ $i ]['percent'] = ( false === empty( $total_count ) ) ? round( $value[0]['values'][0] / $total_count * 100, 2 ) : 0;
990 $sources['sum'] += $value[0]['values'][0];
991 }
992 }
993 $i ++;
994 }
995 }
996 }
997
998 return $sources;
999 }
1000
1001 return false;
1002 }
1003
1004 /**
1005 * Get dashboard boxes data from analytics response data
1006 *
1007 * @param array $data Analytics response data.
1008 *
1009 * @return array dashboard boxes data
1010 */
1011 public static function get_dashboard_boxes_data( $data ) {
1012 if ( false === empty( $data ) ) {
1013 $data = self::get_reports_from_response( $data );
1014 self::handle_more_reports( $data );
1015 $report = self::get_single_report( $data );
1016 self::get_report_column_header( $report );
1017 $report_data = self::get_report_data( $report );
1018 $totals = self::get_totals( $report_data );
1019 $boxes_data = array();
1020 $boxes_data['Sessions'] = array(
1021 'label' => 'Visits',
1022 'value' => $totals[0]['values'][0],
1023 );
1024 $boxes_data['Pageviews'] = array(
1025 'label' => 'Pageviews',
1026 'value' => $totals[0]['values'][1],
1027 );
1028 $boxes_data['pageviewsPerSession'] = array(
1029 'label' => 'Pages / Visit',
1030 'value' => self::number_format_clean( $totals[0]['values'][2], 2, '.', ',' ),
1031 );
1032 $boxes_data['BounceRate'] = array(
1033 'label' => 'Bounce Rate',
1034 'value' => self::number_format_clean( $totals[0]['values'][3], 2, '.', ',' ) . '%',
1035 );
1036 $boxes_data['avgTimeOnPage'] = array(
1037 'label' => 'Avg. Time on Site',
1038 'value' => gmdate( 'H:i:s', $totals[0]['values'][4] ),
1039 );
1040 $boxes_data['percentNewSessions'] = array(
1041 'label' => '% of New Visits',
1042 'value' => self::number_format_clean( $totals[0]['values'][5], 2, '.', ',' ),
1043 );
1044
1045 return $boxes_data;
1046 }
1047 }
1048
1049 /**
1050 * Get Empty Boxes Structure.
1051 *
1052 * @return array Array of empty boxes structure values.
1053 */
1054 public static function get_empty_boxes_structure() {
1055 $boxes_data = array();
1056 $boxes_data['Sessions'] = array(
1057 'label' => 'Visits',
1058 'value' => 0,
1059 );
1060 $boxes_data['Pageviews'] = array(
1061 'label' => 'Pageviews',
1062 'value' => 0,
1063 );
1064 $boxes_data['pageviewsPerSession'] = array(
1065 'label' => 'Pages / Visit',
1066 'value' => self::number_format_clean( 0, 2, '.', ',' ),
1067 );
1068 $boxes_data['BounceRate'] = array(
1069 'label' => 'Bounce Rate',
1070 'value' => self::number_format_clean( 0, 2, '.', ',' ) . '%',
1071 );
1072 $boxes_data['avgTimeOnPage'] = array(
1073 'label' => 'Avg. Time on Site',
1074 'value' => gmdate( 'H:i:s', 0 ),
1075 );
1076 $boxes_data['percentNewSessions'] = array(
1077 'label' => '% of New Visits',
1078 'value' => self::number_format_clean( 0, 2, '.', ',' ),
1079 );
1080
1081 return $boxes_data;
1082 }
1083 }
1084