I have a web site where each node has three diferents designs (i do it through panels and arguments) and i use the statistics module, that way i can see how many visits has the node.

But the statistics module jus count the visits when they visit the main design. I can explain with a example:

  • website .com/node/01 <---when a user visit this url, a visit is counted
  • website .com/node/01/mobile <----- here is not counted the visit
  • website .com/node/01/lightbox <----here is not counted the visit

This was built for panels and arguments... so, is there anyway to put a php code in two panels that visitors don't have to start to count?


  • 1
    If I am not mistaken there is a setting in the Statistics module to do the updates via AJAX. There is also a js file in that module that does an call to statistics.php directly (bypassing index.php). You need to include js that does that call to statistics.php sending the nid as a parameter. Oct 14 '14 at 19:20

It's because panels doesn't invoke node_view(). Please try following code:

function MODULE_ctools_render_alter(&$info, &$page, &$context) {
  if ($context['handler']->task == 'node_view') {
    $key = $context['handler']->conf['context'];
    $node = $context['contexts'][$key]->data;
    statistics_node_view($node, 'full')
  • It appears to me, the statistics_node_view() function is not incrementing. I saw db_merge() code in statistics_exit() though.
    – user18099
    Jul 4 '17 at 13:16

You can do it by using theme template preprocess function ie:

function template_preprocess_page(&$vars) {

 $statistics = '(function ($) {
  $(document).ready(function() {
      type: "POST",
      cache: false,
      url: "'.url(drupal_get_path('module', 'statistics') . '/statistics.php').'",
      data: "nid='.arg(1).'"
   drupal_add_js($statistics, array('type' => 'inline', 'scope' => 'footer'));


I took the db_merge() code from https://api.drupal.org/api/drupal/modules%21statistics%21statistics.module/function/statistics_exit/7.x

That is

db_merge('node_counter')->key(array('nid' => arg(1)))->fields(array(
        'daycount'   => 1,
        'totalcount' => 1,
        'timestamp'  => REQUEST_TIME,
      ))->expression('daycount', 'daycount + 1')->expression('totalcount',     'totalcount + 1')->execute();

... inside my own _ctools_render_alter() hook function. Hence, incrementing it manually.


Solution for Drupal 8.

function MODULE_panels_build_alter(array &$build, $panels_display) {
        if ($nid = \Drupal::routeMatch()->getRawParameter('node')) {
                $build['#attached']['library'][] = 'statistics/drupal.statistics';
                $settings = ['data' => ['nid' => $nid], 'url' => \Drupal::request()->getBasePath() . '/' . drupal_get_path('module', 'statistics') . '/statistics.php'];
                $build['#attached']['drupalSettings']['statistics'] = $settings;

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.