问题描述
如何通过 Google 分析显示任何页面加载时的在线访问者数量?
How can I show the number of visitors online at any page load via Google analytics?
例如,当访问者在某处加载页面时,它会显示58 个在线访问者".
E.g., when a visitor loads the page somewhere it will say "58 Visitors Online".
推荐答案
正如 Trott 之前所解释的,分析中没有这样的功能.但是,我给你一个非常非常古老的选择.我是在 2004 年写的,所以它已经过时了,但基本上有效.此外,它在不使用任何数据库的情况下工作......有时你需要像这样的复古解决方案:)
As Trott explained before, there is no such function in analytics. However, I give you a very very old alternative. I wrote this in 2004, so its outdated, but basically works. Also, it works without using any databases.. sometimes you need retro-solutions like this :)
现场演示:kopli.pri.ee/stackoverflow/6976362.php
(您需要为当前文件夹设置 777 chmod,以便自动创建 users.dat)
<?php
$current_users_file = 'users.txt';
if (!file_exists($current_users_file)) fclose(fopen($current_users_file, "w"));
$users = file($current_users_file);
$found = false;
$user_count = count($users);
$fp = fopen($current_users_file, "w");
foreach($users as $user) {
$user = explode("|", $user);
if ($user[1]+300 < time()) {
$user_count--;
continue;
} elseif ($user[0] == $REMOTE_ADDR) {
$user[1] = time();
$found = true;
}
$user = trim(implode("|", $user))."
";
fputs($fp, $user);
}
if (!$found) {
fputs($fp, $REMOTE_ADDR."|".time()."
");
$user_count++;
}
fclose($fp);
echo 'Active users <b>' . $user_count . '</b>';
?>
这篇关于使用 Google Analytics 在网站上显示实时用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!