MonsterInsights_Tracking

Usage tracking.

Defined (1)

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

/includes/admin/tracking.php  
  1. class MonsterInsights_Tracking { 
  2.  
  3. /** 
  4. * The data to send to the EDD site 
  5. * @access private 
  6. */ 
  7. private $data; 
  8.  
  9. /** 
  10. * Get things going 
  11. * @access public 
  12. */ 
  13. public function __construct() { 
  14.  
  15. add_action( 'init', array( $this, 'schedule_send' ) ); 
  16. add_action( 'monsterinsights_settings_save_general_end', array( $this, 'check_for_settings_optin' ) ); 
  17. add_action( 'admin_head', array( $this, 'check_for_optin' ) ); 
  18. add_action( 'admin_head', array( $this, 'check_for_optout' ) ); 
  19. add_action( 'admin_notices', array( $this, 'monsterinsights_admin_notice' ) ); 
  20. add_filter( 'cron_schedules', array( $this, 'add_schedules' ) ); 
  21. add_action( 'monsterinsights_daily_cron', array( $this, 'send_checkin' ) ); 
  22.  
  23. /** 
  24. * Check if the user has opted into tracking 
  25. * @access private 
  26. * @return bool 
  27. */ 
  28. private function tracking_allowed() { 
  29. return (bool) monsterinsights_get_option( 'anonymous_data', false ); 
  30.  
  31. /** 
  32. * Setup the data that is going to be tracked 
  33. * @access private 
  34. * @return void 
  35. */ 
  36. private function setup_data() { 
  37. $data = array(); 
  38.  
  39. // Retrieve current theme info 
  40. $theme_data = wp_get_theme(); 
  41. $theme = $theme_data->Name . ' ' . $theme_data->Version; 
  42. $tracking_mode = monsterinsights_get_option( 'tracking_mode', 'analytics' ); 
  43. $events_mode = monsterinsights_get_option( 'events_mode', 'none' ); 
  44.  
  45. if ( $tracking_mode === false ) { 
  46. $tracking_mode = 'analytics'; 
  47.  
  48. if ( $events_mode === false ) { 
  49. $events_mode = 'none'; 
  50.  
  51. $data['php_version'] = phpversion(); 
  52. $data['mi_version'] = MONSTERINSIGHTS_VERSION; 
  53. $data['wp_version'] = get_bloginfo( 'version' ); 
  54. $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : ''; 
  55. $data['over_time'] = get_option( 'monsterinsights_over_time', array() ); 
  56. $data['multisite'] = is_multisite(); 
  57. $data['url'] = home_url(); 
  58. $data['theme'] = $theme; 
  59. $data['email'] = get_bloginfo( 'admin_email' ); 
  60. $data['key'] = monsterinsights_get_license(); 
  61. $data['sas'] = monsterinsights_get_shareasale_id(); 
  62. $data['setttings'] = monsterinsights_get_options(); 
  63. $data['tracking_mode'] = $tracking_mode; 
  64. $data['events_mode'] = $events_mode; 
  65.  
  66. // Retrieve current plugin information 
  67. if( ! function_exists( 'get_plugins' ) ) { 
  68. include ABSPATH . '/wp-admin/includes/plugin.php'; 
  69.  
  70. $plugins = array_keys( get_plugins() ); 
  71. $active_plugins = get_option( 'active_plugins', array() ); 
  72.  
  73. foreach ( $plugins as $key => $plugin ) { 
  74. if ( in_array( $plugin, $active_plugins ) ) { 
  75. // Remove active plugins from list so we can show active and inactive separately 
  76. unset( $plugins[ $key ] ); 
  77.  
  78. $data['active_plugins'] = $active_plugins; 
  79. $data['inactive_plugins'] = $plugins; 
  80. $data['locale'] = get_locale(); 
  81.  
  82. $this->data = $data; 
  83.  
  84. /** 
  85. * Send the data to the EDD server 
  86. * @access private 
  87. * @return void 
  88. */ 
  89. public function send_checkin( $override = false, $ignore_last_checkin = false ) { 
  90.  
  91. if( ! $this->tracking_allowed() && ! $override ) { 
  92. return false; 
  93.  
  94. // Send a maximum of once per day 
  95. $last_send = $this->get_last_send(); 
  96. if( is_numeric( $last_send ) && $last_send > strtotime( '-1 day' ) && ! $ignore_last_checkin ) { 
  97. return false; 
  98.  
  99. $this->setup_data(); 
  100.  
  101. $request = wp_remote_post( 'https://www.monsterinsights.com/?edd_action=checkin', array( 
  102. 'method' => 'POST',  
  103. 'timeout' => 20,  
  104. 'redirection' => 5,  
  105. 'httpversion' => '1.1',  
  106. 'blocking' => true,  
  107. 'body' => $this->data,  
  108. 'user-agent' => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . get_bloginfo( 'url' ) 
  109. ) ); 
  110.  
  111. if( is_wp_error( $request ) ) { 
  112. return $request; 
  113.  
  114. update_option( 'mi_tracking_last_send', time() ); 
  115.  
  116. return true; 
  117.  
  118.  
  119. /** 
  120. * Check for a new opt-in on settings save 
  121. * This runs during the sanitation of General settings, thus the return 
  122. * @access public 
  123. * @return array 
  124. */ 
  125. public function check_for_settings_optin() { 
  126. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  127. return; 
  128.  
  129. // Send an intial check in on settings save 
  130. $anonymous_data = isset( $_POST['anonymous_data'] ) ? 1 : 0; 
  131. if ( $anonymous_data ) { 
  132. $this->send_checkin( true ); 
  133.  
  134.  
  135. /** 
  136. * Check for a new opt-in via the admin notice 
  137. * @access public 
  138. * @return void 
  139. */ 
  140. public function check_for_optin() { 
  141. if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_into_tracking' === $_REQUEST['mi_action'] ) ) { 
  142. return; 
  143.  
  144. if ( monsterinsights_get_option( 'anonymous_data', false ) ) { 
  145. return; 
  146.  
  147. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  148. return; 
  149.  
  150. monsterinsights_update_option( 'anonymous_data', 1 ); 
  151. $this->send_checkin( true ); 
  152. update_option( 'monsterinsights_tracking_notice', 1 ); 
  153.  
  154. /** 
  155. * Check for a new opt-in via the admin notice 
  156. * @access public 
  157. * @return void 
  158. */ 
  159. public function check_for_optout() { 
  160. if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_out_of_tracking' === $_REQUEST['mi_action'] ) ) { 
  161. return; 
  162.  
  163. if ( monsterinsights_get_option( 'anonymous_data', false ) ) { 
  164. return; 
  165.  
  166. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  167. return; 
  168.  
  169. monsterinsights_update_option( 'anonymous_data', 0 ); 
  170. update_option( 'monsterinsights_tracking_notice', 1 ); 
  171.  
  172. /** 
  173. * Get the last time a checkin was sent 
  174. * @access private 
  175. * @return false|string 
  176. */ 
  177. private function get_last_send() { 
  178. return get_option( 'monsterinsights_tracking_last_send' ); 
  179.  
  180. /** 
  181. * Schedule a weekly checkin 
  182. * @access public 
  183. * @return void 
  184. */ 
  185. public function schedule_send() { 
  186. // We send once a day (while tracking is allowed) to check in, which can be used to determine active sites 
  187. if ( ! wp_next_scheduled( 'monsterinsights_daily_cron' ) ) { 
  188. // Set the next event of fetching data 
  189. wp_schedule_event( strtotime( date( 'Y-m-d', strtotime( 'tomorrow' ) ) . ' 00:01:00 ' ), 'daily', 'monsterinsights_daily_cron' ); 
  190.  
  191. /** 
  192. * Display the admin notice to users that have not opted-in or out 
  193. * @access public 
  194. * @return void 
  195. */ 
  196. public function monsterinsights_admin_notice() { 
  197.  
  198. $hide_notice = get_option( 'monsterinsights_tracking_notice' ); 
  199.  
  200. if ( $hide_notice ) { 
  201. return; 
  202.  
  203. if ( monsterinsights_get_option( 'anonymous_data', false ) ) { 
  204. return; 
  205.  
  206. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { 
  207. return; 
  208.  
  209. if ( 
  210. stristr( network_site_url( '/' ), 'dev' ) !== false || 
  211. stristr( network_site_url( '/' ), 'localhost' ) !== false || 
  212. stristr( network_site_url( '/' ), ':8888' ) !== false // This is common with MAMP on OS X 
  213. ) { 
  214. update_option( 'monsterinsights_tracking_notice', '1' ); 
  215. } else { 
  216. $optin_url = add_query_arg( 'mi_action', 'opt_into_tracking' ); 
  217. $optout_url = add_query_arg( 'mi_action', 'opt_out_of_tracking' ); 
  218. echo '<div class="updated"><p>'; 
  219. echo esc_html__( 'Allow MonsterInsights to track plugin usage? Opt-in to tracking and our newsletter to stay informed of the latest changes to MonsterInsights and help us ensure compatibility.', 'google-analytics-for-wordpress' ); 
  220. echo ' <a href="' . esc_url( $optin_url ) . '" class="button-secondary">' . __( 'Allow', 'google-analytics-for-wordpress' ) . '</a>'; 
  221. echo ' <a href="' . esc_url( $optout_url ) . '" class="button-secondary">' . __( 'Do not allow', 'google-analytics-for-wordpress' ) . '</a>'; 
  222. echo '</p></div>'; 
  223.  
  224. /** 
  225. * Registers new cron schedules 
  226. * @since 6.0.0 
  227. * @param array $schedules 
  228. * @return array 
  229. */ 
  230. public function add_schedules( $schedules = array() ) { 
  231. // Adds once weekly to the existing schedules. 
  232. $schedules['weekly'] = array( 
  233. 'interval' => 604800,  
  234. 'display' => __( 'Once Weekly', 'google-analytics-for-wordpress' ) 
  235. ); 
  236. return $schedules; 
  237.