Statistics for Form Subscriptions
- added statistics_forms table - added corresponding model to record stats - record stats whenever someone subscribes via a form
This commit is contained in:
@ -86,6 +86,7 @@ class Initializer {
|
||||
$statistics_clicks = Env::$db_prefix . 'statistics_clicks';
|
||||
$statistics_opens = Env::$db_prefix . 'statistics_opens';
|
||||
$statistics_unsubscribes = Env::$db_prefix . 'statistics_unsubscribes';
|
||||
$statistics_forms = Env::$db_prefix . 'statistics_forms';
|
||||
|
||||
define('MP_SETTINGS_TABLE', $settings);
|
||||
define('MP_SEGMENTS_TABLE', $segments);
|
||||
@ -105,6 +106,7 @@ class Initializer {
|
||||
define('MP_STATISTICS_CLICKS_TABLE', $statistics_clicks);
|
||||
define('MP_STATISTICS_OPENS_TABLE', $statistics_opens);
|
||||
define('MP_STATISTICS_UNSUBSCRIBES_TABLE', $statistics_unsubscribes);
|
||||
define('MP_STATISTICS_FORMS_TABLE', $statistics_forms);
|
||||
}
|
||||
|
||||
function runMigrator() {
|
||||
|
@ -27,7 +27,8 @@ class Migrator {
|
||||
'statistics_newsletters',
|
||||
'statistics_clicks',
|
||||
'statistics_opens',
|
||||
'statistics_unsubscribes'
|
||||
'statistics_unsubscribes',
|
||||
'statistics_forms'
|
||||
);
|
||||
}
|
||||
|
||||
@ -318,6 +319,19 @@ class Migrator {
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
function statistics_forms() {
|
||||
$attributes = array(
|
||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||
'form_id mediumint(9) NOT NULL,',
|
||||
'count mediumint(9) NOT NULL DEFAULT 0,',
|
||||
'date DATE NOT NULL,',
|
||||
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||
'PRIMARY KEY (id),',
|
||||
'UNIQUE KEY form_date (form_id,date)'
|
||||
);
|
||||
return $this->sqlify(__FUNCTION__, $attributes);
|
||||
}
|
||||
|
||||
private function sqlify($model, $attributes) {
|
||||
$table = $this->prefix . $model;
|
||||
|
||||
|
36
lib/Models/StatisticsForms.php
Normal file
36
lib/Models/StatisticsForms.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace MailPoet\Models;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class StatisticsForms extends Model {
|
||||
public static $_table = MP_STATISTICS_FORMS_TABLE;
|
||||
|
||||
function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public static function record($form_id) {
|
||||
if($form_id > 0) {
|
||||
$today = date('Y-m-d');
|
||||
|
||||
// check if we already have a record for today
|
||||
$record = self::where('form_id', $form_id)
|
||||
->where('date', $today)
|
||||
->findOne();
|
||||
|
||||
if($record !== false) {
|
||||
$record->setExpr('count', 'count + 1')->save();
|
||||
} else {
|
||||
// create a new entry
|
||||
$record = self::create();
|
||||
$record->hydrate(array(
|
||||
'form_id' => $form_id,
|
||||
'date' => $today,
|
||||
'count' => 1
|
||||
));
|
||||
$record->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ use MailPoet\Models\SubscriberCustomField;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\Util\Url;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
@ -92,6 +93,11 @@ class Subscribers {
|
||||
$errors = $subscriber->getErrors();
|
||||
$result = ($errors === false && $subscriber->id() > 0);
|
||||
|
||||
// record form statistics
|
||||
if($result === true && $form !== false && $form->id > 0) {
|
||||
StatisticsForms::record($form->id);
|
||||
}
|
||||
|
||||
// get success message to display after subscription
|
||||
$form_settings = (
|
||||
isset($form->settings)
|
||||
|
Reference in New Issue
Block a user