Show live user count on website with Google Analytics(使用 Google Analytics 在网站上显示实时用户数)
问题描述
如何通过 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 在网站上显示实时用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Google Analytics 在网站上显示实时用户数


- PHP - if 语句中的倒序 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01