Skip to content

Commit

Permalink
Improved stats tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBakovic committed Nov 3, 2017
1 parent 4a98fed commit 4047bd5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- MyArcadePlugin Lite -
* Tweak - Improved stats tracking

v5.4.0 - 2017-09-08 - Based on MyArcadePlugin Pro 5.30.0
* New - GameDistribution feed integration (http://www.gamedistribution.com/games/)
Expand Down
38 changes: 26 additions & 12 deletions includes/class-myarcade-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,62 @@ class MyArcade_Stats {
/**
* Get the play count for a time period
*
* @version 5.30.0
* @version 5.31.0
* @since 5.30.0
* @static
* @access public
* @param string $time_period Number of days or 'total'
* @param boolean $daily True if daily stats over the given period should be returned
* @return integer Play count
* @param string $time_period Number of days or 'total'
* @param integer $min_duration It allows to count games by minimal play duration in seconds
* @return integer Play count
*/
public static function get_plays( $time_period ) {
public static function get_plays( $time_period, $min_duration = 0 ) {
global $wpdb;

$result = 0;
$query = '';

if ( $min_duration ) {
$duration_query = "AND WHERE duration >= '".intval( $min_duration )."'";
}
else {
$duration_query = "";
}

switch ( $time_period ) {

case 'today': {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date()."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date()."' {$duration_query}";
} break;

case 'yesterday': {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date( '-1' )."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date( '-1' )."' {$duration_query}";
} break;

case 'week': {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-7' )."' AND '".self::get_date()."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-7' )."' AND '".self::get_date()."' {$duration_query}";
} break;

case 'month': {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-30' )."' AND '".self::get_date()."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-30' )."' AND '".self::get_date()."' {$duration_query}";
} break;

case 'year': {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-365' )."' AND '".self::get_date()."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) BETWEEN '".self::get_date( '-365' )."' AND '".self::get_date()."' {$duration_query}";
} break;

case 'total': {
$result = get_option( 'myarcade_site_plays' );
return intval( get_option( 'myarcade_site_plays' ) );
} break;

default: {
$result = $wpdb->get_var( "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date( $time_period )."'" );
$query = "SELECT COUNT(1) FROM {$wpdb->prefix}myarcade_plays WHERE DATE_FORMAT( `date`, '%Y-%m-%d' ) = '".self::get_date( $time_period )."' {$duration_query}";
} break;
}

if ( $query ) {
$result = $wpdb->get_var( $query );
}

return intval( $result );
}

Expand Down
29 changes: 18 additions & 11 deletions includes/class-myarcade-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ private static function get_last_send_time() {
/**
* Get all the tracking data
*
* @version 5.30.0
* @version 5.31.0
* @since 5.30.0
* @static
* @access private
* @access protected
* @return array Array of tracking data
*/
private static function get_tracking_data() {
protected static function get_tracking_data() {

$data = array();

Expand Down Expand Up @@ -106,8 +106,12 @@ private static function get_tracking_data() {
// Get game plays so we can show a top list of MyArcadePlugin sites
$data['total_plays'] = self::get_total_plays();

// All game plays
$data['plays'] = self::get_plays();

// Games with minium play duration of 30 seconds
$data['plays_duration'] = self::get_plays(30);

return $data;
}

Expand Down Expand Up @@ -140,18 +144,20 @@ private static function get_theme_info() {
* MyArcadePlugin memory usage and to know which older WP versions
* we still need to support.
*
* @version 5.30.0
* @version 5.31.0
* @since 5.30.0
* @static
* @access private
* @return array
*/
private static function get_wordpress_info() {

$wp_data = array();
$wp_data['locale'] = get_locale();
$wp_data['version'] = get_bloginfo( 'version' );
$wp_data['multisite'] = is_multisite() ? 'Yes' : 'No';
$wp_data = array(
'name' => get_bloginfo( 'name' ),
'locale' => get_locale(),
'version' => get_bloginfo( 'version' ),
'multisite' => is_multisite() ? 'Yes' : 'No',
);

return $wp_data;
}
Expand Down Expand Up @@ -255,18 +261,19 @@ private static function get_total_plays() {
/**
* Get plays by date
*
* @version 5.30.0
* @version 5.31.0
* @since 5.30.0
* @static
* @access private
* @param int $min_duration Time in seconds (minimal play duration)
* @return array Array of dates and play count
*/
private static function get_plays() {
private static function get_plays( $min_duration = 0 ) {

// We only want to collect plays history to be able to create a top list of myarcadeplugin sites
$plays = array(
'date' => MyArcade_Stats::get_date( '-1' ),
'impressions' => MyArcade_Stats::get_plays( 'yesterday' )
'impressions' => MyArcade_Stats::get_plays( 'yesterday', $min_duration )
);

return $plays;
Expand Down

0 comments on commit 4047bd5

Please sign in to comment.