Skip to content

Commit

Permalink
Move table name const to AnalyticsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtashjian committed Jun 24, 2024
1 parent 1fdb58d commit b55f84d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
28 changes: 21 additions & 7 deletions includes/Analytics/AnalyticsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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' );
Expand All @@ -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' );
Expand All @@ -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' );
Expand Down Expand Up @@ -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();
}
Expand Down
11 changes: 4 additions & 7 deletions includes/Analytics/AnalyticsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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(
Expand All @@ -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 );
}
}

Expand Down

0 comments on commit b55f84d

Please sign in to comment.