nt_key ] ); } } return $properties; } public function maybe_track( $event, $properties = array(), $limit_per_day = 0 ) { if ( $this->should_track( $event, $properties, $limit_per_day ) ) { $this->track( $event, $properties ); } } private function should_track( $event, $properties, $limit_per_day ) { if ( ! $this->settings->get( 'usage' ) ) { return false; } if ( $limit_per_day < 1 ) { return true; } return ! $this->is_event_limit_reached( $event, $properties, $limit_per_day ); } private function is_event_limit_reached( $event, $properties, $limit_per_day ) { $event_count_key = $this->get_event_count_key( $event, $properties ); $event_count = (int) get_transient( $event_count_key, 0 ); if ( $event_count >= $limit_per_day ) { return true; } set_transient( $event_count_key, $event_count + 1, DAY_IN_SECONDS ); return false; } private function get_event_count_key( $event, $properties ) { if ( method_exists( $this, "get_event_count_key_$event" ) ) { return call_user_func( array( $this, "get_event_count_key_$event" ), $event, $properties ); } else { return sprintf( self::EVENT_COUNT_KEY, $event ); } } public function track( $event, $properties = array() ) { $debug_mode = defined( 'WP_SMUSH_MIXPANEL_DEBUG' ) && WP_SMUSH_MIXPANEL_DEBUG; if ( $debug_mode ) { Helper::logger()->track()->info( sprintf( 'Track Event %1$s: %2$s', $event, print_r( $properties, true ) ) ); } else { $this->get_analytics()->track( $event, $properties ); } } public function maybe_track_error( $type, $code, $message ) { $limit_per_day = 1; $this->maybe_track( 'smush_error_encountered', array( 'Error Type' => $type, 'Error Code' => $code, 'Error Message' => $message, ), $limit_per_day ); } protected function get_event_count_key_smush_error_encountered( $event, $properties ) { $event_key = $event; $error_type = $this->array_utils->get_array_value( $properties, 'Error Type' ); $error_code = $this->array_utils->get_array_value( $properties, 'Error Code' ); if ( ! empty( $error_type ) && ! empty( $error_code ) ) { $event_key = $error_type . '_' . $error_code; } return sprintf( self::EVENT_COUNT_KEY, sanitize_key( $event_key ) ); } }