2

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?

Best,

1
  • 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
4

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')
  }
}
1
  • 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
2

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

function template_preprocess_page(&$vars) {

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


}
0

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.

0

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.