Last updated August 15, 2013. Created by ghankstef on October 13, 2001.
Edited by Ruben@TTY.nl, rjacobs, Garrett Albright, batigolix. Log in to edit this page.

The Statistics module shows you how often a given page is viewed, who viewed it, the previous page the user visited (referrer URL), and when it was viewed. These statistics are useful in determining how users are visiting and navigating your site.

Use

Configuring statistics

In Drupal 7 configure statistics via configuration > system > statistics.
In Drupal 6 configure statistics via administer > reports > access log settings.
In Drupal 5 configure statistics via administer > logs > access log settings.

To enable collection of statistics, the Enable access log checkbox on the Statistics settings page must be checked. This access 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.

The Discard access logs older than setting on the settings page specifies the length of time entries are kept in the log before they are deleted. This setting requires a correctly configured cron maintenance task to run.

Enable Count content views 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.

Viewing site usage

The Statistics module can help you break down details about your users and how they are using the site. The module offers four reports:

  • Recent hits displays information about the latest activity on your site, including the URL and title of the page that was accessed, the user name (if available) and the IP address of the viewer.
  • Top referrers displays where visitors came from (referrer URL).
  • Top pages displays a list of pages ordered by how often they were viewed.
  • Top visitors shows you the most active visitors for your site and allows you to ban abusive visitors.

Displaying popular content

The module includes a Popular content block that displays the most viewed pages today and for all time, and the last content viewed. To use the block, enable Count content views on the statistics settings page, and then you can enable and configure the block on the blocks administration page.

Page view counter

The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, enable Count content views on the statistics settings page, and set the necessary permissions (View content hits) so that the counter is visible to the users.
Statistics administration pages

Permissions

To access statistics one needs the proper permissions.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

The function that produces the Top Visitors report (modules/statistics/statistics_admin.inc -> statistics_top_visitors()) groups visitor stats first by hostname then by uid effectively producing hostname stats rather than visitor stats.

If a user logs in from a number of different hostnames/IP addresses (or if the visitor's ISP routinely assigns the user different IP addresses), the user's stats are broken out separately for each hostname/IP address the user logged in from. The result is that the report then gives you the highest number of visits from a given hostname rather than from a given visitor. This is misleading. In order to get top visitors, you'd have to total the separate stats for each user manually.

The query:

<?php
$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);
?>

should be changed to:
<?php
$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.uid, u.name, ac.aid". tablesort_sql($header);
?>

to provide actual visitor stats.

James, please open a Drupal 8 issue for this concern -> http://drupal.org/project/issues/drupal

That is where something like this would be adressed and not in the docs here.

Hi
Now, we can see every flag statistics by adding the below code in the corresponding files in flag module.
Statistics of Flags.
1. Total no of Flags used
2. Flags & Nodes
3. Flags & Content Types

There 3 steps for getting Flag statistics page in flag module.
STEP 1:
In the file “flag\includes\flag.export.inc”, At line no.251, please append the below code:

/**
* Statistics of Flags.
* 1. Total no of Flags used
* 2. Flags & Nodes
* 3. Flags & Content Types
**/
function flag_stat_form($form, &$form_state) {

$form['flag_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Flag Statistics'),
);
$form['flag_settings']['container1'] = array(
'#type' => 'container',
'#weight' => -10,
);

$form['flag_settings']['container1']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('1. Flag Names and their count'),
'#weight' => -10,
);
$form['flag_settings']['container2']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('2.Nodes & Flags Statistics'),
'#weight' => -10,
);
$form['flag_settings']['container3']['flag_stats'] = array(
'#type' => 'fieldset',
'#title' => t('3. Content types & Flags Statistics'),
'#weight' => -10,
);

// Retrieve a list of all flags, and their count for all users
$result1 = db_query("SELECT flags.name,sum(flag_counts.count) as Count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid GROUP BY flags.name ORDER BY flags.fid ASC")->fetchAll();

// Retrieve a list of all flags, nodes list.
$result2 = db_query("SELECT node.title,flags.fid,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id UNION SELECT node.title, flags.fid,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id")->fetchAll();

// Retrieve a list of all flags, content types list.
$result3 = db_query("SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id LEFT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id LEFT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid LEFT JOIN node ON node.nid=flag_counts.content_id RIGHT JOIN node_type ON node.type=node_type.type UNION SELECT node.title,node_type.type,flags.name,flag_counts.count FROM flags LEFT JOIN flag_counts ON flags.fid=flag_counts.fid RIGHT JOIN node ON node.nid=flag_counts.content_id RIGHT JOIN node_type ON node.type=node_type.type")->fetchAll();

