diff --git a/includes/Analytics/AnalyticsManager.php b/includes/Analytics/AnalyticsManager.php index 55d91b9..c900721 100644 --- a/includes/Analytics/AnalyticsManager.php +++ b/includes/Analytics/AnalyticsManager.php @@ -13,6 +13,20 @@ * The AnalyticsManager class. */ class AnalyticsManager { + /** + * The events table. + * + * @var string + */ + const EVENTS_TABLE = 'omniform_stats_events'; + + /** + * The visitors table. + * + * @var string + */ + const VISITOR_TABLE = 'omniform_stats_visitors'; + /** * The QueryBuilder instance. * @@ -74,7 +88,7 @@ protected function get_visitor_hash() { protected function record_event( int $form_id, int $event_type ) { $query_builder = $this->query_builder_factory->create(); - $query_builder->table( AnalyticsServiceProvider::EVENTS_TABLE ) + $query_builder->table( self::EVENTS_TABLE ) ->insert( array( 'form_id' => $form_id, @@ -93,13 +107,13 @@ protected function record_event( int $form_id, int $event_type ) { protected function get_visitor_id() { $query_builder = $this->query_builder_factory->create(); - $visitor_results = $query_builder->table( AnalyticsServiceProvider::VISITOR_TABLE ) + $visitor_results = $query_builder->table( self::VISITOR_TABLE ) ->select( 'visitor_id' ) ->where( 'visitor_hash', '=', $this->get_visitor_hash() ) ->get(); if ( empty( $visitor_results ) ) { - $query_builder->table( AnalyticsServiceProvider::VISITOR_TABLE ) + $query_builder->table( self::VISITOR_TABLE ) ->insert( array( 'visitor_hash' => $this->get_visitor_hash(), @@ -150,7 +164,7 @@ public function record_submission_failure( int $form_id ) { public function get_impression_count( int $form_id, bool $unique = false ) { $query_builder = $this->query_builder_factory->create(); - return $query_builder->table( AnalyticsServiceProvider::EVENTS_TABLE ) + return $query_builder->table( self::EVENTS_TABLE ) ->where( 'form_id', '=', $form_id ) ->where( 'event_type', '=', EventType::IMPRESSION ) ->count( $unique ? 'DISTINCT visitor_id' : 'event_id' ); @@ -167,7 +181,7 @@ public function get_impression_count( int $form_id, bool $unique = false ) { public function get_submission_count( int $form_id, bool $unique = false ) { $query_builder = $this->query_builder_factory->create(); - return $query_builder->table( AnalyticsServiceProvider::EVENTS_TABLE ) + return $query_builder->table( self::EVENTS_TABLE ) ->where( 'form_id', '=', $form_id ) ->where( 'event_type', '=', EventType::SUBMISSION_SUCCESS ) ->count( $unique ? 'DISTINCT visitor_id' : 'event_id' ); @@ -184,7 +198,7 @@ public function get_submission_count( int $form_id, bool $unique = false ) { public function get_failed_submission_count( int $form_id, bool $unique = false ) { $query_builder = $this->query_builder_factory->create(); - return $query_builder->table( AnalyticsServiceProvider::EVENTS_TABLE ) + return $query_builder->table( self::EVENTS_TABLE ) ->where( 'form_id', '=', $form_id ) ->where( 'event_type', '=', EventType::SUBMISSION_FAILURE ) ->count( $unique ? 'DISTINCT visitor_id' : 'event_id' ); @@ -212,7 +226,7 @@ public function get_conversion_rate( int $form_id ) { public function delete_form_data( int $form_id ) { $query_builder = $this->query_builder_factory->create(); - $query_builder->table( AnalyticsServiceProvider::EVENTS_TABLE ) + $query_builder->table( self::EVENTS_TABLE ) ->where( 'form_id', '=', $form_id ) ->delete(); } diff --git a/includes/Analytics/AnalyticsServiceProvider.php b/includes/Analytics/AnalyticsServiceProvider.php index 3579445..7c6589a 100644 --- a/includes/Analytics/AnalyticsServiceProvider.php +++ b/includes/Analytics/AnalyticsServiceProvider.php @@ -23,9 +23,6 @@ class AnalyticsServiceProvider extends AbstractServiceProvider implements Bootab */ const DB_VERSION = 1; - const EVENTS_TABLE = 'omniform_stats_events'; - const VISITOR_TABLE = 'omniform_stats_visitors'; - /** * Get the services provided by the provider. * @@ -104,8 +101,8 @@ public function activate() { 'INDEX (`event_type`)', ); - if ( ! Schema::has_table( self::EVENTS_TABLE ) ) { - Schema::create( self::EVENTS_TABLE, $events_table_definition ); + if ( ! Schema::has_table( AnalyticsManager::EVENTS_TABLE ) ) { + Schema::create( AnalyticsManager::EVENTS_TABLE, $events_table_definition ); } $visitors_table_definition = array( @@ -114,8 +111,8 @@ public function activate() { 'PRIMARY KEY (`visitor_id`)', ); - if ( ! Schema::has_table( self::VISITOR_TABLE ) ) { - Schema::create( self::VISITOR_TABLE, $visitors_table_definition ); + if ( ! Schema::has_table( AnalyticsManager::VISITOR_TABLE ) ) { + Schema::create( AnalyticsManager::VISITOR_TABLE, $visitors_table_definition ); } }