| 1 |
<?php |
| 2 |
|
| 3 |
class EasyOptInsActivity { |
| 4 |
private static $instance; |
| 5 |
|
| 6 |
public $settings; |
| 7 |
|
| 8 |
private $text; |
| 9 |
private $table_name; |
| 10 |
private $form_stats; |
| 11 |
private $daily_stats; |
| 12 |
|
| 13 |
public static function get_instance() { |
| 14 |
if ( ! self::$instance ) { |
| 15 |
self::$instance = new self; |
| 16 |
} |
| 17 |
|
| 18 |
return self::$instance; |
| 19 |
} |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
$this->table_name = $wpdb->prefix . "fca_eoi_activity"; |
| 25 |
|
| 26 |
$this->text = array( |
| 27 |
'impressions' => array( |
| 28 |
'total' => __( 'Total Impressions' ), |
| 29 |
'form' => __( 'Form Impressions' ) |
| 30 |
), |
| 31 |
'conversions' => array( |
| 32 |
'total' => __( 'Total Conversions' ), |
| 33 |
'form' => __( 'Form Conversions' ) |
| 34 |
), |
| 35 |
'conversion_rate' => array( |
| 36 |
'total' => __( 'Conversion Rate' ), |
| 37 |
'form' => __( 'Conversion Rate' ), |
| 38 |
), |
| 39 |
'period' => __( 'Last %d days' ), |
| 40 |
'all_time' => __( 'All time' ) |
| 41 |
); |
| 42 |
|
| 43 |
if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) { |
| 44 |
add_filter( 'request', array( $this, 'track_activity' ) ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function get_text( $name, $category = null, $parameters = array() ) { |
| 49 |
$plain_text = $this->text[ $name ]; |
| 50 |
$text = $category ? $plain_text[ $category ] : $plain_text; |
| 51 |
|
| 52 |
if ( ! empty( $parameters ) ) { |
| 53 |
$text = call_user_func_array( 'sprintf', array_merge( array( $text ), $parameters ) ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( $name == 'period' && empty( $parameters[0] ) ) { |
| 57 |
$text = $this->text['all_time']; |
| 58 |
} |
| 59 |
|
| 60 |
return $text; |
| 61 |
} |
| 62 |
|
| 63 |
public function setup() { |
| 64 |
$sql = "CREATE TABLE $this->table_name ( |
| 65 |
form_id INT(11) NOT NULL, |
| 66 |
type ENUM('impression', 'conversion') NOT NULL, |
| 67 |
timestamp TIMESTAMP NOT NULL, |
| 68 |
day DATE NOT NULL, |
| 69 |
KEY form_id (form_id), |
| 70 |
KEY timestamp (timestamp), |
| 71 |
KEY day (day) |
| 72 |
);"; |
| 73 |
|
| 74 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 75 |
dbDelta( $sql ); |
| 76 |
} |
| 77 |
|
| 78 |
public function get_tracking_code( $form_id, $escape = true ) { |
| 79 |
ob_start(); |
| 80 |
?> |
| 81 |
jQuery.post( <?php echo json_encode( trailingslashit( get_home_url() ) ) ?>, { |
| 82 |
'fca_eoi_track_form_id': <?php echo $escape ? json_encode( $form_id ) : $form_id ?> |
| 83 |
} ); |
| 84 |
<?php |
| 85 |
return ob_get_clean(); |
| 86 |
} |
| 87 |
|
| 88 |
public function track_activity( $request ) { |
| 89 |
if ( empty( $_REQUEST['fca_eoi_track_form_id'] ) ) { |
| 90 |
return $request; |
| 91 |
} |
| 92 |
|
| 93 |
require_once FCA_EOI_PLUGIN_DIR . 'includes/classes/RobotDetector/RobotDetector.php'; |
| 94 |
|
| 95 |
$form_id = (int) $_REQUEST['fca_eoi_track_form_id']; |
| 96 |
if ( get_post($form_id) ) { |
| 97 |
$robot_detector = new RobotDetector(); |
| 98 |
if ( ! $robot_detector->is_robot() ) { |
| 99 |
if ( is_user_logged_in() ) { |
| 100 |
/* USER IS LOGGED IN , DON'T COUNT AS IMPRESSION */ |
| 101 |
}else { |
| 102 |
$this->add_impression( $form_id ); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
exit; |
| 109 |
|
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public function format_column_text( $column_name, $value ) { |
| 114 |
if ( $column_name == 'conversion_rate' ) { |
| 115 |
return number_format( $value * 100, 2 ) . '%'; |
| 116 |
} else { |
| 117 |
return $value; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function get_daily_stats( $day_interval ) { |
| 122 |
if ( empty( $this->daily_stats ) ) { |
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
$days = array(); |
| 126 |
$stats = array( |
| 127 |
'impressions' => array(), |
| 128 |
'conversions' => array(), |
| 129 |
'totals' => array( |
| 130 |
'impressions' => 0, |
| 131 |
'conversions' => 0, |
| 132 |
'conversion_rate' => 0 |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
$now = time(); |
| 137 |
for ( $i = $day_interval - 1; $i >= 0; $i -- ) { |
| 138 |
$time = $now - ( 86400 * $i ); |
| 139 |
$day = date( 'Y-m-d', $time ); |
| 140 |
$days[] = $day; |
| 141 |
|
| 142 |
$stats['impressions'][ $day ] = 0; |
| 143 |
$stats['conversions'][ $day ] = 0; |
| 144 |
} |
| 145 |
|
| 146 |
foreach ( array( 'impression', 'conversion' ) as $activity_type ) { |
| 147 |
$query = $this->get_daily_stats_query( $activity_type, $day_interval ); |
| 148 |
foreach ( $wpdb->get_results( $query ) as $result ) { |
| 149 |
$activity_type_plural = $activity_type . 's'; |
| 150 |
|
| 151 |
if ( ! array_key_exists( $result->day, $stats[ $activity_type_plural ] ) ) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
$total = (int) $result->total; |
| 156 |
|
| 157 |
$stats[ $activity_type_plural ][ $result->day ] = $total; |
| 158 |
$stats['totals'][ $activity_type_plural ] += $total; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$stats['totals']['conversion_rate'] = $this->calculate_conversion_rate( |
| 163 |
$stats['totals']['impressions'], |
| 164 |
$stats['totals']['conversions'] |
| 165 |
); |
| 166 |
|
| 167 |
$this->daily_stats = $stats; |
| 168 |
} |
| 169 |
|
| 170 |
return $this->daily_stats; |
| 171 |
} |
| 172 |
|
| 173 |
public function get_form_stats( $day_interval ) { |
| 174 |
if ( empty( $this->form_stats ) ) { |
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
$stats = array(); |
| 178 |
$ids = array(); |
| 179 |
|
| 180 |
foreach ( array( 'impression', 'conversion' ) as $activity_type ) { |
| 181 |
$stat = array(); |
| 182 |
$query = $this->get_form_stats_query( $activity_type, $day_interval ); |
| 183 |
foreach ( $wpdb->get_results( $query ) as $result ) { |
| 184 |
$ids[ $result->form_id ] = true; |
| 185 |
$stat[ $result->form_id ] = (int) $result->total; |
| 186 |
} |
| 187 |
$stats[ $activity_type . 's' ] = $stat; |
| 188 |
} |
| 189 |
|
| 190 |
foreach ( array_keys( $ids ) as $form_id ) { |
| 191 |
$stats['conversion_rate'][ $form_id ] = $this->calculate_conversion_rate( |
| 192 |
floatval( empty( $stats['impressions'][ $form_id ] ) ? 0 : $stats['impressions'][ $form_id ] ), |
| 193 |
floatval( empty( $stats['conversions'][ $form_id ] ) ? 0 : $stats['conversions'][ $form_id ] ) |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
$this->form_stats = $stats; |
| 198 |
} |
| 199 |
|
| 200 |
return $this->form_stats; |
| 201 |
} |
| 202 |
|
| 203 |
private function calculate_conversion_rate( $impressions, $conversions ) { |
| 204 |
if ( $impressions < 1 ) { |
| 205 |
return 0.0; |
| 206 |
} else { |
| 207 |
return $conversions / $impressions; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public function add_impression( $form_id ) { |
| 212 |
$this->add_activity( $form_id, 'impression' ); |
| 213 |
} |
| 214 |
|
| 215 |
public function add_conversion( $form_id ) { |
| 216 |
$this->add_activity( $form_id, 'conversion' ); |
| 217 |
} |
| 218 |
|
| 219 |
public function reset_stats( $form_id ) { |
| 220 |
global $wpdb; |
| 221 |
|
| 222 |
$wpdb->delete( $this->table_name, array( 'form_id' => $form_id ), '%d' ); |
| 223 |
} |
| 224 |
|
| 225 |
private function add_activity( $form_id, $activity_type ) { |
| 226 |
|
| 227 |
if ( !defined ( 'FCA_EOI_DISABLE_STATS_TRACKING' )) { |
| 228 |
|
| 229 |
global $wpdb; |
| 230 |
|
| 231 |
$time = current_time( 'mysql', 1 ); |
| 232 |
$wpdb->insert( $this->table_name, array( |
| 233 |
'form_id' => $form_id, |
| 234 |
'type' => $activity_type, |
| 235 |
'timestamp' => $time, |
| 236 |
'day' => $time |
| 237 |
), array( '%d', '%s', '%s', '%s' ) ); |
| 238 |
|
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
private function get_form_stats_query( $activity_type, $day_interval ) { |
| 243 |
return $this->get_stats_query( 'form_id', $activity_type, $day_interval ); |
| 244 |
} |
| 245 |
|
| 246 |
private function get_daily_stats_query( $activity_type, $day_interval ) { |
| 247 |
return $this->get_stats_query( 'day', $activity_type, $day_interval ); |
| 248 |
} |
| 249 |
|
| 250 |
private function get_stats_query( $field, $activity_type, $day_interval ) { |
| 251 |
global $wpdb; |
| 252 |
|
| 253 |
return $wpdb->prepare( |
| 254 |
"SELECT `$field`, COUNT(*) AS `total` " . |
| 255 |
"FROM `$this->table_name` " . |
| 256 |
"WHERE " . |
| 257 |
"`type` = %s " . |
| 258 |
( $day_interval |
| 259 |
? "AND `timestamp` >= DATE_SUB(NOW(), INTERVAL $day_interval DAY) " |
| 260 |
: "") . |
| 261 |
"GROUP BY `$field`", $activity_type ); |
| 262 |
} |
| 263 |
} |
| 264 |
|