uid, time()); } else { db_query("INSERT INTO {accesslog} (url, hostname, uid, timestamp) values('%s', '%s', %d, %d)", $referrer, $hostname, $user->uid, time()); } } } /* Permissions hook, defines module's permissions */ function statistics_perm() { /* ** statistics module defines the following permissions: ** administer statistics module - full administrative control of module ** administer statistics - view statistics / referrer log ** access statistics - see how many times individual content has ** been viewed (if enabled) */ return array("administer statistics module", "administer statistics", "access statistics"); } /* Link hook, defines module's links */ function statistics_link($type, $node = 0, $main = 0) { global $id; $links = array(); if ($type == "node" && 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/statistics/log/node/$node->nid"); } 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."))); } } if ($type == "system") { if ((user_access("administer statistics module") || (user_access("administer statistics")))) { menu("admin/statistics", t("statistics"), "statistics_admin", 6); menu("admin/statistics/referrers", t("referrer log"), "statistics_admin"); menu("admin/statistics/referrers/internal", t("internal referrers only"), "statistics_admin"); menu("admin/statistics/referrers/external", t("external referrers only"), "statistics_admin"); menu("admin/statistics/log", t("access log"), "statistics_admin"); menu("admin/statistics/log/node", t("track node"), "statistics_admin", 0, MENU_HIDE); menu("admin/statistics/log/user", t("track user"), "statistics_admin", 0, MENU_HIDE); menu("admin/statistics/log/host", t("track host"), "statistics_admin", 0, MENU_HIDE); menu("admin/statistics/help", t("help"), "statistics_help_page", 9); } if (user_access("access content")) { menu("statistics", t("view most popular content"), "statistics_page", 0, MENU_HIDE); } } return $links; } function statistics_help($section = "admin/help#statistics") { $output = ""; switch ($section) { case 'admin/help#statistics': $output .= t("

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.

referrers log

This admin page shows you site-wide referrer statistics. You can see 'all' statistics, 'external' statistics or 'internal' statistics. Default is 'all'.

access log

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.

Configuring the statistics module

There are some configuration options added to the main administer » configuration section:

Popular content block

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.

Popular content page

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.

Permissions

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).

Statistics module (for developers)

Accessing statistics

To get a node's \"view statistics\" make a call to the function statistics_get(\$nid). When you pass in a Node ID (\$nid), the function returns an array with three entires: [0]=totalcount, [1]=daycount, [2]=timestamp. For example, you could use this function call to add node view counts to your theme.

The module automatically adds '# reads' to each node's link section (if enabled).

Most popular content

The statistics module provides a function 'statistics_title_list(\$dbfield, \$dbrows)' to return an array of links to any of the following: the top viewed content of all time, the top viewed content of today, and the last viewed content. You can pass in:

\$dbrows is the number or rows you want returned in your array.

", array("%modules" => url("admin/system/modules"), "%permissions" => url("admin/user/permission"), "%referers" => url("admin/statistics/referrers"), "%access" => url("admin/statistics/log"), "%configuration" => url("admin/system/modules/statistics"), "%block" => url("admin/system/block"))); break; case 'admin/system/modules#description': $output = t("Logs access statistics for your site."); break; case 'admin/system/modules/statistics': $output = t("Settings for the statistical information that Drupal will keep about the site. See site statistics for the actual information.", array("%statistics" => url("admin/statistics"))); break; case 'admin/statistics': $output = t("This page gives you an at-a-glance look at your most popular content."); break; case 'admin/statistics/referrers': $output = t("This page shows you site-wide referrer statistics. You can see 'all referrers', 'external referrers' or 'internal referrers'. Referrers are web sites, both your site, and other peoples, that point to your web site."); break; case 'admin/statistics/referrers/internal': $output = t("This page shows you only 'internal referrers'. Links pointing to your web site, from within your web site."); break; case 'admin/statistics/referrers/external': $output = t("This page shows you only 'external referrers'. Links pointing to your web site from outside your web site."); break; case 'admin/statistics/log': case 'admin/statistics/log/node': case 'admin/statistics/log/user': case 'admin/statistics/log/host': $output = t("This pages shows you who is accessing your web site. You can see the hostnames, referrers. In particular, it is easy to inspect a user's navigation history/trail by clicking on track user."); break; } return $output; } function statistics_help_page() { print theme("page", statistics_help()); } /* Administration hook, defines module's administrative page */ function statistics_admin() { $op = $_POST["op"]; $edit = $_POST["edit"]; if (empty($op)) { $op = arg(2); } /* non-configuration admin pages */ switch ($op) { case "referrers": $output = statistics_top_refer(); break; case "log": $output = statistics_admin_displaylog(); break; default: $output = statistics_admin_topnodes(); } print theme("page", $output); } 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/view/". $nid["nid"], array("title" => t("View this posting."))), $nid["daycount"], $nid["totalcount"], format_date($nid["timestamp"], "small"), l("track node", "admin/statistics/log/node/$nid[nid]")); } if ($pager = theme("pager", NULL, 20, 0, tablesort_pager())) { $rows[] = array(array("data" => $pager, "colspan" => 5)); } return theme("table", $header, $rows); } function statistics_admin_accesslog_table($type, $id) { if ($type == 1) { /* retrieve user access logs */ if ($id) { /* retrieve recent access logs for user $id */ $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid = ". check_query($id); } else { /* retrieve recent access logs for all users */ $sql = "SELECT a.nid, a.url, a.hostname, a.uid, MAX(a.timestamp) AS timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid <> '0' GROUP BY a.uid, a.nid, a.url, a.hostname"; } } else if ($type == 2) { /* retrieve recent access logs for node $id */ $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a INNER JOIN {node} n ON a.nid = n.nid WHERE a.nid = ". check_query($id); } else if ($type == 3) { /* retrieve recent access logs for hostname $id */ $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.hostname = '". check_query($id) ."'"; } else { /* retrieve all recent access logs */ $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid"; } $header = array( array("data" => t("timestamp"), "field" => "timestamp", "sort" => "desc"), array("data" => t("post"), "field" => "nid"), 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)) { $user = user_load(array("uid" => $log->uid)); if ($log->url) { $url = "url\" title=\"$log->url\">". (strlen($log->url) > 28 ? truncate_utf8($log->url, 28) . '...' : $log->url) .""; } else { $url = message_na(); } $rows[] = array(array("data" => format_date($log->timestamp, "small"), "nowrap" => "nowrap"), ($log->nid ? l($log->title, "node/view/$log->nid") : message_na()), format_name($user), $log->hostname ? $log->hostname : message_na(), $url, ($log->nid ? l(t("track node"), "admin/statistics/log/node/$log->nid") : ""), ($user->uid ? l(t("track user"), "admin/statistics/log/user/$user->uid") : ""), ($log->hostname ? l(t("track host"), "admin/statistics/log/host/$log->hostname") : "")); } if ($pager = theme("pager", NULL, 50, 0, tablesort_pager())) { $rows[] = array(array("data" => $pager, "colspan" => 8)); } return theme("table", $header, $rows); } function statistics_top_refer() { $view = arg(3) ? arg(3) : "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 <> ''"; } 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 of 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 of the past %interval"); } $output = "

