开发者

Show live user count on website with Google Analytics

How can I show the number of visitors online at any page load via Google analytics?

E.g.,开发者_运维百科 when a visitor loads the page somewhere it will say "58 Visitors Online".


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).


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 :)

Live demo: kopli.pri.ee/stackoverflow/6976362.php

(You need to set 777 chmod for your current folder, so users.dat could be created automatically)

<?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>';
?>


You can't. Google Analytics doesn't provide up-to-the-minute data. You will have to find an alternative method.

(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.)


I see Google has released this in limited access beta;

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.

https://developers.google.com/analytics/devguides/reporting/realtime/v3/


Login into google and go to Service account

1) Select a project or create a project

2) Create service account

3) Select furnish a new private key.

4) Click create.

detailed info

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