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