site_id = $this->get_site_id(); $this->api_url = $this->get_api_url(); $this->enabled = $this->is_analytics_enabled(); } /** * Get unique site ID */ private function get_site_id() { $site_id = get_option( 'apf_site_id' ); if ( ! $site_id ) { $site_id = 'apf_' . md5( get_site_url() . time() ); update_option( 'apf_site_id', $site_id ); } return $site_id; } /** * Get analytics API URL */ private function get_api_url() { // Default to localhost server $default_url = 'https://analyticsajaxfilter.rf.gd/api/analytics.php'; return get_option( 'apf_analytics_url', $default_url ); } /** * Check if analytics is enabled */ private function is_analytics_enabled() { return (bool) get_option( 'asfp_analytics_enabled', true ); } /** * Generate nonce for API requests */ private function generate_nonce() { return md5( $this->site_id . date( 'Y-m-d' ) . 'apf_secret' ); } /** * Get client IP address */ private function get_client_ip() { $ip = ''; if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { $ip = $_SERVER['REMOTE_ADDR']; } return sanitize_text_field( $ip ); } /** * Send data to analytics server */ private function send_data( $data ) { if ( ! $this->enabled ) { return false; } // Add common fields $data['site_id'] = $this->site_id; $data['nonce'] = $this->generate_nonce(); $data['timestamp'] = current_time( 'mysql' ); $data['ip_address'] = $this->get_client_ip(); $data['user_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : ''; // Send async request wp_remote_post( $this->api_url, [ 'timeout' => 5, 'blocking' => false, 'body' => wp_json_encode( $data ), 'headers' => [ 'Content-Type' => 'application/json', ], ] ); return true; } /** * Track plugin activation */ public function track_activation() { global $wp_version; $data = [ 'event_type' => 'plugin_activation', 'site_url' => get_site_url(), 'wp_version' => $wp_version, 'php_version' => phpversion(), 'plugin_version' => ASFP_VERSION, ]; $this->send_data( $data ); } /** * Track filter usage (frontend user actions) */ public function track_filter_usage( $filter_set_id, $filters_used, $template, $post_type, $search_term = '', $sort = '' ) { $data = [ 'event_type' => 'filter_usage', 'usage_type' => 'frontend_user_action', // User actually using filters 'set_id' => $filter_set_id, 'template' => $template, 'post_type' => $post_type, 'filters_used' => $filters_used, 'search_term' => $search_term, 'sort' => $sort, ]; $this->send_data( $data ); } /** * Track autocomplete usage (frontend user action) */ public function track_autocomplete_usage( $filter_set_id, $search_term, $post_type ) { $data = [ 'event_type' => 'autocomplete_usage', 'usage_type' => 'frontend_user_action', // User typing in search 'set_id' => $filter_set_id, 'search_term' => $search_term, 'search_length' => strlen( $search_term ), 'post_type' => $post_type, ]; $this->send_data( $data ); } /** * Track shortcode render */ public function track_shortcode_render( $filter_set_id, $template, $post_type ) { $data = [ 'event_type' => 'shortcode_render', 'set_id' => $filter_set_id, 'template' => $template, 'post_type' => $post_type, 'page_url' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', ]; $this->send_data( $data ); } /** * Track error */ public function track_error( $error_message, $context = [] ) { $data = [ 'event_type' => 'error', 'error_message' => $error_message, 'context' => wp_json_encode( $context ), 'page_url' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', ]; $this->send_data( $data ); } /** * Track admin settings change (admin configuration) */ public function track_admin_settings_change( $filter_set_id, $settings ) { $data = [ 'event_type' => 'admin_settings_change', 'usage_type' => 'admin_configuration', // Admin configuring settings 'set_id' => $filter_set_id, 'settings' => $settings, 'page_url' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', ]; $this->send_data( $data ); } /** * Track tab click (frontend user action - actual click) */ public function track_tab_click( $filter_set_id, $taxonomy, $term_id, $term_name ) { $data = [ 'event_type' => 'tab_click', 'usage_type' => 'frontend_user_action', // User actually clicking a tab 'set_id' => $filter_set_id, 'taxonomy' => $taxonomy, 'term_id' => $term_id, 'term_name' => $term_name, 'action_name' => 'tab_click_' . $taxonomy, // Specific action name ]; $this->send_data( $data ); } /** * Track template selection (for Template Popularity chart) */ public function track_custom_template_usage( $filter_set_id, $template_type, $has_custom_html ) { $data = [ 'event_type' => 'template_selection', 'usage_type' => 'template_usage', // For Template Popularity, not admin config 'set_id' => $filter_set_id, 'template_type' => $template_type, 'has_custom_html' => $has_custom_html, ]; $this->send_data( $data ); } /** * Track filter design usage (for Filter Template Analytics) */ public function track_filter_design_usage( $filter_set_id, $filter_design ) { $data = [ 'event_type' => 'filter_design_selection', 'usage_type' => 'filter_template_usage', // Separate from admin config and blog templates 'set_id' => $filter_set_id, 'filter_design' => $filter_design, ]; $this->send_data( $data ); } }