0

I'd like to display the number of unique node views (= how many times the full node page is visited by a unique user) in my nodes' teasers. I've tried to use the Statistics module, but in that case, all reads are stored.

Is there a way to only count the unique views? Which code should I use in my node.tpl.php file to display this information?

0

1 Answer 1

2

You can try Node View Count module.

Node View Count module allows to count node views. Unlike statistics module, this module records each view of node with user uid and timestamp. So now you can know how many times a user viewed node by day, week, month and etc.

To print the count in teasers.

  1. Implement hook_node_view()
  2. Check the $node->type and $view_mode
  3. Call nodeviewcount_db_count_node_views($node, $user = NULL, $distinct_users = FALSE) to get the count
  4. Add the count to $node->content
  5. In your template file print the count that you added.

Update:

You cannot rely on IP for uniqueness, but if you want to count unique views based on IP then follow one of the below steps.

  1. The accesslog table will contain all the access logs with IP details, you can query this table for path like node/nid and group by IP to get the count, One thing to note is that accesslog will be cleared periodically, if you are going to use this table then you have to make sure that it won't be cleared by selecting never in Discard access logs older than. I will not suggest doing this way because this table will contain access log for all pages, though there is a moule Better Statistics using which you can restrict statistics to log access details only for node pages.
  2. Or, you can create a custom module and create a custom table somewhat similar to accesslog and then implement hook_node_view() to populate the table, later you can query the table to get the count.
2
  • I'd like to count anonymous views from different computers seperately in the "distinct users count". However, while testing locally, I've figured out that anonymous views are considered as 1 single view. Is this caused by the fact that I always visited the website from the same computer or is what I'm looking for a missing feature? I suppose it's the latter, because I don't see anything like an IP-adress in the database and the code in the nodeviewcount.db.inc file seems to only consider the uids. But maybe my interpretation is wrong.
    – Jeroen
    Jun 30, 2014 at 21:41
  • 1
    If your site is high traffic, you could also use the google-analytics-based solution outlined here, to avoid writing to the database for every page view: drupal.stackexchange.com/questions/107456/… That solution fetches an array of the most viewed nodes,which isn't quite what you want, but the GA API would allow you to get lots of other information, too. :)
    – Ollie
    Jul 1, 2014 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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