1

I use Statistics module (Drupal core modules) on my site to get node's view count. How to disabled this function for role or anonym users?

Like that in template.php:

function MYTHEME_preprocess_page(&$vars) {
  if (!$logged_in) {
    // Code for disable Statistics module
  }
}
  • Sorry, don't understand your comment... – Platon Oct 1 '15 at 11:27
  • English is not my native language, I understand you just as the Google translate. So you could not be more easier to express thoughts? – Platon Oct 1 '15 at 11:42
  • Thank you, now became clear. I found this one module drupal.org/project/statistics_advanced in Google, but it is for Drupal 6.x... Wait, you're author of this module?! :) – Platon Oct 1 '15 at 11:51
  • Change question title. – Platon Oct 1 '15 at 12:11
  • @Platon Please don't change the meaning of your questions after they've already been answered, and especially after you've already accepted an answer. It's very bad form, and something we specifically don't allow here (wasting people's time is not a particularly nice thing to do). If you have a new question off the back of one that's already been answered, just ask another question. Thanks :) – Clive Oct 1 '15 at 14:07
0

I'm assuming you are looking for a way to "ignore" views by some roles or anonymous users, and not for "disable of the statistics module" for those roles/users (there is no way in Drupal to enable/disable a module for selected roles).

Below are some suggestions about contributed modules. In case none of them are an actual fit for what you're looking for, you may want to look at them to get inspiration that you could use in a custom module.

Node view count

You may want to use the Node view count module for this. Here is a quote about it (from its project page):

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

This module is integrated with Views module. You can use it to create different statistics pages, output new not viewed nodes for user and many more.

Specific to the D7 version of it, this is what is included also on the project page:

Also now we count node views for anonymous users.

Statistics Advanced

The Statistics Advanced (*) module is another option to consider. Here is a quote about it (from its project page):

Provides a few advanced options to the core Statistics module.

Be aware however of the issue "Port Statistics Advanced Settings to D7", with current status = needs review.

My "recommendation" for anybody looking for a D7 version of it, is to review what is attached to Comment # 55 in that issue. And report any potential deficiencies in that same issue (confirmations that it works fine would be appreciated too, also via that same issue).

Statistics Filter

Yet another module is the Statistics Filter module. Here is a quote about it (from its project page):

This module allows customizable filtering of hits from particular user roles or from crawlers.

Benefits:

For sites with light traffic (i.e., most sites), a large percentage of the gross hits recorded by statistics.module are either from the site administrator or from search engines. Filtering out these hits makes the collected statistics more accurately reflect traffic from real, human visitors.

Other modules to consider

If none of the above modules fit your needs, then you may want to consider reviewing this shortlist of possible alternatives (quotes are from their project pages).

  • Visitors (*):

    Displays information about visitors, and integrates with the WhoIs Lookup module

  • Better Statistics:

    introduces a Statistics API, which allows for a way to collect, limit, and route custom access statistics and data.

  • Counter

    ... counts how many visitors on your website. This module provides real time counting. All data saved to database.

  • User Visits (with related User Visits Advanced).

    ... tracks visits and referrer from other users on a user's profile page. It displays the latest visitors on a user's profile page and provides two blocks one with the latest visitors on a user's profile page and one with the latest visitors on own profile page.

    See User Visits Advanced for additional functionality.

  • Piwik Web Analytics:

    Adds the Piwik web statistics tracking system (free open source) to your website.

    Related module: Piwik Reports, which provides graphical reporting of your site's tracking data inside Drupal.

Disclosure: I'm the maintainer of the modules marked with (*).

2

The statistics module doesn't provide that option, or an easy way to inject that logic at first glance.

If you're using the AJAX counting method, you can implement hook_js_alter() to change the settings that the module adds, specifically altering the path it sends the tracking AJAX request to. You can change that to a file controlled by your own module, and you can make the decision whether or not to save the record based on the current user in there (after copying the code from the stats module's own statistics.php file).

If you're not using the AJAX method, there are a few different ways you could attack it, but each are more of a pain than it's probably worth. If the AJAX version isn't an option, I'd recommend writing your own custom module that incorporates the existing logic from Statistics that you need, plus your new logic.

The other options are doing things like trying to remove the Stats module's implementations of hook_exit() and/or hook_node_view() from your own module, and re-implementing them. This is never a great approach as you have to re-visit your module every time there's a core update to make sure nothing's changed.

Or, you could maintain a patch to the core module. Usual caveats apply if you go down that route obviously.

  • I'm not use AJAX. Example code please. – Platon Oct 1 '15 at 11:05
  • I don't have time to write you a module, that's your job! If you get stuck at specific parts, just ask new questions, and include the code you're having trouble with – Clive Oct 1 '15 at 11:08
  • Ok. I ask for sample code. If you bussy, give me link to function for answer my question or manual please. In google — nothing for this response! – Platon Oct 1 '15 at 11:13
  • I doubt there would be a manual/tutorial for this specific task (it's quite niche), but you could start with hook_module_implements_alter(), which can be used to remove another module's implementation of a hook. Once you've removed the hook you're interested in, re-implement it in your own module with the desired changes. Hopefully that's enough to get you going, and of course feel free to ask new questions about it if you hit a stumbling block – Clive Oct 1 '15 at 11:19

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.