6

I want to either use a module or write a module which allows me to count the number of Anonymous Users that are currently using the site.

What is the best method for this to be achieved? I looked at the statistics module but this does not enable me to count the data. I discovered views is capable of this with this setting, "Content statistics: Most recent views". But, there was no option for anonymous data.

1
  • 1
    It may not be possible to do this. By default anonymous users don't get a session in D7 until they do something such as a form submission that requires one, so there is no way to identify them individually and hence count them. – Alfred Armstrong Apr 30 '13 at 12:28
8

The admin_menu module does this, and provides this function:

/**
 * Counts how many users are active on the site.
 *
 * Counts how many users have sessions which have been active since the
 * specified time. Can count either anonymous sessions or authenticated
 * sessions.
 *
 * @param $timestamp
 *   A Unix timestamp. Users who have been active since this time will be
 *   counted. The default is 0, which counts all existing sessions.
 * @param $anonymous
 *   TRUE counts only anonymous users. FALSE counts only authenticated users.
 *
 * @return
 *   The number of users with sessions.
 *
 * @todo There are mostly no anonymous sessions anymore. Split this into a
 *   separate module providing proper user statistics.
 */
function admin_menu_session_count($timestamp = 0, $anonymous = TRUE) {
  $query = db_select('sessions');
  $query->addExpression('COUNT(sid)', 'count');
  $query->condition('timestamp', $timestamp, '>=');
  $query->condition('uid', 0, $anonymous ? '=' : '>');
  return $query->execute()->fetchField();
}

It returns the count of users who have been active in the past (default) 15 minutes like this:

    function admin_menu_get_user_count() {
      $interval   = REQUEST_TIME - variable_get('user_block_seconds_online', 900);
      $count_anon = admin_menu_session_count($interval, TRUE);
      $count_auth = admin_menu_session_count($interval, FALSE);

      return t('@count-anon / @count-auth', array('@count-anon' => $count_anon, '@count-auth' => $count_auth));
}
3

Depending on what you want to know the google analytics module could help you. It counts the number of unique users. This is only helpfull if you don't have to many registered users.

2
  • For most things the Google Analytics module works well for anonymous users and can even work pretty well for logged in users if the role settings are used in the contrib module. – Darrell Ulm Oct 22 '13 at 16:13
  • I would use Google Analytics and set a custom (GA) variable indicating whether the user is logged in. You can easily get this out of a body class using jQuery. – Menno van den Heuvel Aug 30 '18 at 18:24
1

For info related to visitors of a website, consider the visitors module as an alternative.
One of the things you get from it is a Visitors Block, which includes, among others, data about:

  • Total Visitors.
  • Unique Visitor.
  • Registered Users.
  • ...

Combining these 3 numbers is a possible estimate to "calculate" the number of unique not-registered (anonymous) users.

This module also comes with some reports & charts (a typical related requirement ...).

Its community docu and/or project page contains some more details. However its documentation pages still need quite some work to get completed. Refer to issue #2432369, for various topics that still need to be included somehow. The child issues of this issue already contain some pointers to documentation contained in existing issues. Testing the module in your own environment is a possible approach to find out what the module's capabilities really are.

Disclosure: I'm the new maintainer of this module,
I hope this does not violate the site's policy on self-promotion.

2
  • The OP is asking about anonymous users. – kiamlaluno Apr 5 '15 at 7:09
  • The project page doesn't explain how the module helps counting the number of anonymous users who are using the site. May you expand on that part? – kiamlaluno Apr 5 '15 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.