How can I show the number of visitors online at any page load via Google analytics?
如何通过Google分析在任何页面加载中显示在线访问者数量?
E.g., when a visitor loads the page somewhere it will say "58 Visitors Online".
例如,当访问者在某处加载页面时,它将说“58访客在线”。
5 个解决方案
#1
3
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 :)
正如Trott之前解释的那样,分析中没有这样的功能。但是,我给你一个非常古老的选择。我在2004年写了这个,所以它过时了,但基本上都有效。此外,它不使用任何数据库工作..有时你需要像这样的复古解决方案:)
Live demo: kopli.pri.ee/*/6976362.php
现场演示:kopli.pri.ee/*/6976362.php
(You need to set 777 chmod for your current folder, so users.dat could be created automatically)
(您需要为当前文件夹设置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))."\n";
fputs($fp, $user);
}
if (!$found) {
fputs($fp, $REMOTE_ADDR."|".time()."\n");
$user_count++;
}
fclose($fp);
echo 'Active users <b>' . $user_count . '</b>';
?>
#2
4
This cannot be done in analytics; however, you can do this yourself by having a ping-back function in JavaScript, where every N seconds, you kick-off a "heart-beat" request to your server, using XHR, and include some sort of unique ID. When some amount of time (more than N seconds) passes without a heart-beat from a given ID, you can assume that the user is no longer actively on that site. Moreover, you can combine this with the visibility APIs to show only the set of users who are actively viewing the page (as opposed to users who have the page open but in a background tab).
这不能在分析中完成;但是,您可以通过在JavaScript中使用ping-back功能自己完成此操作,其中每N秒,您使用XHR启动对服务器的“心跳”请求,并包含某种唯一ID。当某段时间(超过N秒)没有来自给定ID的心跳时,您可以假设用户不再活跃在该站点上。此外,您可以将其与可见性API结合使用,以仅显示主动查看页面的用户集(而不是打开页面但在后台选项卡中的用户)。
#3
2
You can't. Google Analytics doesn't provide up-to-the-minute data. You will have to find an alternative method.
你不能。 Google Analytics不提供最新数据。您将不得不寻找替代方法。
(Since you tagged the question PHP: A quick-and-dirty way might be to leverage PHP sessions and use a time cut-off, like if a session is not active within 5 minutes, then they are not considered "online". You will need to make sure you update the session on every page load. I imagine you'll have to read the directory containing the session files and check the timestamps of the files. This is probably a terrible way to do it if your site needs to scale way up, but probably OK for an initial proof-of-concept quick-and-dirty mock-up if that's all you're doing.)
(因为你标记了问题PHP:快速而肮脏的方式可能是利用PHP会话并使用时间截止,就像会话在5分钟内没有活动一样,那么它们不被视为“在线”。你将需要确保你在每个页面加载时更新会话。我想你必须阅读包含会话文件的目录并检查文件的时间戳。如果你的网站需要,这可能是一种可怕的方法缩小规模,但如果这就是你正在做的事情,那么最初的概念验证快速和肮脏的模型可能还可以。)
#4
0
I see Google has released this in limited access beta;
我看到Google已经发布了有限访问测试版;
The Real Time Reporting API enables you to request real time data for an authenticated user. This allows you to report on the activity occurring on your property right now. You can use the Real Time Reporting API to query for dimensions and metrics in order to build customer facing web widgets and dashboards.
通过Real Time Reporting API,您可以为经过身份验证的用户请求实时数据。这允许您立即报告您的财产上发生的活动。您可以使用实时报告API查询维度和指标,以构建面向客户的Web小部件和仪表板。
https://developers.google.com/analytics/devguides/reporting/realtime/v3/
#5
0
Login into google and go to Service account
登录谷歌并转到服务帐户
1) Select a project or create a project
1)选择项目或创建项目
2) Create service account
2)创建服务帐户
3) Select furnish a new private key.
3)选择提供新的私钥。
4) Click create.
4)单击“创建”。
#1
3
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 :)
正如Trott之前解释的那样,分析中没有这样的功能。但是,我给你一个非常古老的选择。我在2004年写了这个,所以它过时了,但基本上都有效。此外,它不使用任何数据库工作..有时你需要像这样的复古解决方案:)
Live demo: kopli.pri.ee/*/6976362.php
现场演示:kopli.pri.ee/*/6976362.php
(You need to set 777 chmod for your current folder, so users.dat could be created automatically)
(您需要为当前文件夹设置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))."\n";
fputs($fp, $user);
}
if (!$found) {
fputs($fp, $REMOTE_ADDR."|".time()."\n");
$user_count++;
}
fclose($fp);
echo 'Active users <b>' . $user_count . '</b>';
?>
#2
4
This cannot be done in analytics; however, you can do this yourself by having a ping-back function in JavaScript, where every N seconds, you kick-off a "heart-beat" request to your server, using XHR, and include some sort of unique ID. When some amount of time (more than N seconds) passes without a heart-beat from a given ID, you can assume that the user is no longer actively on that site. Moreover, you can combine this with the visibility APIs to show only the set of users who are actively viewing the page (as opposed to users who have the page open but in a background tab).
这不能在分析中完成;但是,您可以通过在JavaScript中使用ping-back功能自己完成此操作,其中每N秒,您使用XHR启动对服务器的“心跳”请求,并包含某种唯一ID。当某段时间(超过N秒)没有来自给定ID的心跳时,您可以假设用户不再活跃在该站点上。此外,您可以将其与可见性API结合使用,以仅显示主动查看页面的用户集(而不是打开页面但在后台选项卡中的用户)。
#3
2
You can't. Google Analytics doesn't provide up-to-the-minute data. You will have to find an alternative method.
你不能。 Google Analytics不提供最新数据。您将不得不寻找替代方法。
(Since you tagged the question PHP: A quick-and-dirty way might be to leverage PHP sessions and use a time cut-off, like if a session is not active within 5 minutes, then they are not considered "online". You will need to make sure you update the session on every page load. I imagine you'll have to read the directory containing the session files and check the timestamps of the files. This is probably a terrible way to do it if your site needs to scale way up, but probably OK for an initial proof-of-concept quick-and-dirty mock-up if that's all you're doing.)
(因为你标记了问题PHP:快速而肮脏的方式可能是利用PHP会话并使用时间截止,就像会话在5分钟内没有活动一样,那么它们不被视为“在线”。你将需要确保你在每个页面加载时更新会话。我想你必须阅读包含会话文件的目录并检查文件的时间戳。如果你的网站需要,这可能是一种可怕的方法缩小规模,但如果这就是你正在做的事情,那么最初的概念验证快速和肮脏的模型可能还可以。)
#4
0
I see Google has released this in limited access beta;
我看到Google已经发布了有限访问测试版;
The Real Time Reporting API enables you to request real time data for an authenticated user. This allows you to report on the activity occurring on your property right now. You can use the Real Time Reporting API to query for dimensions and metrics in order to build customer facing web widgets and dashboards.
通过Real Time Reporting API,您可以为经过身份验证的用户请求实时数据。这允许您立即报告您的财产上发生的活动。您可以使用实时报告API查询维度和指标,以构建面向客户的Web小部件和仪表板。
https://developers.google.com/analytics/devguides/reporting/realtime/v3/
#5
0
Login into google and go to Service account
登录谷歌并转到服务帐户
1) Select a project or create a project
1)选择项目或创建项目
2) Create service account
2)创建服务帐户
3) Select furnish a new private key.
3)选择提供新的私钥。
4) Click create.
4)单击“创建”。