Yoast_Googleanalytics_Reporting

Class Yoast_Googleanalytics_Reporting.

Defined (1)

The class is defined in the following location(s).

/admin/class-googleanalytics-reporting.php  
  1. class Yoast_Googleanalytics_Reporting { 
  2.  
  3. /** 
  4. * Store this instance 
  5. * @var null 
  6. */ 
  7. private static $instance = null; 
  8.  
  9. /** 
  10. * Getting the instance object 
  11. * This method will return the instance of itself, if instance not exists, becauses of it's called for the first 
  12. * time, the instance will be created. 
  13. * @return null|Yoast_Google_Analytics 
  14. */ 
  15. public static function get_instance() { 
  16. if ( is_null( self::$instance ) ) { 
  17. self::$instance = new self(); 
  18.  
  19. return self::$instance; 
  20.  
  21. /** 
  22. * Format a response 
  23. * @param array $raw_data 
  24. * @param string $store_as 
  25. * @param string $start_date 
  26. * @param string $end_date 
  27. * @return array 
  28. */ 
  29. public function parse_response( $raw_data, $store_as, $start_date, $end_date ) { 
  30. $data = array(); 
  31.  
  32. if ( $store_as == 'datelist' ) { 
  33. $data_tmp = $this->date_range( strtotime( $start_date ), strtotime( $end_date ) ); 
  34. $data = array_keys( $data_tmp ); 
  35.  
  36. if ( isset( $raw_data['body']['rows'] ) && is_array( $raw_data['body']['rows'] ) ) { 
  37. foreach ( $raw_data['body']['rows'] as $key => $item ) { 
  38. if ( $store_as == 'datelist' ) { 
  39. $data[ (int) $this->format_ga_date( $item[0] ) ] = $this->parse_row( $item ); 
  40. else { 
  41. $data[] = $this->parse_data_row( $item ); 
  42.  
  43. if ( $store_as == 'datelist' ) { 
  44. $data = $this->check_validity_data( $data ); 
  45.  
  46. return $data; 
  47.  
  48. /** 
  49. * Check the key on valid unix timestamps and remove invalid keys 
  50. * @param array $data 
  51. * @return array 
  52. */ 
  53. private function check_validity_data( $data = array() ) { 
  54. foreach ( $data as $key => $value ) { 
  55. if ( strlen( $key ) <= 5 ) { 
  56. unset( $data[ $key ] ); 
  57.  
  58. return $data; 
  59.  
  60. /** 
  61. * Format the GA date value 
  62. * @param string $date 
  63. * @return int 
  64. */ 
  65. private function format_ga_date( $date ) { 
  66. $year = substr( $date, 0, 4 ); 
  67. $month = substr( $date, 4, 2 ); 
  68. $day = substr( $date, 6, 2 ); 
  69.  
  70. return strtotime( $year . '-' . $month . '-' . $day ); 
  71.  
  72. /** 
  73. * Parse a row and return an array with the correct data rows 
  74. * @param array $item 
  75. * @return array 
  76. */ 
  77. private function parse_row( $item ) { 
  78. if ( isset( $item[2] ) ) { 
  79. return array( 
  80. 'date' => (int) $this->format_ga_date( $item[0] ),  
  81. 'value' => (string) $item[1],  
  82. 'total' => (int) $item[2],  
  83. ); 
  84.  
  85. return (int) $item[1]; 
  86.  
  87. /** 
  88. * Parse a row for the list storage type 
  89. * @param array $item 
  90. * @return array 
  91. */ 
  92. private function parse_data_row( $item ) { 
  93. return array( 
  94. 'name' => (string) $item[0],  
  95. 'value' => (int) $item[1],  
  96. ); 
  97.  
  98. /** 
  99. * Calculate the date range between 2 dates 
  100. * @param string $current 
  101. * @param string $last 
  102. * @param string $step 
  103. * @param string $format 
  104. * @return array 
  105. */ 
  106. private function date_range( $current, $last, $step = '+1 day', $format = 'Y-m-d' ) { 
  107. $dates = array(); 
  108.  
  109. while ( $current <= $last ) { 
  110. $dates[] = date( $format, $current ); 
  111. $current = strtotime( $step, $current ); 
  112.  
  113. return $dates; 
  114.