// Implementing array of flags, their counts
$flag_count = array();
$flag_list = array();
foreach ($result1 as $row) {
if ($row->Count != '') {
$temp = array('0' => $row->name, '1' => $row->Count);
}
else {
$temp = array('0' => $row->name, '1' => 0);
}
array_push($flag_count,$temp);
array_push($flag_list, $row->name);
}

// Retriving the products list.
$products_list = array();
foreach ($result2 as $row) {
if($row->title != "") {
array_push($products_list,$row->title);
}
}

$products_list = array_values(array_unique($products_list));

// forming a dummy array with zeros.
$final_prod_flags = array_fill(0, count($products_list), array_fill(0, count($flag_list)+1, 0));

// Implementation of second statistics. i.e, Products and Flags relationship.
for($j=0;$jtitle == $products_list[$j]){
if($row->name == $flag_list[$i]){
$final_prod_flags[$j][$i+1] = $row->count;
}// flag if
}//prod if
}// row loop
}//flag for loop
}//prod for loop

// Retriving the content types list.
$content_type_list = array();
foreach ($result3 as $row) {
if($row->type != "") {
array_push($content_type_list,$row->type);
}
}

$content_type_list = array_values(array_unique($content_type_list));

// forming a dummy array with zeros for content type & flag statitics
$final_content_types_flags = array_fill(0, count($content_type_list), array_fill(0, count($flag_list)+1, 0));

// Implementation of third statistics. i.e, content type and Flags relationship.
for($j=0;$jtype == $content_type_list[$j]){
if($row->name == $flag_list[$i]) {
$count = $count + ($row->count);
$final_content_types_flags[$j][$i+1] = $count;
}// flag if
}//prod if
}// row loop
}//flag for loop
}//prod for loop

// Flag-Count Statistics table
$header1 = array(t('Flag name'));
$header1 = array_merge($header1, array(t('Flag count')));

// Node-Flags Statistics table
$header2 = array(t('Product name'));
$header2 = array_merge($header2, $flag_list);

// Content types-Flags Statistics table
$header3 = array(t('Content type name'));
$header3 = array_merge($header3, $flag_list);

$form['flag_settings']['container1']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header1,
'#rows' => $flag_count,
'#empty' => t('No content available.'),
);

$form['flag_settings']['container2']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header2,
'#rows' => $final_prod_flags,
'#empty' => t('No content available.'),
);

$form['flag_settings']['container3']['flag_stats']['flags'] = array(
'#theme' => 'table',
'#header' => $header3,
'#rows' => $final_content_types_flags,
'#empty' => t('No content available.'),
);
return $form;
}

STEP 2:
In the file, “flag\flag.module”, at line no.69, please add below code:

$items[FLAG_ADMIN_PATH . '/stats'] = array(
'title' => 'Stats',
'page callback' => 'drupal_get_form',
'page arguments' => array('flag_stat_form'),
'access arguments' => array('administer flags'),
'file' => 'includes/flag.export.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 4,
);

STEP 3:
Now just clear the cache,

That’s it.
Now Modules > Flags > configure
You can see another tab ”STATS” at the righthand side parallel to “LIST”,“ACTIONS”,“IMPORT”,“EXPORT”.
If you click on it, we can see three flag statistics tables.

First table says, how many flags being used for how many times.
Second table says, how many nodes are flagged for how many time for each flag.
Third table says, how content types with nodes being flagged with each flag.

ananth kiran m

Parse error: syntax error, unexpected ')', expecting ';' in /srv/bindings/1eb349c807a249dcaf8c532aa0ecc7b6/code/sites/all/modules/flag/includes/flag.export.inc on line 322

offending line of code:
for($j=0;$jtitle == $products_list[$j]){

Hello,
I have enabled statistics counter on my website + google analytics reports. But there's something weird: the number of visit of the statistics counter doesn't match with the number of view of ga.
For example on a page I have this results:
statistics counter: number of reads 162
google reports: 37 views

I think it's a big difference...

What could be the problem?

Thank you in advance..
F.

Mescalina Design

An alternative to this is the GAR module — it fetches referrer information from Google Analytics.

------
Tomáš J. Fülöpp @vacilandois