-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.php
More file actions
170 lines (151 loc) · 5.64 KB
/
database.php
File metadata and controls
170 lines (151 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* CloudScale Analytics - Database
*
* Creates the wp_cs_analytics_views_v2 and wp_cs_analytics_referrers_v2 tables.
* Plugin uses V2 hourly bucket schema exclusively.
*
* @package CloudScale_Free_Analytics
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Run on plugin activation: create all database tables and seed the version option.
*
* @since 1.0.0
* @return void
*/
function cspv_activate() {
cspv_create_table_v2();
cspv_create_table_referrers_v2();
cspv_create_table_geo_v2();
cspv_create_table_visitors_v2();
cspv_create_table_404_v2();
cspv_create_table_sessions_v2();
add_option( 'cspv_version', CSPV_VERSION );
}
/**
* Create the V2 hourly bucketed views table.
*
* One row per post per hour, with a view_count column.
* The beacon increments the bucket for the current hour on each view.
*
* Schema:
* post_id — the post
* viewed_at — hour bucket, always truncated to :00:00
* view_count — number of views in this bucket
*/
function cspv_create_table_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cs_analytics_views_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket, always :00:00',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY post_hour (post_id, viewed_at),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* V2 referrer table: one row per post + hour + referrer.
*/
function cspv_create_table_referrers_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cs_analytics_referrers_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket, always :00:00',
referrer VARCHAR(512) NOT NULL DEFAULT '',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY post_hour_ref (post_id, viewed_at, referrer(191)),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create the geo tracking table.
*
* Stores country code per post per hour bucket. Country is resolved
* from the CloudFlare CF-IPCountry header (no raw IP stored).
* Later can be extended with city/lat/lng from DB-IP Lite.
*/
function cspv_create_table_geo_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cs_analytics_geo_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket',
country_code CHAR(2) NOT NULL DEFAULT '',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY post_hour_country (post_id, viewed_at, country_code),
KEY country_code (country_code),
KEY viewed_at (viewed_at)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create the unique visitors tracking table.
*
* Stores a SHA256 hash of the visitor IP per post per calendar day.
* The unique key ensures the same visitor viewing the same post
* on the same day only creates one row (INSERT IGNORE).
* No raw IP is ever stored.
*/
function cspv_create_table_visitors_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cs_analytics_visitors_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
visitor_hash CHAR(64) NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATE NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY hash_post_day (visitor_hash, post_id, viewed_at),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create the session depth tracking table.
*
* One row per session+post pair (INSERT IGNORE deduplication).
* session_id is a browser-generated random token stored in sessionStorage —
* it contains no PII and is scoped to a single browser tab session.
* Enables computing pages-per-session percentiles (P50 / P95 / P99).
*/
function cspv_create_table_sessions_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cs_analytics_sessions_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
session_id VARCHAR(64) NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATE NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY session_post (session_id, post_id),
KEY viewed_at (viewed_at),
KEY session_id (session_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}