feat(database): ✨ add completely new analytics backend
This commit is contained in:
parent
31080cae2b
commit
5e2f0a7185
7 changed files with 446 additions and 130 deletions
66
www/src/setup/analytics.php
Normal file
66
www/src/setup/analytics.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
$data_time_start = microtime(true);
|
||||
|
||||
$analytics_enable_tail = false;
|
||||
$analytics_post = null;
|
||||
|
||||
function deduce_user_agent() {
|
||||
$real_agent=$_SERVER['HTTP_USER_AGENT'];
|
||||
|
||||
if(preg_match('/(Googlebot|\w*Google\w*)/', $real_agent, $match)) {
|
||||
return "bot/google/" . $match[1];
|
||||
}
|
||||
elseif(preg_match('/(Mozilla|Chrome|Chromium)/', $real_agent, $match)) {
|
||||
return "user/" . $match[1];
|
||||
}
|
||||
else {
|
||||
return "unidentified";
|
||||
}
|
||||
}
|
||||
|
||||
function analytics_is_user() {
|
||||
return preg_match('/^user/', deduce_user_agent());
|
||||
}
|
||||
|
||||
register_shutdown_function(function() {
|
||||
$data_end_time = microtime(true);
|
||||
|
||||
global $data_time_start;
|
||||
global $analytics_adapter;
|
||||
|
||||
global $REQUEST_PATH;
|
||||
global $REQUEST_QUERY;
|
||||
|
||||
global $analytics_enable_tail;
|
||||
|
||||
$data_time_end = microtime(true);
|
||||
|
||||
$http_referer = 'magic';
|
||||
if(isset($_SERVER['HTTP_REFERER'])) {
|
||||
$http_referer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
|
||||
}
|
||||
|
||||
$referrer = $REQUEST_QUERY['referer'] ?? $REQUEST_QUERY['ref'] ?? $http_referer;
|
||||
|
||||
$compute_time = $data_time_end - $data_time_start;
|
||||
|
||||
$analytics_adapter->log_path_access($REQUEST_PATH,
|
||||
deduce_user_agent(),
|
||||
$referrer,
|
||||
$compute_time);
|
||||
|
||||
if($analytics_enable_tail) {
|
||||
echo "<!-- Total page time was: " . $compute_time . " -->";
|
||||
}
|
||||
|
||||
if(isset($analytics_post)) {
|
||||
$analytics_post->increment_counter("compute_time", $compute_time);
|
||||
|
||||
if(analytics_is_user()) {
|
||||
$analytics_post->increment_counter("views");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue