PluginProbe
GetResponse Forms by Optin Cat / 1.3.4
GetResponse Forms by Optin Cat v1.3.4
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / eoi-activity.php

eoi-activity.php in GetResponse Forms by Optin Cat 1.3.4, at includes/eoi-activity.php

247 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_filter( 'request', array( $this, 'track_activity' ) );
44 }
45
46 public function get_text( $name, $category = null, $parameters = array() ) {
47 $plain_text = $this->text[ $name ];
48 $text = $category ? $plain_text[ $category ] : $plain_text;
49
50 if ( ! empty( $parameters ) ) {
51 $text = call_user_func_array( 'sprintf', array_merge( array( $text ), $parameters ) );
52 }
53
54 if ( $name == 'period' && empty( $parameters[0] ) ) {
55 $text = $this->text['all_time'];
56 }
57
58 return $text;
59 }
60
61 public function setup() {
62 $sql = "CREATE TABLE $this->table_name (
63 form_id INT(11) NOT NULL,
64 type ENUM('impression', 'conversion') NOT NULL,
65 timestamp TIMESTAMP NOT NULL,
66 day DATE NOT NULL,
67 KEY form_id (form_id),
68 KEY timestamp (timestamp),
69 KEY day (day)
70 );";
71
72 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
73 dbDelta( $sql );
74 }
75
76 public function get_tracking_code( $form_id, $escape = true ) {
77 ob_start();
78 ?>
79 jQuery.post( <?php echo json_encode( trailingslashit( get_home_url() ) ) ?>, {
80 'fca_eoi_track_form_id': <?php echo $escape ? json_encode( $form_id ) : $form_id ?>
81 } );
82 <?php
83 return ob_get_clean();
84 }
85
86 public function track_activity( $request ) {
87 if ( empty( $_REQUEST['fca_eoi_track_form_id'] ) ) {
88 return $request;
89 }
90
91 require_once $this->settings['plugin_dir'] . 'includes/classes/RobotDetector/RobotDetector.php';
92
93 $robot_detector = new RobotDetector();
94 if ( ! $robot_detector->is_robot() ) {
95 $this->add_impression( (int) $_REQUEST['fca_eoi_track_form_id'] );
96 }
97
98 exit;
99 }
100
101 public function format_column_text( $column_name, $value ) {
102 if ( $column_name == 'conversion_rate' ) {
103 return number_format( $value * 100, 2 ) . '%';
104 } else {
105 return $value;
106 }
107 }
108
109 public function get_daily_stats( $day_interval ) {
110 if ( empty( $this->daily_stats ) ) {
111 global $wpdb;
112
113 $days = array();
114 $stats = array(
115 'impressions' => array(),
116 'conversions' => array(),
117 'totals' => array(
118 'impressions' => 0,
119 'conversions' => 0,
120 'conversion_rate' => 0
121 )
122 );
123
124 $now = time();
125 for ( $i = $day_interval - 1; $i >= 0; $i -- ) {
126 $time = $now - ( 86400 * $i );
127 $day = date( 'Y-m-d', $time );
128 $days[] = $day;
129
130 $stats['impressions'][ $day ] = 0;
131 $stats['conversions'][ $day ] = 0;
132 }
133
134 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
135 $query = $this->get_daily_stats_query( $activity_type, $day_interval );
136 foreach ( $wpdb->get_results( $query ) as $result ) {
137 $activity_type_plural = $activity_type . 's';
138
139 if ( ! array_key_exists( $result->day, $stats[ $activity_type_plural ] ) ) {
140 continue;
141 }
142
143 $total = (int) $result->total;
144
145 $stats[ $activity_type_plural ][ $result->day ] = $total;
146 $stats['totals'][ $activity_type_plural ] += $total;
147 }
148 }
149
150 $stats['totals']['conversion_rate'] = $this->calculate_conversion_rate(
151 $stats['totals']['impressions'],
152 $stats['totals']['conversions']
153 );
154
155 $this->daily_stats = $stats;
156 }
157
158 return $this->daily_stats;
159 }
160
161 public function get_form_stats( $day_interval ) {
162 if ( empty( $this->form_stats ) ) {
163 global $wpdb;
164
165 $stats = array();
166 $ids = array();
167
168 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
169 $stat = array();
170 $query = $this->get_form_stats_query( $activity_type, $day_interval );
171 foreach ( $wpdb->get_results( $query ) as $result ) {
172 $ids[ $result->form_id ] = true;
173 $stat[ $result->form_id ] = (int) $result->total;
174 }
175 $stats[ $activity_type . 's' ] = $stat;
176 }
177
178 foreach ( array_keys( $ids ) as $form_id ) {
179 $stats['conversion_rate'][ $form_id ] = $this->calculate_conversion_rate(
180 floatval( empty( $stats['impressions'][ $form_id ] ) ? 0 : $stats['impressions'][ $form_id ] ),
181 floatval( empty( $stats['conversions'][ $form_id ] ) ? 0 : $stats['conversions'][ $form_id ] )
182 );
183 }
184
185 $this->form_stats = $stats;
186 }
187
188 return $this->form_stats;
189 }
190
191 private function calculate_conversion_rate( $impressions, $conversions ) {
192 if ( $impressions < 1 ) {
193 return 0.0;
194 } else {
195 return $conversions / $impressions;
196 }
197 }
198
199 public function add_impression( $form_id ) {
200 $this->add_activity( $form_id, 'impression' );
201 }
202
203 public function add_conversion( $form_id ) {
204 $this->add_activity( $form_id, 'conversion' );
205 }
206
207 public function reset_stats( $form_id ) {
208 global $wpdb;
209
210 $wpdb->delete( $this->table_name, array( 'form_id' => $form_id ), '%d' );
211 }
212
213 private function add_activity( $form_id, $activity_type ) {
214 global $wpdb;
215
216 $time = current_time( 'mysql', 1 );
217 $wpdb->insert( $this->table_name, array(
218 'form_id' => $form_id,
219 'type' => $activity_type,
220 'timestamp' => $time,
221 'day' => $time
222 ), array( '%d', '%s', '%s', '%s' ) );
223 }
224
225 private function get_form_stats_query( $activity_type, $day_interval ) {
226 return $this->get_stats_query( 'form_id', $activity_type, $day_interval );
227 }
228
229 private function get_daily_stats_query( $activity_type, $day_interval ) {
230 return $this->get_stats_query( 'day', $activity_type, $day_interval );
231 }
232
233 private function get_stats_query( $field, $activity_type, $day_interval ) {
234 global $wpdb;
235
236 return $wpdb->prepare(
237 "SELECT `$field`, COUNT(*) AS `total` " .
238 "FROM `$this->table_name` " .
239 "WHERE " .
240 "`type` = %s " .
241 ( $day_interval
242 ? "AND `timestamp` >= DATE_SUB(NOW(), INTERVAL $day_interval DAY) "
243 : "") .
244 "GROUP BY `$field`", $activity_type );
245 }
246 }
247