2

There isn't much info around it, but when I'm using Statistic module to get the views per node it's only working when I'm visiting the page of the specific node. When I'm using an API call to a specific node, the statistic module doesn't increase with 1...

Is there a way to solve this?

  • you want get statistics of node? – Yusef May 16 '16 at 11:33
2

Node views are only counted when node_view() is invoked (i.e. when the node is actually viewed).

When retrieving a node via services you're not, conceptually, "viewing" it, so you shouldn't expect the view count to increase.

In order to get your desired workflow, you'll probably need to implement a custom services resource which updates the statistics manually when a node object is requested. See How to create a custom resource with Services-3.x for more info.

1

Another solution is Ajaxly update node counter , it not require custom module or custom resource service.

First, make sure you enable ajaxly update node counter in statistics settings (admin/config/system/statistics and checkUse Ajax to increment the counter) and then

put this code in callback of your service ( replace yournid with your content nid).

$.ajax({
  type: "POST",
  cache: false,
  url: Drupal.settings.statistics.url,
  data: {"nid":yournid}
});

});

update
change

  url: Drupal.settings.statistics.url,     

to

 url : 'http://yourdomain/modules/statistics/statistics.php',     

enter image description here

  • Do I put this in my js file? What's the URL because it says Drupal isn't defined – Greg May 16 '16 at 12:05
  • in js file, exactly after you get service results, take look at DOM objects in firebug. you can see Drupal. – Yusef May 16 '16 at 12:07
  • My project isn't related to Drupal, I'm only making an API call to get data from a Drupal website. So 'drupal' isn't defined in my js file – Greg May 16 '16 at 12:10
  • ok. I update my answer – Yusef May 16 '16 at 12:12
  • 1
    Someone's going to need to code something @Greg, none of the modules involved have the functionality you require unfortunately. It's just the nature of open source software – Clive May 16 '16 at 13:18
0

You can try update node counter after your service resonse with

if (is_numeric($nid)) {
  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();
}

This is back-end solution and in another answer Front-end solution Provided

  • Is this for Services API call? I can get the total view when I'm on the actual page, but I'm trying to increase the number when I'm making a API call – Greg May 16 '16 at 11:42
  • @Greg you want manually increase view counter? – Yusef May 16 '16 at 11:43
  • No the Statistic module needs to do this, but it's not working with Services – Greg May 16 '16 at 11:45
  • @Greg I update my answer, put this code after you service reply. – Yusef May 16 '16 at 11:50
  • Which file should I put that? Sorry, I'm more of a front-end developer so that's new to me. – Greg May 16 '16 at 11:51

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.