PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 2.5.3
ShareThis Dashboard for Google Analytics v2.5.3
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 4 years ago Ga_Frontend.php 5 years ago Ga_Helper.php 4 years ago Ga_Hook.php 4 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
955 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 if ( true === empty( $date_ranges ) ) {
219 $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
220 }
221
222 $reports_requests = array();
223 $reports_requests[] = array(
224 'viewId' => $id_view,
225 'dateRanges' => $date_ranges,
226 'metrics' => self::set_metrics( 'ga:sessions' ),
227 'includeEmptyRows' => true,
228 'dimensions' => self::set_dimensions( 'ga:userGender' )
229 );
230 $query = array(
231 'reportRequests' => $reports_requests
232 );
233
234 return $query;
235 }
236
237 /**
238 * Preparing query for device chart.
239 *
240 * @param int $id_view The Analytics view ID from which to retrieve data.
241 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
242 * @param string $metric A metric expression
243 *
244 * @return array Chart query
245 * @since 2.5.2
246 */
247 public static function device_chart_query( $id_view, $date_ranges = null, $metric = null ) {
248 return array(
249 'reportRequests' => array(
250 array(
251 'viewId' => $id_view,
252 'dateRanges' => $date_ranges,
253 'metrics' => self::set_metrics( 'ga:sessions' ),
254 'includeEmptyRows' => true,
255 'dimensions' => self::set_dimensions( 'ga:deviceCategory' )
256 )
257 )
258 );
259 }
260
261 /**
262 * Preparing query for age chart
263 *
264 * @param int $id_view The Analytics view ID from which to retrieve data.
265 * @param array $date_ranges An array representing the date ranges that will be passed to chart query.
266 * @param string $metric A metric expression
267 *
268 * @return array Chart query
269 */
270 public static function age_chart_query( $id_view, $date_ranges = null, $metric = null ) {
271 if ( true === empty( $date_ranges ) ) {
272 $date_ranges = self::set_date_ranges( '7daysAgo', 'yesterday', '14daysAgo', '8daysAgo' );
273 }
274
275 $reports_requests = array();
276 $reports_requests[] = array(
277 'viewId' => $id_view,
278 'dateRanges' => $date_ranges,
279 'metrics' => self::set_metrics( 'ga:sessions' ),
280 'includeEmptyRows' => true,
281 'dimensions' => self::set_dimensions( 'ga:userAgeBracket' )
282 );
283 $query = array(
284 'reportRequests' => $reports_requests
285 );
286
287 return $query;
288 }
289
290 /**
291 * Setting order for requests
292 *
293 * @param string $name The field which to sort by. The default sort order is ascending. Example: ga:browser.
294 * @param string $sort The sorting order for the field. 'ASCENDING' or 'DESCENDING'
295 *
296 * @return array OrderBys
297 */
298 public static function set_order_bys( $name, $sort ) {
299 $order = array();
300 $order[] = array(
301 'fieldName' => $name,
302 'sortOrder' => $sort,
303 );
304
305 return $order;
306 }
307
308 /**
309 * Setting metrics for requests
310 *
311 * @param mixed $expression A metric expression or array of expressions
312 *
313 * @return array Metrics
314 */
315 public static function set_metrics( $expression ) {
316 $metrics = array();
317 if ( is_array( $expression ) ) {
318 foreach ( $expression as $exp ) {
319 $metrics[] = array(
320 'expression' => $exp
321 );
322 }
323 } else {
324 $metrics[] = array(
325 'expression' => $expression
326 );
327 }
328
329 return $metrics;
330 }
331
332 /**
333 * Setting dimensions for requests
334 *
335 * @param string $name Name of the dimension to fetch, for example ga:browser.
336 *
337 * @return array Dimensions
338 */
339 public static function set_dimensions( $name ) {
340 $dimensions = array();
341 $dimensions[] = array(
342 'name' => $name
343 );
344
345 return $dimensions;
346 }
347
348 /**
349 * Setting date ranges for requests
350 *
351 * @param string $start_date The start date for the query in the format YYYY-MM-DD.
352 * @param string $end_date The end date for the query in the format YYYY-MM-DD.
353 * @param string $prev_start_date The start date (second range) for the query in the format YYYY-MM-DD.
354 * @param string $prev_end_date The start date (second range) for the query in the format YYYY-MM-DD.
355 *
356 * @return array Date ranges
357 */
358 public static function set_date_ranges( $start_date, $end_date, $prev_start_date = '', $prev_end_date = '' ) {
359 $date_danges = array();
360 $date_danges[] = array(
361 'startDate' => $start_date,
362 'endDate' => $end_date,
363 );
364 if ( !empty( $prev_start_date ) and ! empty( $prev_end_date ) ) {
365 $date_danges[] = array(
366 'startDate' => $prev_start_date,
367 'endDate' => $prev_end_date,
368 );
369 }
370
371 return $date_danges;
372 }
373
374 /**
375 * Preparing response for data received from analytics
376 *
377 * @param array $data Analytics response
378 *
379 * @return array Response rows
380 */
381 public static function prepare_response( $data ) {
382 $data = self::get_reports_from_response( $data );
383 self::handle_more_reports( $data );
384 $report = self::get_single_report( $data );
385 self::get_report_column_header( $report );
386 $report_data = self::get_report_data( $report );
387 self::get_totals( $report_data );
388 self::get_row_count( $report_data );
389 $rows = self::get_rows( $report_data );
390
391 return $rows;
392 }
393
394 /**
395 * Get dimensions from response row
396 *
397 * @param array $row Analytics response row
398 *
399 * @return array|bool Dimensions
400 */
401 public static function get_dimensions( $row ) {
402 if ( !empty( $row[ 'dimensions' ] ) ) {
403 return $row[ 'dimensions' ];
404 }
405
406 return false;
407 }
408
409 /**
410 * Get metrics from response row
411 *
412 * @param array $row Analytics response row
413 *
414 * @return array|bool Metrics
415 */
416 public static function get_metrics( $row ) {
417 if ( !empty( $row[ 'metrics' ] ) ) {
418 return $row[ 'metrics' ];
419 }
420
421 return false;
422 }
423
424 /**
425 * Get row from response report data
426 *
427 * @param array $report_data Analytics response report data
428 *
429 * @return array|bool Rows
430 */
431 public static function get_rows( $report_data ) {
432 if ( !empty( $report_data[ 'rows' ] ) ) {
433 return $report_data[ 'rows' ];
434 }
435
436 return false;
437 }
438
439 /**
440 * Get row count from response report data
441 *
442 * @param array $report_data Analytics response report data
443 *
444 * @return array|bool Row count
445 */
446 public static function get_row_count( $report_data ) {
447 if ( !empty( $report_data[ 'rowCount' ] ) ) {
448 return $report_data[ 'rowCount' ];
449 }
450
451 return false;
452 }
453
454 /**
455 * Get totals from response report data
456 *
457 * @param array $report_data Analytics response report data
458 *
459 * @return array|bool Totals
460 */
461 public static function get_totals( $report_data ) {
462 if ( !empty( $report_data[ 'totals' ] ) ) {
463 return $report_data[ 'totals' ];
464 }
465
466 return false;
467 }
468
469 /**
470 * Get reports from response data
471 *
472 * @param array $data Analytics response data
473 *
474 * @return array|bool Reports
475 */
476 public static function get_reports_from_response( $data ) {
477 if ( !empty( $data[ 'reports' ] ) ) {
478 return $data[ 'reports' ];
479 }
480
481 return false;
482 }
483
484 /**
485 * Show info for multiple data
486 *
487 * @param array $data Analytics response data
488 *
489 */
490 public static function handle_more_reports( $data ) {
491 if ( count( $data ) > 1 ) {
492 echo 'more than one report';
493 }
494 }
495
496 /**
497 * Show info for multiple rows
498 *
499 * @param array $rows Analytics response rows
500 *
501 */
502 public static function handle_more_rows( $rows ) {
503 if ( count( $rows ) > 1 ) {
504 echo 'more than one row';
505 }
506 }
507
508 /**
509 * Get single report from response data
510 *
511 * @param array $data Analytics response data
512 *
513 * @return array|bool Report
514 */
515 public static function get_single_report( $data ) {
516 if ( !empty( $data ) ) {
517 foreach ( $data as $report ) {
518 if ( !empty( $report ) ) {
519 return $report;
520 }
521 }
522 }
523
524 return false;
525 }
526
527 /**
528 * Get single row from response data rows
529 *
530 * @param array $rows Analytics response data rows
531 *
532 * @return array|bool Row
533 */
534 public static function get_single_row( $rows ) {
535 if ( !empty( $rows ) ) {
536 foreach ( $rows as $row ) {
537 if ( !empty( $row ) ) {
538 return $row;
539 }
540 }
541 }
542
543 return false;
544 }
545
546 /**
547 * Get column header from response data
548 *
549 * @param array $data Analytics response data
550 *
551 * @return array Column header
552 */
553 public static function get_report_column_header( $data ) {
554 if ( !empty( $data[ 'columnHeader' ] ) ) {
555 return $data[ 'columnHeader' ];
556 }
557
558 return false;
559 }
560
561 /**
562 * Get report data from response data
563 *
564 * @param array $data Analytics response data
565 *
566 * @return array data
567 */
568 public static function get_report_data( $data ) {
569 if ( !empty( $data[ 'data' ] ) ) {
570 return $data[ 'data' ];
571 }
572
573 return false;
574 }
575
576 /**
577 * Get chart from response data
578 *
579 * @param array $response_data Analytics response data
580 *
581 * @return array chart data
582 */
583 public static function get_chart( $response_data, $period_in_days = 7 ) {
584 $chart_data = array();
585 if ( ! empty( $response_data ) ) {
586 $data = ( ! empty( $response_data['reports'] ) && ! empty( $response_data['reports'][0] ) && ! empty( $response_data['reports'][0]['data'] ) ) ? $response_data['reports'][0]['data'] : array();
587 $rows = ( ! empty( $data['rows'] ) ) ? $data['rows'] : array();
588 if ( ! empty( $rows ) ) {
589 foreach ( $rows as $key => $row ) {
590 if ( $key < $period_in_days ) {
591 $chart_data[ $key ]['previous'] = ! empty( $row['metrics'][1]['values'][0] ) ? $row['metrics'][1]['values'][0] : 0;
592 $chart_data[ $key ]['previous-day'] = date( 'M j', strtotime( $row['dimensions'][0] ) );
593 } else {
594 $chart_data[ $key - $period_in_days ]['day'] = date( 'M j', strtotime( $row['dimensions'][0] ) );
595 $chart_data[ $key - $period_in_days ]['current'] = ! empty( $row['metrics'][0]['values'][0] ) ? $row['metrics'][0]['values'][0] : 0;
596 $chart_data['date'] = strtotime( $row['dimensions'][0] );
597 }
598 }
599 }
600 }
601
602 return $chart_data;
603 }
604
605 /**
606 * Get gender chart from response data
607 *
608 * @param array $response_data Analytics response data
609 *
610 * @return array chart data
611 */
612 public static function get_gender_chart( $response_data ) {
613 $chart_data = [];
614 if ( !empty( $response_data ) ) {
615 $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
616 $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
617 if ( !empty( $rows ) ) {
618 foreach ( $rows as $key => $row ) {
619 $chart_data[$row['dimensions'][0]] = self::get_metric_value($row['metrics']);
620 }
621 }
622 }
623
624 return $chart_data;
625 }
626
627 /**
628 * Get device chart from response data.
629 *
630 * @param array $response_data Analytics response data array.
631 *
632 * @return array Chart data array.
633 */
634 public static function get_device_chart( $response_data ) {
635 $chart_data = [];
636 if ( false === empty( $response_data ) ) {
637 $data = (false === empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
638 $rows = ( false === empty( $data['rows'] ) ) ? $data['rows'] : array();
639 if ( false === empty( $rows ) ) {
640 foreach ( $rows as $row ) {
641 $chart_data[$row['dimensions'][0]] = self::get_metric_value($row['metrics']);
642 }
643 }
644 }
645
646 return $chart_data;
647 }
648
649 /**
650 * Get the value of metric data response.
651 *
652 * @param $metrics
653 *
654 * @return mixed
655 */
656 private static function get_metric_value($metrics)
657 {
658 if (is_array($metrics)) {
659 foreach($metrics as $metric) {
660 $values[] = $metric['values'][0];
661 }
662 }
663
664 return $values[0];
665 }
666
667 /**
668 * Get gender chart from response data
669 *
670 * @param array $response_data Analytics response data
671 *
672 * @return array chart data
673 */
674 public static function get_age_chart( $response_data ) {
675 $chart_data = [];
676 if ( !empty( $response_data ) ) {
677 $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
678 $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
679 if ( !empty( $rows ) ) {
680 foreach ( $rows as $key => $row ) {
681 $chart_data[$row['dimensions'][0]] = self::get_metric_value($row['metrics']);
682 }
683 }
684 }
685
686 return $chart_data;
687 }
688
689 /**
690 * Get dasboard chart from response data
691 *
692 * @param array $response_data Analytics response data
693 *
694 * @return array dashboard chart data
695 */
696 public static function get_dashboard_chart( $response_data ) {
697 $chart_data = array();
698 if ( !empty( $response_data ) ) {
699 $data = (!empty( $response_data[ 'reports' ] ) && !empty( $response_data[ 'reports' ][ 0 ] ) && !empty( $response_data[ 'reports' ][ 0 ][ 'data' ] ) ) ? $response_data[ 'reports' ][ 0 ][ 'data' ] : array();
700 $rows = (!empty( $data[ 'rows' ] ) ) ? $data[ 'rows' ] : array();
701 if ( !empty( $rows ) ) {
702 foreach ( $rows as $row ) {
703 $chart_data[] = array(
704 'day' => date( 'M j', strtotime( $row[ 'dimensions' ][ 0 ] ) ),
705 'current' => !empty( $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] ) ? $row[ 'metrics' ][ 0 ][ 'values' ][ 0 ] : 0
706 );
707 }
708 }
709 }
710
711 return $chart_data;
712 }
713
714 /**
715 * Get boxes from response data
716 *
717 * @param array $data Analytics response data
718 *
719 * @return array boxes data
720 */
721 public static function get_boxes( $data ) {
722 if ( !empty( $data ) ) {
723 $data = self::get_reports_from_response( $data );
724 self::handle_more_reports( $data );
725 $report = self::get_single_report( $data );
726 self::get_report_column_header( $report );
727 $report_data = self::get_report_data( $report );
728 $totals = self::get_totals( $report_data );
729
730 return self::get_boxes_from_totals( $totals );
731 }
732 }
733
734 /**
735 * Get boxes from totals
736 *
737 * @param array $totals Analytics response totals
738 *
739 * @return array boxes data
740 */
741 public static function get_boxes_from_totals( $totals ) {
742 if ( !empty( $totals ) ) {
743 $boxes_data = array();
744 foreach ( $totals as $key => $total ) {
745 if ( $key == 0 ) {
746 $boxes_data[ 'Users' ][ 'current' ] = $total[ 'values' ][ 0 ];
747 $boxes_data[ 'Pageviews' ][ 'current' ] = $total[ 'values' ][ 1 ];
748 $boxes_data[ 'PageviewsPerSession' ][ 'current' ] = $total[ 'values' ][ 2 ];
749 $boxes_data[ 'BounceRate' ][ 'current' ] = round( $total[ 'values' ][ 3 ], 2 );
750 } else {
751 $boxes_data[ 'Users' ][ 'previous' ] = $total[ 'values' ][ 0 ];
752 $boxes_data[ 'Pageviews' ][ 'previous' ] = $total[ 'values' ][ 1 ];
753 $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] = $total[ 'values' ][ 2 ];
754 $boxes_data[ 'BounceRate' ][ 'previous' ] = round( $total[ 'values' ][ 3 ], 2 );
755 }
756 }
757
758 return self::prepare_boxes( $boxes_data );
759 }
760
761 return false;
762 }
763
764 /**
765 * Prepare boxes data
766 *
767 * @param array $boxes_data Boxes data
768 *
769 * @return array boxes data
770 */
771 public static function prepare_boxes( $boxes_data ) {
772 $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;
773 $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;
774 $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;
775 $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;
776 $boxes_data[ 'Users' ][ 'diff' ] = ( $boxes_data[ 'Users' ][ 'previous' ] == 0 && $boxes_data[ 'Users' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Users' ][ 'diff' ];
777 $boxes_data[ 'Pageviews' ][ 'diff' ] = ( $boxes_data[ 'Pageviews' ][ 'previous' ] == 0 && $boxes_data[ 'Pageviews' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'Pageviews' ][ 'diff' ];
778 $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ] == 0 && $boxes_data[ 'PageviewsPerSession' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'PageviewsPerSession' ][ 'diff' ];
779 $boxes_data[ 'BounceRate' ][ 'diff' ] = ( $boxes_data[ 'BounceRate' ][ 'previous' ] == 0 && $boxes_data[ 'BounceRate' ][ 'current' ] == 0 ) ? 0 : $boxes_data[ 'BounceRate' ][ 'diff' ];
780 $boxes_data[ 'Users' ][ 'label' ] = 'Users';
781 $boxes_data[ 'Pageviews' ][ 'label' ] = 'Pageviews';
782 $boxes_data[ 'PageviewsPerSession' ][ 'label' ] = 'Pages / Session';
783 $boxes_data[ 'BounceRate' ][ 'label' ] = 'Bounce Rate';
784 $boxes_data[ 'Users' ][ 'comparison' ] = $boxes_data[ 'Users' ][ 'current' ] . ' vs ' . $boxes_data[ 'Users' ][ 'previous' ];
785 $boxes_data[ 'Pageviews' ][ 'comparison' ] = $boxes_data[ 'Pageviews' ][ 'current' ] . ' vs ' . $boxes_data[ 'Pageviews' ][ 'previous' ];
786 $boxes_data[ 'PageviewsPerSession' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'current' ], 2, '.', ',' ) . ' vs ' . self::number_format_clean( $boxes_data[ 'PageviewsPerSession' ][ 'previous' ], 2, '.', ',' );
787 $boxes_data[ 'BounceRate' ][ 'comparison' ] = self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'current' ], 2, '.', ',' ) . '% vs ' . self::number_format_clean( $boxes_data[ 'BounceRate' ][ 'previous' ], 2, '.', ',' ) . '%';
788 $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] > 0 ) ? 'green' : 'red';
789 $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] > 0 ) ? 'green' : 'red';
790 $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] > 0 ) ? 'green' : 'red';
791 $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] > 0 ) ? 'red' : 'green';
792 $boxes_data[ 'Users' ][ 'color' ] = ( $boxes_data[ 'Users' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Users' ][ 'color' ] : 'black';
793 $boxes_data[ 'Pageviews' ][ 'color' ] = ( $boxes_data[ 'Pageviews' ][ 'diff' ] != 0 ) ? $boxes_data[ 'Pageviews' ][ 'color' ] : 'black';
794 $boxes_data[ 'PageviewsPerSession' ][ 'color' ] = ( $boxes_data[ 'PageviewsPerSession' ][ 'diff' ] != 0 ) ? $boxes_data[ 'PageviewsPerSession' ][ 'color' ] : 'black';
795 $boxes_data[ 'BounceRate' ][ 'color' ] = ( $boxes_data[ 'BounceRate' ][ 'diff' ] != 0 ) ? $boxes_data[ 'BounceRate' ][ 'color' ] : 'black';
796
797 return $boxes_data;
798 }
799
800 /**
801 * Number format for boxes
802 *
803 * @param float $number Number to format
804 * @param int $precision Precision
805 * @param string $dec_point Decimal point
806 * @param string $thousands_sep Thousands Separator
807 *
808 * @return string clean number format
809 */
810 public static function number_format_clean( $number, $precision = 0, $dec_point = '.', $thousands_sep = ',' ) {
811 if ( $number == 0 ) {
812 return 0;
813 } else {
814 $format = number_format( $number, $precision, $dec_point, $thousands_sep );
815 if ( substr( $format, 2 ) == '.00' ) {
816 return substr( $format, 0, - 3 );
817 }
818
819 return $format;
820 }
821 }
822
823 /**
824 * Get sources from analytics response data
825 *
826 * @param array $data Analytics response data
827 *
828 * @return array|bool sources data
829 */
830 public static function get_sources( $data ) {
831 if ( !empty( $data ) ) {
832 $data = self::get_reports_from_response( $data );
833 self::handle_more_reports( $data );
834 $report = self::get_single_report( $data );
835 self::get_report_column_header( $report );
836 $report_data = self::get_report_data( $report );
837 $rows = self::get_rows( $report_data );
838 $totals = self::get_totals( $report_data );
839 $totalCount = array();
840 if ( !empty( $totals ) ) {
841 foreach ( $totals as $key => $total ) {
842 $totalCount = $total[ 'values' ][ 0 ];
843 }
844 }
845 $sources = array(
846 'total' => $totalCount,
847 'sum' => 0,
848 'rows' => array(),
849 );
850 if ( !empty( $rows ) ) {
851 $i = 1;
852 foreach ( $rows as $row ) {
853 if ( !empty( $row ) ) {
854 foreach ( $row as $key => $value ) {
855 if ( $key == 'dimensions' ) {
856 $sources[ 'rows' ][ $i ][ 'name' ] = $value[ 0 ];
857 $sources[ 'rows' ][ $i ][ 'url' ] = $value[ 0 ];
858 } elseif ( $key == 'metrics' ) {
859 $sources[ 'rows' ][ $i ][ 'number' ] = $value[ 0 ][ 'values' ][ 0 ];
860 $sources[ 'rows' ][ $i ][ 'percent' ] = (!empty( $totalCount ) ) ? round( $value[ 0 ][ 'values' ][ 0 ] / $totalCount * 100, 2 ) : 0;
861 $sources[ 'sum' ] += $value[ 0 ][ 'values' ][ 0 ];
862 }
863 }
864 $i ++;
865 }
866 }
867 }
868
869 return $sources;
870 }
871
872 return false;
873 }
874
875 /**
876 * Get dashboard boxes data from analytics response data
877 *
878 * @param array $data Analytics response data
879 *
880 * @return array dashboard boxes data
881 */
882 public static function get_dashboard_boxes_data( $data ) {
883 if ( !empty( $data ) ) {
884 $data = self::get_reports_from_response( $data );
885 self::handle_more_reports( $data );
886 $report = self::get_single_report( $data );
887 self::get_report_column_header( $report );
888 $report_data = self::get_report_data( $report );
889 $totals = self::get_totals( $report_data );
890 $boxes_data = array();
891 $boxes_data[ 'Sessions' ] = array(
892 'label' => 'Visits',
893 'value' => $totals[ 0 ][ 'values' ][ 0 ],
894 );
895 $boxes_data[ 'Pageviews' ] = array(
896 'label' => 'Pageviews',
897 'value' => $totals[ 0 ][ 'values' ][ 1 ],
898 );
899 $boxes_data[ 'pageviewsPerSession' ] = array(
900 'label' => 'Pages / Visit',
901 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 2 ], 2, '.', ',' ),
902 );
903 $boxes_data[ 'BounceRate' ] = array(
904 'label' => 'Bounce Rate',
905 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 3 ], 2, '.', ',' ) . '%',
906 );
907 $boxes_data[ 'avgTimeOnPage' ] = array(
908 'label' => 'Avg. Time on Site',
909 'value' => gmdate( "H:i:s", $totals[ 0 ][ 'values' ][ 4 ] ),
910 );
911 $boxes_data[ 'percentNewSessions' ] = array(
912 'label' => '% of New Visits',
913 'value' => self::number_format_clean( $totals[ 0 ][ 'values' ][ 5 ], 2, '.', ',' ),
914 );
915
916 return $boxes_data;
917 }
918 }
919
920 /**
921 * Get Empty Boxes Structure.
922 *
923 * @return array Array of empty boxes structure values.
924 */
925 public static function get_empty_boxes_structure() {
926 $boxes_data = array();
927 $boxes_data[ 'Sessions' ] = array(
928 'label' => 'Visits',
929 'value' => 0,
930 );
931 $boxes_data[ 'Pageviews' ] = array(
932 'label' => 'Pageviews',
933 'value' => 0,
934 );
935 $boxes_data[ 'pageviewsPerSession' ] = array(
936 'label' => 'Pages / Visit',
937 'value' => self::number_format_clean( 0, 2, '.', ',' ),
938 );
939 $boxes_data[ 'BounceRate' ] = array(
940 'label' => 'Bounce Rate',
941 'value' => self::number_format_clean( 0, 2, '.', ',' ) . '%',
942 );
943 $boxes_data[ 'avgTimeOnPage' ] = array(
944 'label' => 'Avg. Time on Site',
945 'value' => gmdate( "H:i:s", 0 ),
946 );
947 $boxes_data[ 'percentNewSessions' ] = array(
948 'label' => '% of New Visits',
949 'value' => self::number_format_clean( 0, 2, '.', ',' ),
950 );
951
952 return $boxes_data;
953 }
954 }
955