I created a Services Views, I added a Statistics field(total view) to the Views.

Then I try to use the browser to visit the node through RESTful api, like this url: http://daxuebao.local:8083/api/node/61

But, when I try to visit the Service Views, the Statistics field not effect, It still zero.

How to make Services support Statistics?

| improve this question | | | | |

Statistics increments hits in hook_exit i.e:


It's normally called as part of drupal_exit in drupal_page_footer

In any case, statistics_exit for the node counter actually checks

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) {}

That won't be true on an api request URL.

You might need to implement


check the node request and make the stats increment call yourself.

Something like:

 * Alter results of the services call.
 * @param $controller
 *   Controller definition
 * @param array $args
 *   Arguments passed to services callback
 * @param array $result
 *   Array of results that can be altered
 * @see services_controller_execute()
 * @see services.runtime.inc
function MODULE_services_request_postprocess_alter($controller, $args, &$result) {
  // Increment hit count for node view api requests.
  $nid = $result->nid; // May need to confirm this is the node id.
  if($controller['callback'] == '_node_resource_retrieve'){
    // See statistics_exit()
    if (variable_get('statistics_count_content_views', 0)) {
      // A node has been requested, so update the node's counters.
      db_merge('node_counter')->key(array('nid' => $nid))->fields(array(
        'daycount' => 1,
        'totalcount' => 1,
        'timestamp' => REQUEST_TIME,
      ))->expression('daycount', 'daycount + 1')->expression('totalcount', 'totalcount + 1')->execute();
| improve this answer | | | | |

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.