66 lines
No EOL
1.5 KiB
PHP
66 lines
No EOL
1.5 KiB
PHP
<?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");
|
|
}
|
|
}
|
|
});
|
|
|
|
?>
|