PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.19.0
Independent Analytics – WordPress Analytics Plugin v1.19.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Queries / View.php
independent-analytics / IAWP / Queries Last commit date
Campaigns.php 3 years ago City_Statistics.php 3 years ago Country_Statistics.php 3 years ago Current_Traffic_Finder.php 3 years ago Interval.php 3 years ago Minute_Interval.php 3 years ago Range_Query.php 3 years ago Referrers.php 3 years ago Reset_Database.php 3 years ago Resources.php 3 years ago Ten_Second_Interval.php 3 years ago View.php 3 years ago Views.php 3 years ago Visitors_Over_Time_Finder.php 3 years ago
View.php
298 lines
1 <?php
2
3 namespace IAWP\Queries;
4
5 use IAWP\Models\Page_Author_Archive;
6 use IAWP\Models\Page_Date_Archive;
7 use IAWP\Models\Page_Home;
8 use IAWP\Models\Page_Not_Found;
9 use IAWP\Models\Page_Post_Type_Archive;
10 use IAWP\Models\Page_Search;
11 use IAWP\Models\Page_Singular;
12 use IAWP\Models\Page_Term_Archive;
13 use IAWP\Models\Visitor;
14 use IAWP\Query;
15 use IAWP\Utils\String_Util;
16 use IAWP\Utils\URL;
17
18 class View
19 {
20 private $payload;
21 private $referrer_url;
22 private $visitor;
23 private $campaign_fields;
24 private $viewed_at;
25 private $resource;
26 private $referrer;
27 private $campaign;
28 private $session;
29
30 /**
31 * @param array $payload
32 * @param string|null $referrer_url
33 * @param Visitor $visitor
34 * @param array $campaign
35 * @param null $viewed_at
36 */
37 public function __construct(array $payload, ?string $referrer_url, Visitor $visitor, array $campaign_fields, $viewed_at = null)
38 {
39 $this->payload = $payload;
40 $this->referrer_url = trim($referrer_url);
41 $this->visitor = $visitor;
42 $this->campaign_fields = $campaign_fields;
43 $this->viewed_at = $viewed_at;
44 $this->resource = $this->get_resource();
45 $this->session = $this->get_session();
46 $view_id = $this->create_view();
47 $this->set_sessions_initial_view($view_id);
48 }
49
50 private function set_sessions_initial_view(int $view_id)
51 {
52 global $wpdb;
53
54 $sessions_table = Query::get_table_name(Query::SESSIONS);
55
56 $wpdb->query(
57 $wpdb->prepare(
58 "UPDATE $sessions_table SET initial_view_id = %d WHERE session_id = %d AND initial_view_id IS NULL",
59 $view_id,
60 $this->session
61 )
62 );
63 }
64
65 private function create_view()
66 {
67 $date = is_null($this->viewed_at) ? new \DateTime() : $this->viewed_at;
68 $viewed_at = $date->format('Y-m-d H:i:s');
69
70 return Query::query('create_view', [
71 'resource_id' => $this->resource,
72 'viewed_at' => $viewed_at,
73 'page' => $this->payload['page'],
74 'session_id' => $this->session,
75 ])->last_inserted_id();
76 }
77
78 private function get_resource(): ?int
79 {
80 global $wpdb;
81 $resources_table = Query::get_table_name(Query::RESOURCES);
82 $query = '';
83
84 $payload = array_merge($this->payload);
85 unset($payload['page']);
86
87 switch ($payload['resource']) {
88 case 'singular':
89 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND singular_id = %d", $payload['resource'], $payload['singular_id']);
90 break;
91 case 'author_archive':
92 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND author_id = %d", $payload['resource'], $payload['author_id']);
93 break;
94 case 'date_archive':
95 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND date_archive = %s", $payload['resource'], $payload['date_archive']);
96 break;
97 case 'post_type_archive':
98 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND post_type = %s", $payload['resource'], $payload['post_type']);
99 break;
100 case 'term_archive':
101 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND term_id = %s", $payload['resource'], $payload['term_id']);
102 break;
103 case 'search':
104 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND search_query = %s", $payload['resource'], $payload['search_query']);
105 break;
106 case 'home':
107 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s ", $payload['resource']);
108 break;
109 case '404':
110 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND not_found_url = %s", $payload['resource'], $payload['not_found_url']);
111 break;
112 }
113
114 $resource = $wpdb->get_row($query);
115
116 if (is_null($resource)) {
117 $wpdb->insert($resources_table, $payload);
118 $resource = $wpdb->get_row($query);
119 }
120
121 // Todo - This probably shouldn't happen here... A call should be make to the page and then the page should
122 // know if it's a page type that needs to be cached or not.
123 switch ($resource->resource) {
124 case 'singular':
125 (new Page_Singular($resource))->update_cache();
126 break;
127 case 'author_archive':
128 (new Page_Author_Archive($resource))->update_cache();
129 break;
130 case 'post_type_archive':
131 (new Page_Post_Type_Archive($resource))->update_cache();
132 break;
133 case 'term_archive':
134 (new Page_Term_Archive($resource))->update_cache();
135 break;
136 case 'home':
137 (new Page_Home($resource))->update_cache();
138 break;
139 case 'date_archive':
140 (new Page_Date_Archive($resource))->update_cache();
141 break;
142 case '404':
143 (new Page_Not_Found($resource))->update_cache();
144 break;
145 case 'search':
146 (new Page_Search($resource))->update_cache();
147 break;
148 }
149
150 return $resource->id;
151 }
152
153 private function get_session(): int
154 {
155 $is_internal_referrer = $this->is_internal_referrer($this->referrer_url);
156 $current_session = $this->get_current_session();
157
158 if (isset($current_session) && $is_internal_referrer) {
159 return $current_session;
160 } else {
161 return $this->create_session();
162 }
163 }
164
165 /**
166 * @param string|null $referrer_url
167 * @return bool
168 */
169 private function is_internal_referrer(?string $referrer_url): bool
170 {
171 return !empty($referrer_url) && String_Util::str_starts_with(strtolower($referrer_url), strtolower(site_url()));
172 }
173
174 /**
175 * Gets the current session not including any that might get created by the current view
176 *
177 * @return int|null
178 */
179 private function get_current_session(): ?int
180 {
181 $session = Query::query('get_current_session', [
182 'visitor_id' => $this->visitor->id(),
183 ])->row();
184
185 if (isset($session)) {
186 return $session->session_id;
187 } else {
188 return null;
189 }
190 }
191
192 public function create_session(): int
193 {
194 $date = is_null($this->viewed_at) ? new \DateTime() : $this->viewed_at;
195 $created_at = $date->format('Y-m-d H:i:s');
196
197 return Query::query('create_session', [
198 'visitor_id' => $this->visitor->id(),
199 'referrer_id' => $this->get_referrer(),
200 'campaign_id' => $this->get_campaign(),
201 'created_at' => $created_at,
202 ])->last_inserted_id();
203 }
204
205 private function get_referrer(): ?int
206 {
207 if (!isset($this->referrer_url) || strlen($this->referrer_url) === 0) {
208 return null;
209 }
210
211 $url = new URL($this->referrer_url);
212
213 if (!$url->is_valid_url()) {
214 return null;
215 }
216
217 if ($this->is_internal_referrer($this->referrer_url)) {
218 return null;
219 }
220
221 Query::query('create_referrer', [
222 'domain' => $url->get_domain(),
223 ]);
224
225 $referrer = Query::query('get_referrer', [
226 'domain' => $url->get_domain(),
227 ])->row();
228
229 if (isset($referrer)) {
230 return $referrer->id;
231 } else {
232 return null;
233 }
234 }
235
236 private function get_campaign(): ?int
237 {
238 global $wpdb;
239 $required_fields = ['utm_source', 'utm_medium', 'utm_campaign'];
240 $valid = true;
241
242 foreach ($required_fields as $field) {
243 if (!isset($this->campaign_fields[$field])) {
244 $valid = false;
245 }
246 }
247
248 if (!$valid) {
249 return null;
250 }
251
252 $campaigns_table = Query::get_table_name(Query::CAMPAIGNS);
253 $campaign = $wpdb->get_row(
254 $wpdb->prepare(
255 "SELECT * FROM $campaigns_table WHERE utm_source = %s AND utm_medium = %s AND utm_campaign = %s AND (utm_term = %s OR (%d = 0 AND utm_term IS NULL)) AND (utm_content = %s OR (%d = 0 AND utm_content IS NULL))",
256 $this->campaign_fields['utm_source'],
257 $this->campaign_fields['utm_medium'],
258 $this->campaign_fields['utm_campaign'],
259 $this->campaign_fields['utm_term'],
260 isset($this->campaign_fields['utm_term']) ? 1 : 0,
261 $this->campaign_fields['utm_content'],
262 isset($this->campaign_fields['utm_content']) ? 1 : 0
263 )
264 );
265
266 if (!is_null($campaign)) {
267 return $campaign->campaign_id;
268 }
269
270 $wpdb->insert($campaigns_table, [
271 'utm_source' => $this->campaign_fields['utm_source'],
272 'utm_medium' => $this->campaign_fields['utm_medium'],
273 'utm_campaign' => $this->campaign_fields['utm_campaign'],
274 'utm_term' => $this->campaign_fields['utm_term'],
275 'utm_content' => $this->campaign_fields['utm_content'],
276 ]);
277
278 $campaign = $wpdb->get_row(
279 $wpdb->prepare(
280 "SELECT * FROM $campaigns_table WHERE utm_source = %s AND utm_medium = %s AND utm_campaign = %s AND (utm_term = %s OR (%d = 0 AND utm_term IS NULL)) AND (utm_content = %s OR (%d = 0 AND utm_content IS NULL))",
281 $this->campaign_fields['utm_source'],
282 $this->campaign_fields['utm_medium'],
283 $this->campaign_fields['utm_campaign'],
284 $this->campaign_fields['utm_term'],
285 isset($this->campaign_fields['utm_term']) ? 1 : 0,
286 $this->campaign_fields['utm_content'],
287 isset($this->campaign_fields['utm_content']) ? 1 : 0
288 )
289 );
290
291 if (!is_null($campaign)) {
292 return $campaign->campaign_id;
293 }
294
295 return null;
296 }
297 }
298