get_charset_collate(); $p = $wpdb->prefix . 'eistat_'; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // --- Sessions --- dbDelta("CREATE TABLE {$p}sessions ( session_hash CHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, ip_hash CHAR(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '', date DATE NOT NULL, first_seen DATETIME NOT NULL, last_seen DATETIME NOT NULL, entry_page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', entry_page_id BIGINT UNSIGNED NOT NULL DEFAULT 0, entry_page_url VARCHAR(255) NOT NULL DEFAULT '', exit_page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', exit_page_id BIGINT UNSIGNED NOT NULL DEFAULT 0, exit_page_url VARCHAR(255) NOT NULL DEFAULT '', pageviews SMALLINT UNSIGNED NOT NULL DEFAULT 0, unique_pages SMALLINT UNSIGNED NOT NULL DEFAULT 0, total_time INT UNSIGNED NOT NULL DEFAULT 0, pages_json TEXT NULL, source ENUM('direct','google','yandex','bing','social','referral','internal','other') NOT NULL DEFAULT 'direct', source_detail VARCHAR(50) NOT NULL DEFAULT '', referrer_host VARCHAR(100) NOT NULL DEFAULT '', utm_source VARCHAR(50) NOT NULL DEFAULT '', utm_medium VARCHAR(50) NOT NULL DEFAULT '', utm_campaign VARCHAR(100) NOT NULL DEFAULT '', device ENUM('desktop','mobile','tablet') NOT NULL DEFAULT 'desktop', is_bot TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (session_hash), KEY idx_date (date), KEY idx_date_source (date, source), KEY idx_date_source_detail (date, source_detail), KEY idx_date_bot (date, is_bot), KEY idx_date_ip (date, ip_hash) ) $charset;"); // --- Pageviews --- dbDelta("CREATE TABLE {$p}pageviews ( date DATE NOT NULL, page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', page_id BIGINT UNSIGNED NOT NULL DEFAULT 0, page_url VARCHAR(255) NOT NULL DEFAULT '', views INT UNSIGNED NOT NULL DEFAULT 0, unique_views INT UNSIGNED NOT NULL DEFAULT 0, total_time INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (date, page_type, page_id), KEY idx_date_views (date, views) ) $charset;"); // --- Sources --- dbDelta("CREATE TABLE {$p}sources ( date DATE NOT NULL, source ENUM('direct','google','yandex','bing','social','referral','internal','other') NOT NULL, source_detail VARCHAR(50) NOT NULL DEFAULT '', referrer_host VARCHAR(100) NOT NULL DEFAULT '', sessions INT UNSIGNED NOT NULL DEFAULT 0, pageviews INT UNSIGNED NOT NULL DEFAULT 0, total_duration INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (date, source, source_detail, referrer_host) ) $charset;"); // --- Daily summary --- dbDelta("CREATE TABLE {$p}daily ( date DATE NOT NULL, sessions INT UNSIGNED NOT NULL DEFAULT 0, unique_visitors INT UNSIGNED NOT NULL DEFAULT 0, pageviews INT UNSIGNED NOT NULL DEFAULT 0, bounces INT UNSIGNED NOT NULL DEFAULT 0, bounces_human INT UNSIGNED NOT NULL DEFAULT 0, bot_sessions INT UNSIGNED NOT NULL DEFAULT 0, total_duration INT UNSIGNED NOT NULL DEFAULT 0, total_duration_human INT UNSIGNED NOT NULL DEFAULT 0, total_duration_approx INT UNSIGNED NOT NULL DEFAULT 0, total_duration_approx_human INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (date) ) $charset;"); // --- Transitions --- dbDelta("CREATE TABLE {$p}transitions ( date DATE NOT NULL, from_page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', from_page_id BIGINT UNSIGNED NOT NULL, to_page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', to_page_id BIGINT UNSIGNED NOT NULL, count INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (date, from_page_type, from_page_id, to_page_type, to_page_id) ) $charset;"); // --- Events --- dbDelta("CREATE TABLE {$p}events ( date DATE NOT NULL, page_type ENUM('post','page','category','tag','tax','archive','home','search','404','other') NOT NULL DEFAULT 'other', page_id BIGINT UNSIGNED NOT NULL DEFAULT 0, link_type ENUM('outbound','email','phone') NOT NULL DEFAULT 'outbound', link_url VARCHAR(500) NOT NULL DEFAULT '', link_text VARCHAR(120) NOT NULL DEFAULT '', count INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (date, page_type, page_id, link_type, link_url), KEY idx_date_type (date, link_type), KEY idx_date_page (date, page_type, page_id) ) $charset;"); self::create_data_folders(); update_option('eistat_db_version', EISTAT_VERSION); } /** * Create wp-content/uploads/eistat/ and its subfolders, copy the * collection endpoint, and protect the data folder. */ private static function create_data_folders() { $data_dir = eistat_get_data_dir(); $buffer_dir = $data_dir . 'buffer/'; $archive_dir = $data_dir . 'archives/'; if (!file_exists($data_dir)) wp_mkdir_p($data_dir); if (!file_exists($buffer_dir)) wp_mkdir_p($buffer_dir); if (!file_exists($archive_dir)) wp_mkdir_p($archive_dir); // --- Protect the data folder (Apache/LiteSpeed) --- $data_htaccess = "Options -Indexes\n" . "\n" . " Require all denied\n" . "\n"; @file_put_contents($data_dir . '.htaccess', $data_htaccess); @file_put_contents($data_dir . 'index.php', "