1

I am trying to setup a view where it shows the most popular articles and couldn't find any answer to this. My readers will all be anonymous as it's my company's blog.

Looking forward an answer as this is pretty useful on blogs!

Cheers!

  • What's you criteria for 'most popular'? Are you thinking basic page views or something more advanced like Radioactivity? (Awesome module by the way, integrates with views, might be worth a look anyway) – Clive Mar 21 '14 at 18:32
  • I was more into what articles are the most popular but I will havea look at the radioactivity module thanks! – GroovyMotion Mar 21 '14 at 20:17
2

If your site is relatively low traffic, you could possibly use the statistics module, or as @clive pointed out, the radioactivity module.

For higher traffic sites, I'd be wary of writing to the db for every visit, however. Moreover, if you're using varnish or some other reverse proxy caching system (which you probably should), Apache and PHP normally aren't invoked for anon visitors, so you'd need to do something different.

I've landed on using google analytics for this. To do it, the site obviously needs a google analytics account, and you need to install the google analytics and google analytics reports modules. Once you've configured them, you can fetch an array of the most visited nodes from the google analytics api. You can then simply serialise it, and save it to the variable table. This uses hook_cron, and needs to be done in a custom module:

function YOUR_MODULE_cron() {
  $last_time = variable_get('YOUR_MODULE_last_time', 0);
  // DO EVERY 24H
  if ((REQUEST_TIME - $last_time) > (60 * 60 * 24)) {
    $data = array();
    $dates = array();
    $params = array(
      'metrics' => array('ga:uniquePageviews'),
      'dimensions' => array('ga:pagePath'),
      'segment' => 'gaid::-11',
      'filters' => 'ga:pagePath=@/PATH/TO/NODES',
      'sort_metric' => array('-ga:uniquePageviews'),
      'start_date' => strtotime('-7 days'),
      'end_date' => strtotime('-1 day'),
    );
    $feed = google_analytics_api_report_data($params);
    if (!$feed->error) {
      $k = 0;
      $nids = array();
      foreach ($feed->results as $f) {
        // SAVE 6 MOST VIEWED
        if ($k < 6) {
          $nids[] = (int) array_pop(explode('/', drupal_lookup_path('source', ltrim($f['pagePath'], '/'))));
          $k++;
        }
      }
      $nids = serialize($nids);
      variable_set('YOUR_MODULE_ga_items', $nids);
      variable_set('YOUR_MODULE_last_time', REQUEST_TIME);
    }
  }
}

Every 24 hours, this would save a serialised array of the most popular nodes that appear under the path http://yoursite.com/PATH/TO/NODES/REST_OF_PATH. To use the nids, you'd simply unserialise the variable after retrieving it:

$nids = unserialize(variable_get('YOUR_MODUE_ga_items', '');
foreach($nids as $nid) {
   $node = node_load($nid);
   ...
}
  • This is pretty nice but I won't need such a level of complexity as it might take while before I have a lot of readers. But thanks for the analytics tip, I do use it every day – GroovyMotion Mar 21 '14 at 20:19
  • in that case, just go ahead and use statistics or radioactivity :) – Ollie Mar 21 '14 at 20:20

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.