PluginProbe
GetResponse Forms by Optin Cat / 1.4.1
GetResponse Forms by Optin Cat v1.4.1
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.4.1, at includes/eoi-activity.php

255 lines 6.4 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 $form_id = (int) $_REQUEST['fca_eoi_track_form_id'];
94 if ( get_post($form_id) ) {
95 $robot_detector = new RobotDetector();
96 if ( ! $robot_detector->is_robot() ) {
97 if ( is_user_logged_in() ) {
98 /* USER IS LOGGED IN , DON'T COUNT AS IMPRESSION */
99 }else {
100 $this->add_impression( $form_id );
101 }
102
103 }
104 }
105
106 exit;
107 }
108
109 public function format_column_text( $column_name, $value ) {
110 if ( $column_name == 'conversion_rate' ) {
111 return number_format( $value * 100, 2 ) . '%';
112 } else {
113 return $value;
114 }
115 }
116
117 public function get_daily_stats( $day_interval ) {
118 if ( empty( $this->daily_stats ) ) {
119 global $wpdb;
120
121 $days = array();
122 $stats = array(
123 'impressions' => array(),
124 'conversions' => array(),
125 'totals' => array(
126 'impressions' => 0,
127 'conversions' => 0,
128 'conversion_rate' => 0
129 )
130 );
131
132 $now = time();
133 for ( $i = $day_interval - 1; $i >= 0; $i -- ) {
134 $time = $now - ( 86400 * $i );
135 $day = date( 'Y-m-d', $time );
136 $days[] = $day;
137
138 $stats['impressions'][ $day ] = 0;
139 $stats['conversions'][ $day ] = 0;
140 }
141
142 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
143 $query = $this->get_daily_stats_query( $activity_type, $day_interval );
144 foreach ( $wpdb->get_results( $query ) as $result ) {
145 $activity_type_plural = $activity_type . 's';
146
147 if ( ! array_key_exists( $result->day, $stats[ $activity_type_plural ] ) ) {
148 continue;
149 }
150
151 $total = (int) $result->total;
152
153 $stats[ $activity_type_plural ][ $result->day ] = $total;
154 $stats['totals'][ $activity_type_plural ] += $total;
155 }
156 }
157
158 $stats['totals']['conversion_rate'] = $this->calculate_conversion_rate(
159 $stats['totals']['impressions'],
160 $stats['totals']['conversions']
161 );
162
163 $this->daily_stats = $stats;
164 }
165
166 return $this->daily_stats;
167 }
168
169 public function get_form_stats( $day_interval ) {
170 if ( empty( $this->form_stats ) ) {
171 global $wpdb;
172
173 $stats = array();
174 $ids = array();
175
176 foreach ( array( 'impression', 'conversion' ) as $activity_type ) {
177 $stat = array();
178 $query = $this->get_form_stats_query( $activity_type, $day_interval );
179 foreach ( $wpdb->get_results( $query ) as $result ) {
180 $ids[ $result->form_id ] = true;
181 $stat[ $result->form_id ] = (int) $result->total;
182 }
183 $stats[ $activity_type . 's' ] = $stat;
184 }
185
186 foreach ( array_keys( $ids ) as $form_id ) {
187 $stats['conversion_rate'][ $form_id ] = $this->calculate_conversion_rate(
188 floatval( empty( $stats['impressions'][ $form_id ] ) ? 0 : $stats['impressions'][ $form_id ] ),
189 floatval( empty( $stats['conversions'][ $form_id ] ) ? 0 : $stats['conversions'][ $form_id ] )
190 );
191 }
192
193 $this->form_stats = $stats;
194 }
195
196 return $this->form_stats;
197 }
198
199 private function calculate_conversion_rate( $impressions, $conversions ) {
200 if ( $impressions < 1 ) {
201 return 0.0;
202 } else {
203 return $conversions / $impressions;
204 }
205 }
206
207 public function add_impression( $form_id ) {
208 $this->add_activity( $form_id, 'impression' );
209 }
210
211 public function add_conversion( $form_id ) {
212 $this->add_activity( $form_id, 'conversion' );
213 }
214
215 public function reset_stats( $form_id ) {
216 global $wpdb;
217
218 $wpdb->delete( $this->table_name, array( 'form_id' => $form_id ), '%d' );
219 }
220
221 private function add_activity( $form_id, $activity_type ) {
222 global $wpdb;
223
224 $time = current_time( 'mysql', 1 );
225 $wpdb->insert( $this->table_name, array(
226 'form_id' => $form_id,
227 'type' => $activity_type,
228 'timestamp' => $time,
229 'day' => $time
230 ), array( '%d', '%s', '%s', '%s' ) );
231 }
232
233 private function get_form_stats_query( $activity_type, $day_interval ) {
234 return $this->get_stats_query( 'form_id', $activity_type, $day_interval );
235 }
236
237 private function get_daily_stats_query( $activity_type, $day_interval ) {
238 return $this->get_stats_query( 'day', $activity_type, $day_interval );
239 }
240
241 private function get_stats_query( $field, $activity_type, $day_interval ) {
242 global $wpdb;
243
244 return $wpdb->prepare(
245 "SELECT `$field`, COUNT(*) AS `total` " .
246 "FROM `$this->table_name` " .
247 "WHERE " .
248 "`type` = %s " .
249 ( $day_interval
250 ? "AND `timestamp` >= DATE_SUB(NOW(), INTERVAL $day_interval DAY) "
251 : "") .
252 "GROUP BY `$field`", $activity_type );
253 }
254 }
255