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