Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit 7786a12

Browse files
committed
Performance improvements
1 parent 17b8fcc commit 7786a12

File tree

2 files changed

+22
-167
lines changed

2 files changed

+22
-167
lines changed

webanalytics.php

Lines changed: 19 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class web_analytics {
158158
private $s = null;
159159
private $h = null;
160160
private $d = null;
161-
private $agent_id = null;
162161
private $profile_id = null;
163162
private $isp_id = null;
164163
private $ua = null;
@@ -168,95 +167,6 @@ class web_analytics {
168167
private $u_host = null;
169168
private $u_language = null;
170169
private $ubid = null;
171-
private $unid = null;
172-
private $u_mobile = 0;
173-
private $u_bot = 0;
174-
175-
function analyse_user_agent($user_agent) {
176-
$result = array();
177-
$gecko = preg_match("/Mozilla\/\d[\d.]* \([A-Za-z0-9_.\- ;:\/]*\) Gecko\/\d+/i", $user_agent);
178-
$webkit = preg_match("/Mozilla\/\d[\d.]* \([A-Za-z0-9_.\- ;:\/]*\) AppleWebKit\/\d[\d.]* \(KHTML, like Gecko\)/i", $user_agent);
179-
if(preg_match_all("/\w+\/\d[\d.]*/", $user_agent, $matches)) {
180-
$browser = preg_split("/\//",$matches[0][array_key_last($matches[0])]);
181-
$trident = (preg_match("/trident/i", $browser[0]) && !$gecko && !$webkit);
182-
if($webkit) {
183-
if(preg_match("/safari/i", $browser[0])) {
184-
$browser = preg_split("/\//",$matches[0][2]);
185-
$i = 3;
186-
while((preg_match("/version/i", $browser[0]) || preg_match("/mobile/i", $browser[0])) && isset($matches[0][$i])) {
187-
$browser = preg_split("/\//",$matches[0][$i]);
188-
$i++;
189-
}
190-
}
191-
}
192-
}
193-
if(preg_match("/\([A-Za-z0-9_.\- ;:\/]*\)/", $user_agent, $match)) {
194-
$platforms = preg_split("/; /", preg_replace("/\)/", "", preg_replace("/\(/", "", $match[0])));
195-
if($trident) {
196-
$browser = preg_split("/ /",$platforms[1]);
197-
if(preg_match("/msie/i", $browser[0])) {
198-
$os = preg_split("/ \d/", preg_replace("/ nt/i", "",$platforms[2]));
199-
$osv = preg_split("/ /",$platforms[2]);
200-
if(preg_match("/xbox/i", $platforms[array_key_last($platforms)])) {
201-
$result["device"]["name"] = $platforms[array_key_last($platforms)];
202-
}
203-
} else {
204-
$browser[0] = "msie";
205-
$version = preg_split("/:/", $platforms[array_key_last($platforms)]);
206-
$browser[1] = $version[1];
207-
}
208-
}
209-
if(preg_match("/windows/i", $platforms[0])) {
210-
$os = preg_split("/ \d/", preg_replace("/ nt/i", "",$platforms[0]));
211-
$osv = preg_split("/ /",$platforms[0]);
212-
if(preg_match("/phone/i", $os[0])) {
213-
$result["device"]["name"] = $platforms[array_key_last($platforms)-1]." ".$platforms[array_key_last($platforms)];
214-
}
215-
if(preg_match("/xbox/i", $platforms[array_key_last($platforms)])) {
216-
$result["device"]["name"] = $platforms[array_key_last($platforms)];
217-
}
218-
} else if(preg_match("/linux/i", $platforms[0])) {
219-
$i = preg_match("/u/i", $platforms[1]) ? 2 : 1;
220-
$os = preg_split("/ \d/",$platforms[$i]);
221-
if(preg_match("/android/i", $os[0])) {
222-
$osv = preg_split("/ /",$platforms[$i]);
223-
} else {
224-
$os = preg_split("/ /",$platforms[0]);
225-
}
226-
foreach ($platforms as $property) {
227-
if(preg_match("/build/i", $property)) {
228-
$device = preg_split("/ build/i", $property);
229-
$result["device"]["name"] = $device[0];
230-
}
231-
}
232-
} else if(preg_match("/linux/i", $platforms[1]) || preg_match("/cros/i", $platforms[1]) || preg_match("/ubuntu/i", $platforms[1])) {
233-
$os = preg_split("/ /",$platforms[1]);
234-
} else if(preg_match("/macintosh/i", $platforms[0])) {
235-
$os = preg_split("/ \d/",preg_replace("/intel /i", "", $platforms[1]));
236-
$osv = preg_split("/ /",$platforms[1]);
237-
$result["device"]["name"] = $platforms[0];
238-
} else if(preg_match("/iphone/i", $platforms[0]) || preg_match("/ipad/i", $platforms[0]) || preg_match("/ipod/i", $platforms[0])) {
239-
$os = preg_split("/ \d/",preg_replace("/cpu /i", "", $platforms[1]));
240-
$osv = preg_split("/ /", preg_replace("/ like mac os x/i", "", $platforms[1]));
241-
$result["device"]["name"] = $platforms[0];
242-
} else if(preg_match("/android/i", $platforms[0])) {
243-
$os = preg_split("/ \d/",$platforms[0]);
244-
$osv = preg_split("/ /",$platforms[0]);
245-
$result["device"]["name"] = $platforms[1];
246-
}
247-
if(isset($os)) {
248-
$result["os"]["name"] = $os[0];
249-
}
250-
if(isset($osv)) {
251-
$result["os"]["version"] = $osv[array_key_last($osv)];
252-
}
253-
}
254-
if(isset($browser)) {
255-
$result["browser"]["name"] = $browser[0];
256-
$result["browser"]["version"] = $browser[1];
257-
}
258-
return $result;
259-
}
260170

261171
// Get user language and country from hostname and http header
262172
function get_country_code() {
@@ -290,25 +200,13 @@ function check_database() {
290200
"country" => "VARCHAR(2)",
291201
"last_update" => "TIMESTAMP NULL"
292202
));
293-
$this->db_manager->create_table("wa_networks", array(
294-
"id" => "VARCHAR(15) PRIMARY KEY",
295-
"ip" => "VARCHAR(45) NOT NULL",
203+
$this->db_manager->create_table("wa_ips", array(
204+
"ip" => "VARCHAR(45) PRIMARY KEY",
296205
"host" => "VARCHAR(253)",
297206
"country" => "VARCHAR(2)",
298207
"isp_id" => "VARCHAR(10)",
299208
"last_update" => "TIMESTAMP NULL"
300209
));
301-
$this->db_manager->create_table("wa_agents", array(
302-
"id" => "VARCHAR(10) PRIMARY KEY",
303-
"agent" => "TEXT",
304-
"browser" => "VARCHAR(40)",
305-
"browser_version" => "VARCHAR(20)",
306-
"os" => "VARCHAR(40)",
307-
"os_version" => "VARCHAR(10)",
308-
"device" => "VARCHAR(40)",
309-
"mobile" => "TINYINT(1)",
310-
"bot" => "TINYINT(1)"
311-
));
312210
$this->db_manager->create_table("wa_profiles", array(
313211
"id" => "VARCHAR(10) PRIMARY KEY",
314212
"screen_width" => "VARCHAR(9)",
@@ -324,17 +222,14 @@ function check_database() {
324222
"id" => "VARCHAR(20) PRIMARY KEY",
325223
"domain" => "TEXT",
326224
"browser_id" => "VARCHAR(15) NOT NULL",
327-
"agent_id" => "VARCHAR(10)"
225+
"user_agent" => "TEXT"
328226
));
329227
$this->db_manager->create_table("wa_browsers", array(
330228
"id" => "VARCHAR(15) PRIMARY KEY",
331229
"ip" => "VARCHAR(45) NOT NULL",
332230
"country" => "VARCHAR(2)",
333231
"language" => "VARCHAR(2)",
334-
"mobile" => "TINYINT(1)",
335-
"bot" => "TINYINT(1)",
336-
"agent_id" => "VARCHAR(10)",
337-
"network_id" => "VARCHAR(15) NOT NULL",
232+
"user_agent" => "TEXT",
338233
"profile_id" => "VARCHAR(10)",
339234
"last_update" => "TIMESTAMP NULL"
340235
));
@@ -349,8 +244,8 @@ function check_database() {
349244
"visitor_ip" => "VARCHAR(45)",
350245
"visitor_country" => "VARCHAR(2)",
351246
"cf_ray_id" => "TEXT",
247+
"user_agent" => "TEXT",
352248
"browser_id" => "VARCHAR(15)",
353-
"network_id" => "VARCHAR(15)"
354249
));
355250
}
356251

@@ -377,49 +272,16 @@ function get_isp() {
377272
}
378273

379274
// Get network's unique id
380-
function get_network() {
275+
function save_ip() {
381276
if(!isset($this->u_ip)) {
382-
return null;
277+
return;
383278
}
384-
$row = $this->db_manager->first("wa_networks", "id", array("ip" => $this->u_ip));
385-
if($row != null) {
386-
$this->db_manager->update("wa_networks", array("host" => $this->u_host), array("id" => $row[0]));
387-
return $row["id"];
388-
}
389-
$unid = $this->db_manager->generate_id(15);
390-
$this->db_manager->add("wa_networks", array(
391-
"id" => $unid,
279+
$this->db_manager->add("wa_ips", array(
392280
"ip" => $this->u_ip,
393281
"host" => $this->u_host,
394282
"country" => $this->u_country_code,
395283
"isp_id" => $this->isp_id
396284
));
397-
return $unid;
398-
}
399-
400-
// Get agent's unique id
401-
function get_agent() {
402-
if($this->ua == null && $this->ua == "") {
403-
return null;
404-
}
405-
$row = $this->db_manager->get_one_row("SELECT id FROM wa_agents WHERE agent LIKE '".$this->ua."' LIMIT 1;");
406-
if($row != null) {
407-
return $row["id"];
408-
}
409-
$id = $this->db_manager->generate_id();
410-
$uaa = $this->analyse_user_agent($this->ua);
411-
$this->db_manager->add("wa_agents", array(
412-
"id" => $id,
413-
"agent" => $this->ua,
414-
"browser" => isset($uaa["browser"]["name"]) ? $uaa["browser"]["name"] : null,
415-
"browser_version" => isset($uaa["browser"]["version"]) ? $uaa["browser"]["version"] : null,
416-
"os" => isset($uaa["os"]["name"]) ? $uaa["os"]["name"] : null,
417-
"os_version" => isset($uaa["os"]["version"]) ? $uaa["os"]["version"] : null,
418-
"device" => isset($uaa["device"]["name"]) ? $uaa["device"]["name"] : null,
419-
"mobile" => $this->u_mobile,
420-
"bot" => $this->u_bot
421-
));
422-
return $id;
423285
}
424286

425287
// Use cookies set by tracking script to get device's unique profile id
@@ -462,9 +324,8 @@ function indentify_browser() {
462324
setcookie("webid", $this->c["webid"], time()+60*60*24*180, "/", $this->d);
463325
$this->db_manager->update("wa_browsers", array(
464326
"ip" => $this->u_ip,
465-
"network_id" => $this->unid,
466327
"profile_id" => $this->profile_id,
467-
"agent_id" => $this->agent_id,
328+
"user_agent" => $this->ua,
468329
"last_update" => date('Y-m-d H:i:s')
469330
), array("id" => $row["browser_id"]));
470331
return $row["browser_id"];
@@ -474,9 +335,9 @@ function indentify_browser() {
474335
$cid = $this->db_manager->generate_id(20);
475336
$result = null;
476337
if($this->u_language != null) {
477-
$result = $this->db_manager->query("SELECT id FROM wa_browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language = '".$this->u_language."' AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
338+
$result = $this->db_manager->query("SELECT id FROM wa_browsers WHERE ip = '".$this->u_ip."' AND user_agent LIKE '".$this->ua."' AND language = '".$this->u_language."' AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
478339
} else {
479-
$result = $this->db_manager->query("SELECT id FROM wa_browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language IS NULL AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
340+
$result = $this->db_manager->query("SELECT id FROM wa_browsers WHERE ip = '".$this->u_ip."' AND user_agent LIKE '".$this->ua."' AND language IS NULL AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
480341
}
481342
$ubid = "";
482343
$ubid_count = 0;
@@ -486,20 +347,20 @@ function indentify_browser() {
486347
}
487348
if($ubid_count == 1) {
488349
$this->db_manager->update("wa_browsers", array("last_update" => date('Y-m-d H:i:s')), array("id" => $ubid));
489-
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, time FROM wa_trackers".$this->db_manager->get_filter(array("browser_id" => $ubid, "agent_id" => $this->agent_id))." ORDER BY time DESC LIMIT 1;");
350+
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, time FROM wa_trackers".$this->db_manager->get_filter(array("browser_id" => $ubid, "user_agent" => $this->ua))." ORDER BY time DESC LIMIT 1;");
490351
if($cidrow != null) {
491352
if(strtotime($cidrow["time"]) >= strtotime("-90 days") && $cidrow["domain"] == $this->d) {
492353
setcookie("webid", $cidrow["id"], time()+60*60*24*180, "/", $this->d);
493354
$this->db_manager->update("wa_trackers", array("time" => date('Y-m-d H:i:s')), array("id" => $cidrow["id"]));
494355
return $ubid;
495356
}
496357
}
497-
$this->db_manager->delete("wa_trackers", array("browser_id" => $ubid, "agent_id" => $this->agent_id, "domain" => $this->d));
358+
$this->db_manager->delete("wa_trackers", array("browser_id" => $ubid, "user_agent" => $this->ua, "domain" => $this->d));
498359
$this->db_manager->add("wa_trackers", array(
499360
"id" => $cid,
500361
"domain" => $this->d,
501362
"browser_id" => $ubid,
502-
"agent_id" => $this->agent_id
363+
"user_agent" => $this->ua
503364
));
504365
setcookie("webid", $cid, time()+60*60*24*180, "/", $this->d);
505366
return $ubid;
@@ -509,18 +370,15 @@ function indentify_browser() {
509370
"id" => $cid,
510371
"domain" => $this->d,
511372
"browser_id" => $ubid,
512-
"agent_id" => $this->agent_id
373+
"user_agent" => $this->ua
513374
));
514375
setcookie("webid", $cid, time()+60*60*24*180, "/", $this->d);
515376
$this->db_manager->add("wa_browsers", array(
516377
"id" => $ubid,
517378
"ip" => $this->u_ip,
518379
"country" => $this->u_country_code,
519380
"language" => $this->u_language,
520-
"mobile" => $this->u_mobile,
521-
"bot" => $this->u_bot,
522-
"agent_id" => $this->agent_id,
523-
"network_id" => $this->unid,
381+
"user_agent" => $this->ua,
524382
"profile_id" => $this->profile_id
525383
));
526384
return $ubid;
@@ -539,8 +397,8 @@ function save_request() {
539397
"visitor_ip" => $this->u_ip,
540398
"visitor_country" => $this->u_country_code,
541399
"cf_ray_id" => isset($this->s["HTTP_CF_RAY"]) ? $this->s["HTTP_CF_RAY"] : null,
542-
"browser_id" => $this->ubid,
543-
"network_id" => $this->unid
400+
"user_agent" => $this->ua,
401+
"browser_id" => $this->ubid
544402
));
545403
}
546404

@@ -568,14 +426,11 @@ function __construct($db_manager, $server, $cookies, $anonymousips = TRUE) {
568426
$this->d = $domain_parts[count($domain_parts) - 2] . "." . $domain_parts[count($domain_parts) - 1];
569427
} else { $this->d = $domain; }
570428
}
571-
$this->u_mobile = preg_match('/mobile/i', $this->ua) ? 1 : 0;
572-
$this->u_bot = (preg_match('/bot/i', $this->ua) || preg_match('/crawler/i', $this->ua)) ? 1 : 0;
573429
$this->u_language = isset($this->s["HTTP_ACCEPT_LANGUAGE"]) ? substr($this->s['HTTP_ACCEPT_LANGUAGE'], 0, 2) : null;
574430
$this->u_country_code = $this->get_country_code();
575431
$this->check_database();
576432
$this->isp_id = $this->get_isp();
577-
$this->unid = $this->get_network();
578-
$this->agent_id = $this->get_agent();
433+
$this->save_ip();
579434
$this->profile_id = $this->get_profile();
580435
$this->ubid = $this->indentify_browser();
581436
$this->save_request();

webstatistics.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
return;
3232
}
3333
$total_visitors = $web_analytics_db->count("wa_browsers");
34-
$total_networks = $web_analytics_db->count("wa_networks");
34+
$total_networks = $web_analytics_db->count("wa_ips");
3535
$total_isps = $web_analytics_db->count("wa_isps");
3636
$top_countries = array();
3737
$top_continents = array();
@@ -71,11 +71,11 @@
7171
}
7272
}
7373
$top_useragents = array();
74-
foreach($web_analytics_db->query("SELECT `agent_id`, COUNT(*) FROM wa_browsers GROUP BY `agent_id` ORDER BY COUNT(*) DESC;") as $useragent) {
74+
foreach($web_analytics_db->query("SELECT `user_agent`, COUNT(*) FROM wa_browsers GROUP BY `user_agent` ORDER BY COUNT(*) DESC;") as $useragent) {
7575
$top_useragents[$useragent[0]] = $useragent[1];
7676
}
7777
$top_isps = array();
78-
foreach($web_analytics_db->query("SELECT `isp_id`, COUNT(*) FROM wa_networks GROUP BY `isp_id` ORDER BY COUNT(*) DESC;") as $isp) {
78+
foreach($web_analytics_db->query("SELECT `isp_id`, COUNT(*) FROM wa_ips GROUP BY `isp_id` ORDER BY COUNT(*) DESC;") as $isp) {
7979
$top_isps[$isp[0]] = $isp[1];
8080
}
8181
$top_uris = array();

0 commit comments

Comments
 (0)