Statistics: tracking referrers, page hits, etc.
Last modified: November 8, 2009 - 23:40
The statistics module keeps track of numerous statistics of site usage. It counts how many times, and from where each of your posts is viewed. The statistics module can be used to learn many useful things about how users are interacting with each other and with your site.
Statistics module features
- Logs show statistics for how many times the site and specific content on the site has been accessed.
- Referrers tells you from where visitors came from (referrer URL).
- Top pages shows you what's hot, what is the most popular content on your site.
- Top visitors shows you the most active visitors for your site and allows you to ban abusive visitors.
- Recent hits displays information about the latest activity on the site.
- Node count displays the number of times a node has been accessed in the node's link section next to # comments.
Configuring the statistics module at Administer >> Logs >> Access log settings
Note for Drupal 6: Enable access logs and count content views at:
Administer >> Reports >> Access log settings
- Enable access log allows you to turn the access log on and off. This log is used to store data about every page accessed, such as the remote host's IP address, where they came from (referrer), what node they've viewed, and their user name. Enabling the log adds one database call per page displayed by Drupal.
- Discard access logs older than allows you to configure how long an access log entry is saved, after which time it is deleted from the database table. To use this you need to run cron.php
- Enabling Count content views allows you to turn on and off the node-counting functionality of this module. If it is turned on, an extra database query is added for each node displayed, which increments a counter.
Permissions at Administer >> User management >> Access control
- access statistics allows users within a role to view the statistics logs.
- view post access counter allows users within a role to view the count results from the Count content views setting.
You can
- administer statistics administer >> settings >> statistics for Drupal 4.7 or Administer >> Logs >> Access log settings for Drupal 5.x.
- access statistics logs Administer >> Logs.
- view recent hits Administer >> Logs >> Recent hits.

referer vs. request uri
On line 64 of statics.module data is written to the accesslog. Why do we use referer_uri() for the URL instead of request_uri?
I wonder the same.
I just struggled a whole day building a simple module that lists last content viewed by a user (it's a sin there is no id-based relation between access logs and real resource visited) and I always get wrong results. I finally figured out 'url' is there to store the referer to the last page visited, but if so, don't call it 'url', just 'referer'.
If I get it right, url should contain referer_uri(), while path should contain current_uri(): this makes statistics Pathauto-aware; otherwise the actual path visited by user is lost.
Bye
-=[ Hiade Airik ]=-
The Man (beta version)
http://www.heterogenius.it
referrer tracking
Hi Hiade,
One tip from me about your referrer traffic: try smarter software like Logaholic Web Analytics. It supports both log files and java script and it displays the referrers URLs as referrers, an not under URLs. Besides basic reporting you can also follow trends, make sales funnels and do A/B split testing with Logaholic. You can get a free trial from: http://www.logaholic.com/wp/trial-download/. Good luck! :-)
Impact of aggressive caching on stats?
I've got a block or two that sort node lists by total views. If aggressive caching + stats results in view counts that aren't perfectly accurate but are relatively close to the proportional traffic each page receives, I can live with that. If they have no relationship to reality at all, I'll be sad forever.
Here are some examples that illustrate what I've got in mind:
Fictional but accurate traffic using the stats module + normal caching:
The data in Drupal which I could live with because it will still sort accurately using the stats module + aggressive caching, based on the fictional but presumptively accurate traffic numbers above:
What I'm afraid stats would look like to Drupal were I using the stats module + aggressive caching:
I suspect that it's more like the last example than the second one, but if anyone knows otherwise definitively, I'd be very happy to hear more.
Patch for statistics.admin.inc
statistics.admin.inc throws a database error when the DB backend is PostgreSQL.
At line 340 in modules/system/system.install the following function is defined if the database sis PostgreSQL:
if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'concat'"))) {db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
\'SELECT $1 || $2;\'
LANGUAGE \'sql\''
);
}
PostgreSQL versions >= than 8.3 no longer allow implicit casts.
On line 87 of /modules/statistics/statistics.admin.inc the CONCAT function is called.
$sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";In this line CONCAT is called with as CONCAT (unsigned_int, text) and that is an implicit cast.
Here is a patch that uses the portable SQL cast (expr as type) to fix this implicit cast.
This works for Postgresql (any version) and verified against MySQL documentation. I do not run MySQL so I can't actually test it.
--- statistics.admin.inc.old Sun Jan 31 09:54:55 2010
+++ statistics.admin.inc Sun Jan 31 09:55:35 2010
@@ -83,7 +83,7 @@
);
$sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid". tablesort_sql($header);
- $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";
+ $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(cast(uid as text), hostname))) FROM {accesslog}";
$result = pager_query($sql, 30, 0, $sql_cnt);
$rows = array();
In case the above diff is mangled, I've also put it up on my web site at http://www.openvistas.net/statistics.admin.inc.patch