Yoast_GA_Dashboards

Dashboards class.

Defined (1)

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

/admin/dashboards/class-admin-dashboards.php  
  1. class Yoast_GA_Dashboards { 
  2.  
  3. /** 
  4. * @var Yoast_GA_Dashboards_Collector $aggregator Store the data aggregator 
  5. */ 
  6. public $aggregator; 
  7.  
  8. /** 
  9. * @var array $data Store the Data instance 
  10. */ 
  11. public $data; 
  12.  
  13. /** 
  14. * @var array $active_metrics Store the active metrics 
  15. */ 
  16. public $active_metrics; 
  17.  
  18. /** 
  19. * Store the valid metrics which are available in the Google API, more can be added 
  20. * @var array 
  21. * @link https://ga-dev-tools.appspot.com/explorer/ 
  22. */ 
  23. private $valid_metrics = array( 'sessions', 'bounces', 'users', 'newUsers', 'percentNewSessions', 'bounceRate', 'sessionDuration', 'avgSessionDuration', 'hits' ); 
  24.  
  25. /** 
  26. * Store this instance 
  27. * @var null 
  28. */ 
  29. private static $instance = null; 
  30.  
  31. /** 
  32. * @var boolean $dashboards_disabled Store the Dashboards disabled bool 
  33. */ 
  34. private $dashboards_disabled; 
  35.  
  36. /** 
  37. * Construct on the dashboards class for GA 
  38. */ 
  39. protected function __construct() { 
  40. add_filter( 'ga_extend_dashboards', array( $this, 'extend_dashboards' ), 10, 1 ); 
  41.  
  42. $this->dashboards_disabled = Yoast_GA_Settings::get_instance()->dashboards_disabled(); 
  43.  
  44. /** 
  45. * Init the dashboards 
  46. * @param integer $ga_profile_id 
  47. */ 
  48. public function init_dashboards( $ga_profile_id ) { 
  49. if ( ! $this->dashboards_disabled ) { 
  50. $dashboards = $this->get_default_dashboards(); 
  51.  
  52. $this->extend_dashboards( $dashboards ); 
  53.  
  54. // Register the active metrics 
  55. $register = array_keys( $dashboards ); 
  56.  
  57. $this->aggregator = new Yoast_GA_Dashboards_Collector( $ga_profile_id, $register, $this->valid_metrics ); 
  58.  
  59. $this->register( $register ); 
  60.  
  61. /** 
  62. * Adding dashboards for front-end 
  63. * By hook as filter: $dashboards = apply_filters( 'ga_extend_dashboards', $dashboards); 
  64. * @param array $dashboards 
  65. * @return mixed 
  66. */ 
  67. public function extend_dashboards( $dashboards ) { 
  68. // Initialize the dashboard graphs 
  69. Yoast_GA_Dashboards_Display::get_instance()->add_dashboards( $dashboards ); 
  70.  
  71. return $dashboards; 
  72.  
  73. /** 
  74. * Get the instance 
  75. * @return Yoast_GA_Dashboards 
  76. */ 
  77. public static function get_instance() { 
  78. if ( is_null( self::$instance ) ) { 
  79. self::$instance = new self(); 
  80.  
  81. return self::$instance; 
  82.  
  83. /** 
  84. * Register the dashboard types 
  85. * @param array|string $types 
  86. * @return bool 
  87. */ 
  88. public function register( $types ) { 
  89. if ( is_array( $types ) == false ) { 
  90. $types = array( $types ); 
  91.  
  92. if ( is_array( $types ) && count( $types ) >= 1 ) { 
  93. if ( $this->validate_dashboard_types( $types ) ) { 
  94. $this->active_metrics = $types; 
  95.  
  96. return true; 
  97.  
  98. return false; 
  99.  
  100. /** 
  101. * Adding translations to ga-admin-dashboard 
  102. */ 
  103. public function add_dashboard_js_translations() { 
  104. // Now we can localize the script with our data. 
  105. $translation_array = array( 
  106. // For datatables 
  107. 'sort_ascending' => __( ': activate to sort column ascending', 'google-analytics-for-wordpress' ),  
  108. 'sort_descending' => __( ': activate to sort column descending', 'google-analytics-for-wordpress' ),  
  109. 'empty_table' => __( 'No data available', 'google-analytics-for-wordpress' ),  
  110. 'info' => _x( 'Showing _START_ to _END_ of _TOTAL_ rows', '_START_, _END_ and _TOTAL_ will be replaced by JS (See: http://datatables.net/reference/option/language.info)', 'google-analytics-for-wordpress' ),  
  111. 'info_empty' => __( 'No rows to show', 'google-analytics-for-wordpress' ),  
  112. 'info_filtered' => _x( '(filtered from _MAX_ total rows)', '_MAX_ will be replaced by JS (See: http://datatables.net/reference/option/language.infoFiltered)', 'google-analytics-for-wordpress' ),  
  113. 'length_menu' => _x( 'Show _MENU_ rows', '_MAX_ will be replaced by JS', 'google-analytics-for-wordpress' ),  
  114. 'loading_records' => __( 'Loading...', 'google-analytics-for-wordpress' ),  
  115. 'pagination_first' => __( 'First', 'google-analytics-for-wordpress' ),  
  116. 'pagination_last' => __( 'Last', 'google-analytics-for-wordpress' ),  
  117. 'pagination_next' => __( 'Next', 'google-analytics-for-wordpress' ),  
  118. 'pagination_previous' => __( 'Previous', 'google-analytics-for-wordpress' ),  
  119. 'processing' => __( 'Processing...', 'google-analytics-for-wordpress' ),  
  120. 'search_placeholder' => __( 'Search', 'google-analytics-for-wordpress' ),  
  121. 'zero_records' => __( 'No matching records found', 'google-analytics-for-wordpress' ),  
  122.  
  123. // For dimensions 
  124. 'dimensions' => __( 'Reports', 'google-analytics-for-wordpress' ),  
  125. 'custom_dimensions' => __( 'Custom dimension reports', 'google-analytics-for-wordpress' ),  
  126. ); 
  127.  
  128. wp_localize_script( 'ga-admin-dashboard', 'dashboard_translate', $translation_array ); 
  129.  
  130. /** 
  131. * Reset all dashboards data by removing the options of the registered dashboards 
  132. * @return bool 
  133. */ 
  134. public function reset_dashboards_data() { 
  135. if ( ! $this->dashboards_disabled ) { 
  136. $dashboards = $this->get_default_dashboards(); 
  137.  
  138. if ( is_array( $dashboards ) && count( $dashboards ) >= 1 ) { 
  139. foreach ( $dashboards as $name => $board ) { 
  140. Yoast_GA_Dashboards_Data::reset( $name ); 
  141.  
  142. // Make sure we fetch new data if we enable the dashboards by updating the last_run option 
  143. update_option( 'yst_ga_last_wp_run', date( 'Y-m-d', strtotime( '-2 days' ) ) ); 
  144.  
  145. return true; 
  146.  
  147. return false; 
  148.  
  149. /** 
  150. * Get the defaults dashboard array to register 
  151. * @return array 
  152. */ 
  153. private function get_default_dashboards() { 
  154. return array( 
  155. 'sessions' => array( 
  156. 'title' => __( 'Sessions', 'google-analytics-for-wordpress' ),  
  157. 'help' => __( 'A session is a group of interactions that take place on your website within a given time frame. For example a single session can contain multiple screen or page views, events, social interactions, and ecommerce transactions. <a href="http://yoa.st/gasessions" target="_blank">[Learn more]</a>', 'google-analytics-for-wordpress' ),  
  158. 'type' => 'graph',  
  159. 'tab' => 'general',  
  160. ),  
  161. 'bounceRate' => array( 
  162. 'title' => __( 'Bounce rate', 'google-analytics-for-wordpress' ),  
  163. 'help' => __( 'Bounce Rate is the percentage of single-page sessions (i.e. sessions in which the person left your site from the entrance page without interacting with the page). <a href="http://yoa.st/gabounce" target="_blank">[Learn more]</a>', 'google-analytics-for-wordpress' ),  
  164. 'data-percent' => true,  
  165. 'type' => 'graph',  
  166. 'tab' => 'general',  
  167. ),  
  168. 'source' => array( 
  169. 'title' => __( 'Traffic sources', 'google-analytics-for-wordpress' ),  
  170. 'help' => __( 'Every referral to a web site has an origin, or (traffic) source. Possible sources include: *google* (the name of a search engine), *facebook.com* (the name of a referring site), *spring_newsletter* (the name of one of your newsletters), and *direct* (users that typed your URL directly into their browser, or who had bookmarked your site). <a href="http://yoa.st/gabnce" target="_blank">[Learn more]</a>', 'google-analytics-for-wordpress' ),  
  171. 'type' => 'table',  
  172. 'columns' => array( 
  173. __( 'Sessions', 'google-analytics-for-wordpress' ) 
  174. ),  
  175. 'tab' => 'dimensions',  
  176. ),  
  177. 'top_pageviews' => array( 
  178. 'title' => __( 'Popular pages', 'google-analytics-for-wordpress' ),  
  179. 'help' => __( 'Pages by url.', 'google-analytics-for-wordpress' ),  
  180. 'type' => 'table',  
  181. 'columns' => array( 
  182. __( 'Sessions', 'google-analytics-for-wordpress' ) 
  183. ),  
  184. 'tab' => 'dimensions',  
  185. ),  
  186. 'top_countries' => array( 
  187. 'title' => __( 'Countries', 'google-analytics-for-wordpress' ),  
  188. 'help' => __( 'The country or territory from which visits originated. <a href="http://yoa.st/gacountry" target="_blank">[Learn more]</a>', 'google-analytics-for-wordpress' ),  
  189. 'type' => 'table',  
  190. 'columns' => array( 
  191. __( 'Sessions', 'google-analytics-for-wordpress' ) 
  192. ),  
  193. 'tab' => 'dimensions',  
  194. ),  
  195. ); 
  196.  
  197. /** 
  198. * Validate the registered types of dashboards 
  199. * @param array $types 
  200. * @return bool 
  201. */ 
  202. private function validate_dashboard_types( $types ) { 
  203. $valid = true; 
  204.  
  205. if ( is_array( $types ) ) { 
  206. foreach ( $types as $check_type ) { 
  207. if ( ! in_array( $check_type, $this->valid_metrics ) ) { 
  208. $valid = false; 
  209.  
  210. return $valid;