Introduction
The statistics module keeps track of numerous statistics for your site but be warned, statistical collection does cause a little overhead, thus everything comes disabled by default.
The module counts how many times, and from where -- using HTTP referrer -- each of your posts is viewed. Once we have that count the module can do the following with it:
Notes on using the statistics:
As with any new module, the statistics module needs to be enabled before you can use it. Also refer to the permissions section, as this module supports four separate permissions.
This admin page shows you site-wide referrer statistics. You can see 'all' statistics, 'external' statistics or 'internal' statistics. Default is 'all'.
This admin page gives you an at-a-glance look at your most popular content. It is useful for understanding what content on your Drupal site is the most popular. Also on this page are links to the referrer statistics for each listed node.
There are some configuration options added to the main administer » settings » statistics section:
This module creates a block that can display the day's top viewed content, the all time top viewed content, and the last content viewed. Each of these links can be enabled or disabled individually, and the number of posts displayed for each can be configured with a drop down menu. If you disable all sections of this block, it will not appear.
Don't forget to enable the block.
This module creates a user page that can display summaries of the day's most popular viewed content, the all time most popular content, and the last content viewed. Each of these summaries can be enabled or disabled individually, and the number of posts displayed for each can be configured with a drop down menu. You can also assign a name for the automatically generated link to the user page. If no name is set, the link will not be displayed.
This module has four permissions that need to be configured in the permissions section.
If 'administer statistics' and 'access statistics' are both enabled, the user will see a link from each node to that node's referrer statistics (if enabled).
", array('%modules' => url('admin/modules'), '%permissions' => url('admin/user/configure/permission'), '%referer' => url('admin/logs/access/referrers'), '%access' => url('admin/logs/access'), '%configuration' => url('admin/settings/statistics'), '%block' => url('admin/block'))); case 'admin/modules#description': return t('Logs access statistics for your site.'); case 'admin/settings/statistics': return t('Settings for the statistical information that Drupal will keep about the site. See site statistics for the actual information.', array('%statistics' => url('admin/logs/topnodes'))); case 'admin/logs/topnodes': return t('This page gives you an at-a-glance look at your most popular content.'); case 'admin/logs/access/titles': return t('This page shows access statistics for each page of your website.'); case 'admin/logs/access/users': return t('This page shows access statistics for each user of your website.'); case 'admin/logs/access/hostnames': return t('This page shows access statistics for each hostname visiting your website.'); case 'admin/logs/access/referrers': return t('This page shows your site-wide referrer statistics. You can optionally view just the "external referrers" or the "internal referrers". Referrers are web pages, both local and on other sites, that point to your web site.'); case 'admin/logs/access/referrers/internal': return t('This page shows you only "internal referrers". These are links pointing to your web site from within your web site.'); case 'admin/logs/access/referrers/external': return t('This page shows you only "external referrers". These are links pointing to your web site from outside your web site.'); case strstr($section, 'admin/logs/access'): return t("This page shows you who is accessing your web site. You can see the hostnames and referrers. In particular, it is easy to inspect a user's navigation history/trail by clicking on track user."); } } /** * Implementation of hook_exit(). * * This is where statistics are gathered on page accesses. */ function statistics_exit() { global $user, $recent_activity; if (variable_get('statistics_count_content_views', 0)) { // We are counting content views. if ((arg(0) == 'node') && arg(1)) { // A node has been viewed, so update the node's counters. db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1)); // If we affected 0 rows, this is the first time viewing the node. if (!db_affected_rows()) { // We must create a new row to store counters for the new node. db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES(%d, 1, 1, %d)', arg(1), time()); } } } if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') < 5)) { // Statistical logs are enabled. $referrer = referer_uri(); $hostname = $_SERVER['REMOTE_ADDR']; $path = (drupal_get_path_alias($_GET['q'])) ? drupal_get_path_alias($_GET['q']) : $_GET['q']; // Log this page access. db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, timestamp) values('%s', '%s', '%s', '%s', %d, %d)", drupal_get_title(), $path, $referrer, $hostname, $user->uid, time()); } } /** * Implementation of hook_perm(). * * The following permissions are defined: * - "administer statistics module": full administrative control of module * - "administer statistics": view statistics / referrer log * - "access statistics": see how many times individual nodes have been * viewed (if enabled) */ function statistics_perm() { return array('administer statistics module', 'administer statistics', 'access statistics'); } /** * Implementation of hook_link(). */ function statistics_link($type, $node = 0, $main = 0) { global $id; $links = array(); if ($type != 'comment' && user_access('access statistics') && variable_get('statistics_display_counter', 0)) { $statistics = statistics_get($node->nid); if ($statistics) { if (user_access('administer statistics')) { $links[] = l(format_plural($statistics['totalcount'], '1 read', '%count reads'), 'admin/logs/access/title/'. urlencode($node->title)); } else { $links[] = format_plural($statistics['totalcount'], '1 read', '%count reads'); } } } if ($type == 'page' && user_access('access content')) { $userlink = variable_get('statistics_userpage_link', ''); if ($userlink) { $links[] = l(t($userlink), 'statistics', array('title' => t("View this site's most popular content."))); } } return $links; } /** * Implementation of hook_menu(). */ function statistics_menu() { $items = array(); $items[] = array('path' => 'statistics', 'title' => t('most popular content'), 'callback' => 'statistics_page', 'access' => user_access('access content'), 'type' => MENU_SUGGESTED_ITEM); $access = user_access('administer statistics module') || user_access('administer statistics'); $items[] = array('path' => 'admin/logs/topnodes', 'title' => t('top nodes'), 'callback' => 'statistics_admin_topnodes', 'access' => $access, 'weight' => 1); $items[] = array('path' => 'admin/logs/access', 'title' => t('access'), 'callback' => 'statistics_admin_displaylog', 'access' => $access, 'weight' => 3); $items[] = array('path' => 'admin/logs/access/titles', 'title' => t('titles'), 'callback' => 'statistics_top_titles', 'access' => $access, 'weight' => 1); $items[] = array('path' => 'admin/logs/access/users', 'title' => t('users'), 'callback' => 'statistics_top_users', 'access' => $access, 'weight' => 2); $items[] = array('path' => 'admin/logs/access/hostnames', 'title' => t('hostnames'), 'callback' => 'statistics_top_hostnames', 'access' => $access, 'weight' => 3); $items[] = array('path' => 'admin/logs/access/referrers', 'title' => t('referrers'), 'callback' => 'statistics_top_referrers', 'access' => $access, 'weight' => 4); $items[] = array('path' => 'admin/logs/access/referrers/internal', 'title' => t('internal'), 'access' => $access); $items[] = array('path' => 'admin/logs/access/referrers/external', 'title' => t('external'), 'access' => $access); return $items; } function statistics_admin_topnodes_table() { $header = array( array('data' => t('title'), 'field' => 'n.title'), array('data' => t('today'), 'field' => 's.daycount', 'sort' => 'desc'), array('data' => t('all time'), 'field' => 's.totalcount'), array('data' => t('last hit'), 'field' => 's.timestamp'), array('data' => t('operations')) ); $sql = 'SELECT s.nid, s.daycount, s.totalcount, s.timestamp, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid'; $sql .= tablesort_sql($header); $result = pager_query($sql, 20); // WHERE s.%s <> '0' while ($nid = db_fetch_array($result)) { $rows[] = array(l($nid['title'], 'node/'. $nid['nid'], array('title' => t('View this posting.'))), $nid['daycount'], $nid['totalcount'], format_date($nid['timestamp'], 'small'), l('track node', "admin/logs/access/node/$nid[nid]")); } if ($pager = theme('pager', NULL, 20, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 5)); } return theme('table', $header, $rows); } /** * Menu callback; presents the "Access logs" page. * * @param $type * - "user": display accesses for a particular user. * - "title": display accesses of a particular title. * - "host": display accesses originated at a given IP. * - "all": display all accesses. * * @param $value * The user, host, or node to filter by. */ function statistics_admin_displaylog($type = 'all', $id = 0) { switch ($type) { case 'user': if ($id) { // retrieve recent access logs for specific user $id $user = user_load(array('uid' => $id)); $page_title = t('Recent access logs for "%username"', array('%username' => $user->name)); $sql = 'SELECT title, path, url, hostname, uid, timestamp FROM {accesslog} WHERE uid = \''. check_query($id) ."'"; } else { // retrieve recent access logs for all users (not guests) $page_title = t('Recent access logs for all users'); $sql = 'SELECT title, path, url, hostname, uid, MAX(timestamp) AS timestamp FROM {accesslog} WHERE uid <> 0 GROUP BY uid, title, path, url, hostname'; } break; case 'title': // retrieve recent access logs for title $id $page_title = t('Recent access logs for "%title"', array('%title' => $id)); $sql = 'SELECT title, path, url, hostname, uid, timestamp FROM {accesslog} WHERE title = \''. check_query($id) ."'"; break; case 'host': // retrieve recent access logs for hostname $id $page_title = t('Recent access logs for "%hostname"', array('%hostname' => $id)); $sql = 'SELECT title, path, url, hostname, uid, timestamp, title FROM {accesslog} WHERE hostname = \''. check_query($id) ."'"; break; case 'all': default: // retrieve all recent access logs $page_title = t('Recent access logs'); $sql = 'SELECT title, path, url, hostname, uid, timestamp FROM {accesslog}'; } $header = array( array('data' => t('timestamp'), 'field' => 'timestamp', 'sort' => 'desc'), array('data' => t('title'), 'field' => 'title'), array('data' => t('user'), 'field' => 'uid'), array('data' => t('hostname'), 'field' => 'hostname'), array('data' => t('referrer'), 'field' => 'url'), array('data' => t('operations'), 'colspan' => '3') ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50); while ($log = db_fetch_object($result)) { // display title if possible, otherwise display path if ($log->title) $title = l(_statistics_column_width($log->title), urlencode($log->path), array('title' => $log->path)); else $title = '('. l(_statistics_column_width($log->path), urlencode($log->path), array('title' => $log->path)) .')'; // display url if possible, constructing our own link as may not be local if ($log->url) $url = "url\" title=\"$log->url\">". _statistics_column_width($log->url) .''; else $url = message_na(); $user = user_load(array('uid' => $log->uid)); $rows[] = array(array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'), $title, format_name($user), $log->hostname ? $log->hostname : message_na(), $url, ($user->uid ? l(t('track user'), "admin/logs/access/user/$user->uid") : ''), ($log->title ? l(t('track title'), 'admin/logs/access/title/'. urlencode($log->title)) : ''), ($log->hostname ? l(t('track host'), "admin/logs/access/host/$log->hostname") : '')); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 8)); } $output = theme('table', $header, $rows); print theme('page', $output, $page_title); } /** * Menu callback; presents the "Top titles" page. */ function statistics_top_titles() { $sql = "SELECT title, path, MAX(timestamp) AS last_view, COUNT(title) AS count FROM {accesslog} WHERE title <> '' GROUP BY title"; $sql_cnt = "SELECT COUNT(DISTINCT(title)) FROM {accesslog} WHERE title <> ''"; $describe = t('Top titles in the past %interval'); $page_title = strtr($describe, array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))); $header = array( array('data' => t('title'), 'field' => 'title'), array('data' => t('last path'), 'field' => 'path'), array('data' => t('last view'), 'field' => 'last_view'), array('data' => t('hits'), 'field' => 'count', 'sort' => 'desc'), array('data' => t('operations')) ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50, 0, $sql_cnt); while ($title = db_fetch_object($result)) { $rows[] = array(l(_statistics_column_width($title->title, '_title', 56), urlencode($title->path)), _statistics_column_width($title->path, '_title', 56), format_date($title->last_view, 'small'), $title->count, ($title->title ? l(t('track title'), 'admin/logs/access/title/'. urlencode($title->title)) : '')); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } $output = theme('table', $header, $rows); print theme('page', $output, $page_title); } /** * Menu callback; presents the "Top users" page. */ function statistics_top_users() { $sql = "SELECT uid, hostname, MAX(timestamp) AS last_view, COUNT(uid) AS count FROM {accesslog} GROUP BY uid"; $sql_cnt = "SELECT COUNT(DISTINCT(uid)) FROM {accesslog}"; $describe = t('Top users in the past %interval'); $page_title = strtr($describe, array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))); $header = array( array('data' => t('user'), 'field' => 'user'), array('data' => t('last hostname'), 'field' => 'hostname'), array('data' => t('last view'), 'field' => 'last_view'), array('data' => t('hits'), 'field' => 'count', 'sort' => 'desc'), array('data' => t('operations'), 'colspan' => 2) ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50, 0, $sql_cnt); while ($u = db_fetch_object($result)) { $user = user_load(array('uid' => $u->uid)); $rows[] = array(format_name($user), $u->hostname, format_date($u->last_view, 'small'), $u->count, ($u->uid ? l(t('track user'), "admin/logs/access/user/$user->uid") : ''), ($u->hostname ? l(t('track host'), "admin/logs/access/host/$u->hostname") : '')); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } $output = theme('table', $header, $rows); print theme('page', $output, $page_title); } /** * Menu callback; presents the "Top hostnames" page. */ function statistics_top_hostnames() { $sql = "SELECT hostname, uid, MAX(timestamp) AS last_view, COUNT(hostname) AS count FROM {accesslog} GROUP BY hostname"; $sql_cnt = "SELECT COUNT(DISTINCT(hostname)) FROM {accesslog}"; $describe = t('Top hostnames in the past %interval'); $page_title = strtr($describe, array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))); $header = array( array('data' => t('hostname'), 'field' => 'hostname'), array('data' => t('last user'), 'field' => 'user'), array('data' => t('last view'), 'field' => 'last_view'), array('data' => t('hits'), 'field' => 'count', 'sort' => 'desc'), array('data' => t('operations'), 'colspan' => 2) ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50, 0, $sql_cnt); while ($hostname = db_fetch_object($result)) { $user = user_load(array('uid' => $hostname->uid)); $rows[] = array($hostname->hostname, format_name($user), format_date($hostname->last_view, 'small'), $hostname->count, ($hostname->hostname ? l(t('track host'), "admin/logs/access/host/$hostname->hostname") : ''), ($hostname->uid ? l(t('track user'), "admin/logs/access/$hostname->uid") :'')); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } $output = theme('table', $header, $rows); print theme('page', $output, $page_title); } /** * Menu callback; presents the "Top referrers" page. * * @param $view * - "internal": Only display internal links. * - "external": Only display links from off-site. * - "all": Display all referrers. */ function statistics_top_referrers($view = 'all') { if ($view == 'all') { $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url <> '' GROUP BY url"; $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> ''"; $describe = t('Top referrers in the past %interval'); } elseif ($view == 'internal') { $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' GROUP BY url"; $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'"; $describe = t('Top internal referrers in the past %interval'); } else { /* default to external */ $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' AND url <> '' GROUP BY url"; $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'"; $describe = t('Top external referrers in the past %interval'); } $title = strtr($describe, array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))); $header = array( array('data' => t('URL'), 'field' => 'url'), array('data' => t('last view'), 'field' => 'last_view'), array('data' => t('hits'), 'field' => 'count', 'sort' => 'desc') ); $query .= tablesort_sql($header); $result = pager_query($query, 50, 0, $query_cnt); while ($referrer = db_fetch_object($result)) { $rows[] = array(''. _statistics_column_width($referrer->url, '_refer', 75) .'', format_date($referrer->last_view, 'small'), $referrer->count); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => 3)); } $output .= theme('table', $header, $rows); print theme('page', $output, $title); } /** * Menu callback; presents the "Most Popular Content" page. */ function statistics_admin_topnodes() { print theme('page', statistics_admin_topnodes_table()); } /** * Implementation of hook_settings(). */ function statistics_settings() { // access log settings: $group = form_radios(t('Enable access log'), 'statistics_enable_access_log', variable_get('statistics_enable_access_log', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Log each page access. Required for referrer statistics.')); $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'); $group .= form_select(t('Discard access logs older than'), 'statistics_flush_accesslog_timer', variable_get('statistics_flush_accesslog_timer', 259200), $period, t('Older access log entries (including referrer statistics) will be automatically discarded. Requires crontab.')); $output = form_group(t('Access log settings'), $group); // count content views settings $group = form_radios(t('Count content views'), 'statistics_count_content_views', variable_get('statistics_count_content_views', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Increment a counter each time content is viewed.')); $group .= form_radios(t('Display counter values'), 'statistics_display_counter', variable_get('statistics_display_counter', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Display how many times given content has been viewed. User must have the "access statistics" permissions to be able to view these counts.')); $output .= form_group(t('Content viewing counter settings'), $group); // Popular content block settings $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40)); $group = form_select(t("Number of day's top views to display"), 'statistics_block_top_day_num', variable_get('statistics_block_top_day_num', 0), $numbers, t('How many content items to display in "day" list. Requires enabled content viewing counters.')); $group .= form_select(t('Number of all time views to display'), 'statistics_block_top_all_num', variable_get('statistics_block_top_all_num', 0), $numbers, t('How many content items to display in "all time" list. Requires enabled content viewing counters.')); $group .= form_select(t('Number of most recent views to display'), 'statistics_block_top_last_num', variable_get('statistics_block_top_last_num', 0), $numbers, t('How many content items to display in "recently viewed" list. Requires enabled content viewing counters.')); $output .= form_group(t('"Popular content" block settings'), $group); // Popular content page settings $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25)); $group = form_textfield(t('Name for link to user page'), 'statistics_userpage_link', variable_get('statistics_userpage_link', ''), 20, 40, t("This node generates a user page listing your site's most popular content. If you specify a name here, a link to the \"Popular content\" page will be added automatically.")); $group .= form_select(t("Number of day's top views to display"), 'statistics_userpage_day_cnt', variable_get('statistics_userpage_day_cnt', 0), $numbers, t('How many content items to display in the "day" list. Requires enabled content viewing counters.')); $group .= form_select(t('Number of all time top views to display'), 'statistics_userpage_all_cnt', variable_get('statistics_userpage_all_cnt', 0), $numbers, t('How many content items to display in the "all time" list. Requires enabled content viewing counters.')); $group .= form_select(t('Number of most recent views to display'), 'statistics_userpage_last_cnt', variable_get('statistics_userpage_last_cnt', 0), $numbers, t('How many posts to display in the "recently viewed" list. Requires enabled content viewing counters.')); $output .= form_group(t('"Popular content" page settings'), $group); return $output; } /** * Saves the values entered in the "config statistics" administration form. */ function statistics_save_statistics($edit) { variable_set('statistics_display_counter', $edit['statistics_display_counter']); } /** * Implementation of hook_cron(). */ function statistics_cron() { $statistics_timestamp = variable_get('statistics_day_timestamp', ''); if ((time() - $statistics_timestamp) >= 86400) { /* reset day counts */ db_query('UPDATE {node_counter} SET daycount = 0'); variable_set('statistics_day_timestamp', time()); } /* clean expired access logs */ db_query('DELETE FROM {accesslog} WHERE '. time() .' - timestamp > '. variable_get('statistics_flush_accesslog_timer', 259200)); } /** * Returns all time or today top or last viewed node(s). * * @param $dbfield * one of * - 'totalcount': top viewed content of all time. * - 'daycount': top viewed content for today. * - 'timestamp': last viewed node. * * @param $dbrows * number of rows to be returned. * * @return * A query result containing n.nid, n.title, u.uid, u.name of the selected node(s) * or FALSE if the query could not be executed correctly. */ function statistics_title_list($dbfield, $dbrows) { return db_query_range("SELECT s.nid, n.title, u.uid, u.name FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE %s <> '0' AND n.status = 1 ORDER BY %s DESC", 's.'. $dbfield, 's.'. $dbfield, 0, $dbrows); } /** * Retrieves a node's "view statistics". * * @param $nid * node ID * * @return * An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp * - totalcount: count of the total number of times that node has been viewed. * - daycount: count of the total number of times that node has been viewed "today". * For the daycount to be reset, cron must be enabled. * - timestamp: timestamp of when that node was last viewed. */ function statistics_get($nid) { if ($nid > 0) { /* retrieves an array with both totalcount and daycount */ $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid)); } return $statistics; } /** * Implementation of hook_block(). */ function statistics_block($op = 'list', $delta = 0) { if ($op == 'list') { if (variable_get('statistics_count_content_views', 0)) { $blocks[0]['info'] = t('Popular content'); } return $blocks; } else if (user_access('access content')) { switch ($delta) { case 0: $content = array(); $daytop = variable_get('statistics_block_top_day_num', 0); if ($daytop) { $content[] = node_title_list(statistics_title_list('daycount', $daytop), t("Today's:")); } $alltimetop = variable_get('statistics_block_top_all_num', 0); if ($alltimetop) { $content[] = node_title_list(statistics_title_list('totalcount', $alltimetop), t('All time:')); } $lasttop = variable_get('statistics_block_top_last_num', 0); if ($lasttop) { $content[] = node_title_list(statistics_title_list('timestamp', $lasttop), t('Last viewed:')); } $output = implode($content, '