Show the number of users in real time on a website using Google Analytics

How can I show the number of visitors on the Internet at any page load using Google Analytics?

For example, when a visitor loads a page somewhere, he will say "58 visitors online."

+6
source share
5 answers

As Trott explained earlier, there is no such function in analytics. However, I give you a very old alternative. I wrote this in 2004, so it is deprecated, but basically works. In addition, it works without using any databases. Sometimes you need retro solutions like this :)

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

(You need to install 777 chmod for your current folder, so users.dat can 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>'; ?> 
+3
source

This is not possible in analytics; however, you can do it yourself by executing a callback function in JavaScript, where every N seconds you start a heart-beat request to your server using XHR and include some kind of unique identifier. When a certain amount of time (more than N seconds) passes without a heart rate from this identifier, you can assume that the user is no longer actively working on this site. In addition, you can combine this with the visibility API to show only the set of users who are actively viewing the page (unlike users who have a page, but on the background tab).

+4
source

You can not. Google Analytics does not provide relevant data. You will need to find an alternative method.

(Since you noted a PHP question: a quick and dirty way could be to use PHP sessions and use time outs, for example, if the session is not active for 5 minutes, then they are not considered "online". You will need to make sure that you are updating the session each time the page loads. I suppose you will need to read the directory containing the session files and check the timestamps for the files. This is probably a terrible way to do this if your site needs to scale, but maybe OK for the initial conceptual three and dirty layout if that's all you do.)

+2
source

I see that Google released it in a beta version with limited access;

The real-time reporting API allows you to request real-time data for an authenticated user. This allows you to report on activities taking place on your property right now. You can use the real-time reporting API to query sizes and metrics to create client-side web widgets and dashboards.

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

0
source

Log in to Google and go to the service account

1) Select a project or create a project

2) Create a service account

3) Select a new secret key.

4) Click "Create."

detailed information

0
source

Source: https://habr.com/ru/post/894501/


All Articles