Hi,
First of all let me thank you for this wonderful module...

I am working on a gaming portal.
I need to record each user's game page accesses too.
That is, in addition to the 3 operations (create, edit, delete) for the nodeactvity module, I also need to know when a user access a particular node (of a certain content type - 'game', in my case).
Would it be possible to get this stat too?
If not can you at least point me as to what needs to be tweaked to get this?
I am ok with having to work on the code a bit.

This is kind of urgent for me, that's why I am willing to get something working right away myself in case you guys won't be adding this feature in the near future.

Thanking you in advance.

Comments

sirkitree’s picture

Ah, I guess you kinda did open a new issue ;)

Extending this module, you should be able to do something like this. Nodeapi provides insert, update, delete (which we implement) but also view (which I don't think we currently are). So you would extend it to record on case 'view' the way it does with insert, update and delete.

paganwinter’s picture

Thanks sirkitree,
As I have mentioned here, I used the core Statistics module and the User Activity module to record the page views.
I am using the hook_exit API to record a page view.

Everything seemed to be working fine until I noticed that When a user clicks on the home link at the top of any page (Site title + Logo), the count gets duplicated in the table. This doesn't happen with the superuser though.
Haven't yet noticed this happening with any other module.

The code is almost the same as the Statistics module, except for the tables getting updated.

function user_page_access_exit($destination = NULL) {
  global $user;
//This below line is taken from the Statistics module. I am really not sure what it does :P
  drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
  _user_log_page_count($user);
}

function _user_log_page_count($user) {
    if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '' && $user->uid) {
      $nid = arg(1);
      // A node has been viewed, so update the node's counters.
      db_query('UPDATE {user_page_access} SET count = count + 1, timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid);
      // If we affected 0 rows, this is the first time viewing the node.
      if (!db_affected_rows()) {
        // We must create a new row to store counters for the new node.
        db_query('INSERT INTO {user_page_access} (uid, nid, count, timestamp) VALUES (%d, %d, 1, %d)', $user->uid, $nid, time());
      }
    }
}

What could be the problem here?
Else, is there any other fool proof way of getting a page hit?

Are you suggesting that hook_nodeAPI is a better option than hook_exit?
And what about hook_init?

UPDATE 1:
I have tried out hook_nodeapi. Still no luck.
It too gives me a duplicate entry.
Though this still seems a better bet because I have info on the node being acted on.
I only need to up the counter in case of nodes of a particular type. this might help me. Let me try it out too.

UPDATE 2:
hook_nodeapi seems to be a better option than hook_exit for me, as I have control over what kind of page is getting logged, and I only what visits to pages of a certain content type to be logged.
But now every visit is getting logged twice, even for the superuser.
This surely is not a problem for me right now as I can just straight away divid the count by 2 to get the actual count. But I would need a solution for later on.

sirkitree’s picture

I'm actually thinking that hook_exit() is better for this. First of all, let me explain your first bit of code commenting:
//This below line is taken from the Statistics module. I am really not sure what it does :P
From the API:

If you implement this hook and see an error like 'Call to undefined function', it is likely that you are depending on the presence of a module which has not been loaded yet. It is not loaded because Drupal is still in bootstrap mode.

So, you can try to run your function _user_log_page_count($user); without this and see what happens. I believe that db_query() is available at this point without having to bootstrap again, but I've not tested it. The sample code in the API for hook_exit() seems to suggest it is. See http://api.drupal.org/api/function/hook_exit/6 and http://api.drupal.org/api/function/drupal_bootstrap/6 for more.

Hook_nodeapi() will give you more results than you want because you can have multiple nodes show up on one page (such as the core front page, or if you're using Views2 and using node view instead of fields). As I understand your goal, you only want to record a view once/page.

As for hook_exit() giving you an additional count on the front page, I'm really not sure but I suspect it has something to do with bootstrapping. The API says that

Each phase adds to the previous one, so invoking a later phase automatically runs the earlier phases too.

which leads me to believe that maybe you're using the wrong phase, DRUPAL_BOOTSTRAP_PATH, but that is only a guess. I'm not too knowledgeable in this area. I'd suggest trying to ask someone in IRC on drupal_bootstrap() specifics when it comes to using hook_exit().

paganwinter’s picture

Here's the code that I am currently using:

function user_page_access_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  global $user;
  if ($op == 'view' && $node->type == 'game') {
    _user_log_page_count($user, $node);
  }
}

function _user_log_page_count($user, $node) {
      $nid = $node->nid;
      // A node has been viewed, so update the node's counters.
      db_query('UPDATE {user_page_access} SET count = count + 1, timestamp = %d WHERE uid = %d AND nid = %d', time(), $user->uid, $nid);
      // If we affected 0 rows, this is the first time viewing the node.
      if (!db_affected_rows()) {
        // We must create a new row to store counters for the new node.
        db_query('INSERT INTO {user_page_access} (uid, nid, count, timestamp) VALUES (%d, %d, 1, %d)', $user->uid, $nid, time());
      }
}

Now the behavoiur is absolutely erratic!
Certain nodes are causing double entries. While some others are working fine.
Initially everything was fine for the admin, but after another test user accessed those troublesome pages, admin's visits too are getting logged twice for those nodes!
:-\

I think I'll try again with hook_exit().
If Statistics depends on that, why can't my module work with that?!?

paganwinter’s picture

I just noticed that even the Statistics module is logging those nodes twice!

paganwinter’s picture

If you overlook that detail, I have somewhat of a raw module that logs each user's page visits and displays a block of the top users based on that statistic.

I am really new to Drupal and am not really very sure about the Drupal best practices.
If any one is interested they can have a look at the code, and probably refine it.

As of now, theres no admin control on the statistics.
There's just a page that outputs the raw stats.
Will refine it if time permits.

dave reid’s picture

@paganwinter #5: If you're logging using both hook_exit and hook_nodeapi, then yes, it will count it twice.

paganwinter’s picture

I haven't been using both the APIs.
Anyways I am currently tied up with some other work. So haven't been able to work on this for a while now...
You might as well close this issue if no one else seems to be facing it...

sirkitree’s picture

Status: Active » Closed (works as designed)
GolDRoger’s picture

Boys, I'm a noob in php, it's possible modify this script for a block, putting the name (maybe the avatar) of an user when visit for example the node type profile of user_x?

This just for create a user visits list for every user Who create a profile. Because the user visits module doesn't records personal content types :) (for who use content profile)

can u help making examples please? :)

thx a lot :)