How to get node visits in access log

You, DrupalDrupal module
Back

Some times you might already have tried to create accesslog views sometimes this might be the first time you see that accesslog views existing in views. No big deal, statistics module has great views integration. If you do not already enable the Statistics module enable the statistics and in admin>reports enable accesslog.

When you create new views you can see Accsslog as below ;o).

View type:

Term Taxonomy terms are attached to nodes. User Users who have created accounts on your site. Access log

There is no any implementation to the nodes with accesslog, but we are going to discuss how we can generate reports like nodesccess count for certain time period (which is not available in Statatistic module). Oki we are going to create node_statistics module.

Module Name : node_statistics

  1. Create .info file. Create node_statistics.info
; $Id:
name = Node Statistics
description = Integration of node hits to access log and visible in views
core = 6.x
dependencies[] = views
  1. Create .install file. Create node_statistics.install file
<?php
// $Id$

/**
* Implementation of hook_install().
*/
function node_statistics_install() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {accesslog} ADD COLUMN nid int(11) unsigned NOT NULL default 0 AFTER uid");
      $ret[] = update_sql("ALTER TABLE {accesslog} ADD INDEX nid");
      break;
    case 'pgsql':
    // @TODO : add index
//      db_add_column($ret, 'accesslog', 'nid', 'integer unsigned', array('not null' => TRUE, 'default' => 0));
  }
  return $ret;
}

/**
* Implementation of hook_uninstall().
*/
function node_statistics_uninstall() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {accesslog} DROP COLUMN nid");
      $ret[] = update_sql("ALTER TABLE {accesslog} DROP INDEX nid");
      break;
    case 'pgsql':
    // @TODO : DROP index
  }
  return $ret;
}
?>
  1. Create .module file which is node_statistics.module
<?php
// $Id$

/**
* hook_views_api - registers the module with the Views API
* This specific example tells Drupal to use version 2 of the API
*/
function node_statistics_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'node_statistics'),
  );
}

/**
* Implementation of hook_exit().
*
* This is where statistics are gathered on page accesses.
*/
function node_statistics_exit() {
  global $user, $recent_activity;

  drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);

  if (variable_get('statistics_enable_access_log', 0)) {
    // We are counting content views.
    if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
      $session_id = session_id();
      $nid = arg(1);
      // This might not be the best way to do this?
      db_query("UPDATE {accesslog} SET nid = %d WHERE path ='%s' AND hostname ='%s'
        AND uid =%d AND nid = 0 AND sid ='%s' AND timestamp = %d",
          $nid, $_GET['q'], ip_address(),
          $user->uid, $session_id, time());
      if (!db_affected_rows()) {
        db_query("INSERT INTO {accesslog}
        (title, path, url, hostname, uid, nid, sid, timer, timestamp)
        values('%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d)",
            strip_tags(drupal_get_title()), $_GET['q'], referer_uri(), ip_address(),
            $user->uid, $nid, $session_id, timer_read('page'), time());
      }
    }
  }
}
?>
  1. Now integrate with views, create node_statistics.views.inc
<?php
// $Id$

/**
* Implementation of hook_views_data_alter().
*/
function node_statistics_views_data_alter(&$data) {
  // user
  $data['accesslog']['nid'] = array(
    'title' => t('Node'),
    'help' => t('A user visit to the node.'),
    'relationship' => array(
      'handler' => 'views_handler_relationship',
      'base' => 'node',
      'base field' => 'nid',
    ),
  );
}
?>

Once you add and enable the module then try creating new accesslog views. Then go to relationships, add new, now you can see node field is there, so you can can associate with node tables now :o).

© Heshan Wanigasooriya.RSS

🍪 This site does not track you.