PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 1.15
Independent Analytics – WordPress Analytics Plugin v1.15
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 / inc / queries / view.php
independent-analytics / inc / queries Last commit date
campaigns.php 3 years ago current_traffic_finder.php 3 years ago geos.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 view.php 3 years ago views.php 3 years ago visitors_over_time_finder.php 3 years ago
view.php
261 lines
1 <?php
2
3 namespace IAWP;
4
5 class View
6 {
7 public function __construct($payload, $referrer_url, $visitor, $campaign, $viewed_at = null)
8 {
9 global $wpdb;
10 $views_table = DB::get_table_name(DB::VIEWS);
11 $view = [];
12
13 $view['page'] = $payload['page'];
14
15 if (isset($referrer_url)) {
16 $url = new URL($referrer_url);
17
18 if ($url->is_valid_url()) {
19 $referrer = $this->get_or_create_referrer($url->get_domain());
20 $view['referrer_id'] = $referrer->id;
21 }
22 }
23
24 $visitor = $this->get_or_create_visitor($visitor);
25 $view['visitor_id'] = $visitor->id;
26
27 $campaign = $this->get_or_maybe_create_campaign($campaign);
28 if (!is_null($campaign)) {
29 $view['campaign_id'] = $campaign->campaign_id;
30 }
31
32 $resource_payload = $payload;
33 unset($resource_payload['page']);
34 $resource = $this->get_or_create_resource($resource_payload);
35 $view['resource_id'] = $resource->id;
36
37 if (isset($viewed_at)) {
38 $view['viewed_at'] = $viewed_at;
39 } else {
40 $date = new \DateTime();
41 $view['viewed_at'] = $date->format('Y-m-d H:i:s');
42 }
43
44 try {
45 $wpdb->insert($views_table, $view);
46 } catch (Exception $e) {
47 }
48 }
49
50 private function get_or_create_resource($payload)
51 {
52 global $wpdb;
53 $resources_table = DB::get_table_name(DB::RESOURCES);
54 $query = '';
55
56 switch ($payload['resource']) {
57 case 'singular':
58 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND singular_id = %d", $payload['resource'], $payload['singular_id']);
59 break;
60 case 'author_archive':
61 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND author_id = %d", $payload['resource'], $payload['author_id']);
62 break;
63 case 'date_archive':
64 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND date_archive = %s", $payload['resource'], $payload['date_archive']);
65 break;
66 case 'post_type_archive':
67 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND post_type = %s", $payload['resource'], $payload['post_type']);
68 break;
69 case 'term_archive':
70 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND term_id = %s", $payload['resource'], $payload['term_id']);
71 break;
72 case 'search':
73 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND search_query = %s", $payload['resource'], $payload['search_query']);
74 break;
75 case 'home':
76 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s ", $payload['resource']);
77 break;
78 case '404':
79 $query = $wpdb->prepare("SELECT * FROM $resources_table WHERE resource = %s AND not_found_url = %s", $payload['resource'], $payload['not_found_url']);
80 break;
81 }
82
83 $resource = $wpdb->get_row($query);
84
85 if (is_null($resource)) {
86 $wpdb->insert($resources_table, $payload);
87 $resource = $wpdb->get_row($query);
88 }
89
90 // Todo - This probably shouldn't happen here... A call should be make to the page and then the page should
91 // know if it's a page type that needs to be cached or not.
92 switch ($resource->resource) {
93 case 'singular':
94 (new Page_Singular($resource))->update_cache();
95 break;
96 case 'author_archive':
97 (new Page_Author_Archive($resource))->update_cache();
98 break;
99 case 'post_type_archive':
100 (new Page_Post_Type_Archive($resource))->update_cache();
101 break;
102 case 'term_archive':
103 (new Page_Term_Archive($resource))->update_cache();
104 break;
105 case 'home':
106 (new Page_Home($resource))->update_cache();
107 break;
108 case 'date_archive':
109 (new Page_Date_Archive($resource))->update_cache();
110 break;
111 case '404':
112 (new Page_Not_Found($resource))->update_cache();
113 break;
114 case 'search':
115 (new Page_Search($resource))->update_cache();
116 break;
117 }
118
119 return $resource;
120 }
121
122 // Why not use an index? Indexing a varchar(2048) isn't possible.
123 private function get_or_create_referrer($referrer_domain)
124 {
125 global $wpdb;
126 $referrers_table = DB::get_table_name(DB::REFERRERS);
127 $referrer_domain = trim($referrer_domain);
128
129 if (strlen($referrer_domain) == 0) {
130 return null;
131 }
132
133 $referrer = $wpdb->get_row($wpdb->prepare("SELECT * FROM $referrers_table WHERE domain = %s", $referrer_domain));
134
135 if (!is_null($referrer)) {
136 return $referrer;
137 }
138
139 $wpdb->insert($referrers_table, ['domain' => $referrer_domain]);
140
141 return $wpdb->get_row($wpdb->prepare("SELECT * FROM $referrers_table WHERE domain = %s", $referrer_domain));
142 }
143
144 private function get_or_create_visitor($visitor)
145 {
146 global $wpdb;
147 $visitors_table = DB::get_table_name(DB::VISITORS);
148
149 $existing_visitor = $wpdb->get_row($wpdb->prepare("
150 SELECT *
151 FROM $visitors_table
152 WHERE visitor_token = %s
153 ", $visitor->token()));
154
155 // If there is an existing visitor
156 if (!is_null($existing_visitor)) {
157 // If the existing visitor doesn't have geo data
158 if (is_null($existing_visitor->country_code)) {
159 $wpdb->update(
160 $visitors_table,
161 [
162 'country_code' => $visitor->country_code(),
163 'city' => $visitor->city(),
164 'subdivision' => $visitor->subdivision(),
165 'country' => $visitor->country(),
166 'continent' => $visitor->continent(),
167 ],
168 ['visitor_token' => $visitor->token()],
169 [
170 '%s',
171 '%s',
172 '%s',
173 '%s',
174 '%s',
175 ],
176 ['%s']
177 );
178
179 return $wpdb->get_row($wpdb->prepare("
180 SELECT *
181 FROM $visitors_table
182 WHERE visitor_token = %s
183 ", $visitor->token()));
184 }
185
186 return $existing_visitor;
187 }
188
189 $wpdb->insert($visitors_table, [
190 'visitor_token' => $visitor->token(),
191 'country_code' => $visitor->country_code(),
192 'city' => $visitor->city(),
193 'subdivision' => $visitor->subdivision(),
194 'country' => $visitor->country(),
195 'continent' => $visitor->continent(),
196 ]);
197
198 return $wpdb->get_row($wpdb->prepare("
199 SELECT *
200 FROM $visitors_table
201 WHERE visitor_token = %s
202 ", $visitor->token()));
203 }
204
205 private function get_or_maybe_create_campaign($payload)
206 {
207 global $wpdb;
208 $required_fields = ['utm_source', 'utm_medium', 'utm_campaign'];
209 $valid = true;
210
211 foreach ($required_fields as $field) {
212 if (!isset($payload[$field])) {
213 $valid = false;
214 }
215 }
216
217 if (!$valid) {
218 return null;
219 }
220
221 $campaigns_table = DB::get_table_name(DB::CAMPAIGNS);
222 $campaign = $wpdb->get_row(
223 $wpdb->prepare(
224 "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))",
225 $payload['utm_source'],
226 $payload['utm_medium'],
227 $payload['utm_campaign'],
228 $payload['utm_term'],
229 isset($payload['utm_term']) ? 1 : 0,
230 $payload['utm_content'],
231 isset($payload['utm_content']) ? 1 : 0
232 )
233 );
234
235 if (!is_null($campaign)) {
236 return $campaign;
237 }
238
239 $wpdb->insert($campaigns_table, [
240 'utm_source' => $payload['utm_source'],
241 'utm_medium' => $payload['utm_medium'],
242 'utm_campaign' => $payload['utm_campaign'],
243 'utm_term' => $payload['utm_term'],
244 'utm_content' => $payload['utm_content'],
245 ]);
246
247 return $wpdb->get_row(
248 $wpdb->prepare(
249 "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))",
250 $payload['utm_source'],
251 $payload['utm_medium'],
252 $payload['utm_campaign'],
253 $payload['utm_term'],
254 isset($payload['utm_term']) ? 1 : 0,
255 $payload['utm_content'],
256 isset($payload['utm_content']) ? 1 : 0
257 )
258 );
259 }
260 }
261