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