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