". 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("count"), "field" => "count", "sort" => "desc") ); $query .= tablesort_sql($header); $result = pager_query($query, 50, 0, $query_cnt); while ($referrer = db_fetch_array($result)) { $rows[] = array("". truncate_utf8($referrer["url"], 100) ."", 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); return $output; } function statistics_admin_topnodes() { $output = "

". t("Popular content") ."

\n"; $output .= statistics_admin_topnodes_table(); return $output; } function statistics_admin_displaylog() { $type = arg(3); $value = arg(4); switch ($type) { case "user": $user = user_load(array("uid" => $value)); $output = "

". t("Recent access logs for '%name'", array("%name" => $user->name)) ."

\n"; $output .= statistics_admin_accesslog_table(1, $user->uid); break; case "node": $node = node_load(array("nid" => $value)); $output = "

". t("Recent access logs for '%title'", array("%title" => $node->title)) ."

\n"; $output .= statistics_admin_accesslog_table(2, $value); break; case "host": $output = "

". t("Recent access logs for '%hostname'", array("%hostname" => $value)) ."

\n"; $output .= statistics_admin_accesslog_table(3, $value); break; default: $output = "

". t("Recent access logs") ."

\n"; $output .= statistics_admin_accesslog_table(0, 0); } return $output; } // settings for the statistics module 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"]); } /* cron hook, performs automatic functions */ 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)); } /* Display linked title based on field name */ function statistics_title_list($dbfield, $dbrows) { /* valid dbfields: totalcount, daycount, timestamp */ 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); } /* Function to retreive current page counter value. */ 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; } /* Block hook */ 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, "
"); $block["subject"] = variable_get("statistics_block_top_title", t("Popular content")); $block["content"] = $output; break; } return $block; } } function statistics_page() { $output = ""; // build day's most popular content list if enabled if ($displaycount = variable_get("statistics_userpage_day_cnt", 0)) { $table = ""; $table .= statistics_summary("daycount", $displaycount); $table .= "
"; $output .= theme("box", t("Day's most popular content:"), $table, "main"); } // build all time most popular content list if enabled if ($displaycount = variable_get("statistics_userpage_all_cnt", 0)) { $table = ""; $table .= statistics_summary("totalcount", $displaycount); $table .= "
"; $output .= theme("box", t("All time most popular content:"), $table, "main"); } // build last viewed content list if enabled if ($displaycount = variable_get("statistics_userpage_last_cnt", 0)) { $table = ""; $table .= statistics_summary("timestamp", $displaycount); $table .= "
"; $output .= theme("box", t("Last viewed content:"), $table, "main"); } print theme("page", $output); } function statistics_summary($dbfield, $dbrows) { /* valid dbfields: totalcount, daycount, timestamp */ $output = ""; $result = db_query_range("SELECT n.nid, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid ORDER BY %s DESC", $dbfield, 0, $dbrows); while ($nid = db_fetch_array($result)) { $content = node_load(array("nid" => $nid["nid"])); $links = link_node($content, 1); $output .= "". l($nid["title"], "node/view/". $nid["nid"], array("title" => t("View this posting."))) ."". t("Submitted by %a on %b", array("%a" => format_name($content), "%b" => format_date($content->created, "large"))) .""; $output .= "
". check_output($content->teaser) ."
"; $output .= "[ ". theme("links", $links) ." ]

"; } return $output; } function statistics_nodeapi(&$node, $op, $arg = 0) { switch ($op) { case "delete": // clean up statistics table when node is deleted db_query("DELETE FROM {node_counter} WHERE nid = %d", $node->nid); } } ?>