I have created a page in Drupal using views. Is there any option to get the count of visitors that visit the page?


Enable the statistics module that comes with core in admin/module. You can start tracking pages once you enable the log settings in admin/config/system/statistics

Once you enable the Setting Enable access log drupal will start tracking all pages. Now follow below process to create a view.

  • Create a view with base type as access log
  • Enable aggregation in the other settings
  • Add Access Log path field and set "Group results together" in aggregation settings next to field settings.
  • Add one more field access log path and select Aggregation type as count.
  • Now you can see all paths and number of visits associated with them.
  • Add filter access log path and provide the path for which you would like to see number of visits.

Find the exported view below.

$view = new view();
$view->name = 'statistics';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'accesslog';
$view->human_name = 'Statistics';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Statistics Custom';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Access log: Path */
$handler->display->display_options['fields']['path']['id'] = 'path';
$handler->display->display_options['fields']['path']['table'] = 'accesslog';
$handler->display->display_options['fields']['path']['field'] = 'path';
/* Field: COUNT(Access log: Path) */
$handler->display->display_options['fields']['path_1']['id'] = 'path_1';
$handler->display->display_options['fields']['path_1']['table'] = 'accesslog';
$handler->display->display_options['fields']['path_1']['field'] = 'path';
$handler->display->display_options['fields']['path_1']['group_type'] = 'count';
$handler->display->display_options['fields']['path_1']['label'] = 'Count';
/* Filter criterion: Access log: Path */
$handler->display->display_options['filters']['path']['id'] = 'path';
$handler->display->display_options['filters']['path']['table'] = 'accesslog';
$handler->display->display_options['filters']['path']['field'] = 'path';
$handler->display->display_options['filters']['path']['value'] = 'admin/config/system/statistics';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['defaults']['hide_admin_links'] = FALSE;
$handler->display->display_options['path'] = 'statistics-custom';

That's it :-)

If you would like to display page counter contextually then below code in a custom block will help.

$path = $_GET['q'];
$visits = db_query("SELECT count(*) FROM {accesslog} where path = :path", array(':path' => $path))->fetchField();
echo "Views: " . $visits;
| improve this answer | |
  • Hi Anil, Thank you very much for your quick and clear reply for getting visitors counts of a page in views.But my requirement is that I have a page created using views.When any user visits that page the corresponding page count should increased and store in db.And the view url will change according to the contextual filter value.Is there any option to get the list of that views page count? – Manu Benjamin Oct 19 '12 at 12:22
  • Unfortunately, You cannot do it using views out of the box.. but you can do it with a small custom block... – Anil Sagar Oct 19 '12 at 12:25
  • I updated the answer with 4 lines of php code.. By putting the code in custom block you can see the page visits :-) – Anil Sagar Oct 19 '12 at 12:44
  • Thank you Anil, your code was really helpful and it works :) – Manu Benjamin Oct 22 '12 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.