The documentation has very little information about the "view type" property defined when I'm beginning to create a view. I'm referring to the "view type" on admin/build/views/add ("Node", "User", etc), not the other "view type". The help should include more information about these options, possibly including a list of fields available for each.

I'm trying to create a view of my nodes with their pageviews, which I think would imply either a "Node" view type or an "Access Log" view type, but under the "fields" I can't get any kind of counter of the node's views. Under "Access Log", I can get a list of the usernames and timestamps of everyone who visited the node, but not a simple count of them. Ultimately, I'd like two columns, one of total views and one of unique views, but I don't see any way how to do this.

Comments

dawehner’s picture

If you have enable the statistics module you can have the following view:

$view = new view;
$view->name = 'stat';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'label' => 'Title',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 0,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
  'totalcount' => array(
    'label' => 'Total views',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'set_precision' => FALSE,
    'precision' => 0,
    'decimal' => '.',
    'separator' => ',',
    'prefix' => '',
    'suffix' => '',
    'exclude' => 0,
    'id' => 'totalcount',
    'table' => 'node_counter',
    'field' => 'totalcount',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('header', array(
  'type' => 'none',
));
$handler->override_option('footer', array(
  'type' => 'none',
));
$handler->override_option('empty', array(
  'type' => 'none',
));
a.bond’s picture

Thanks for that! I didn't see the "Node statistics: Total Views" field before. Is that each hit, or only unique hits? Also, is there a way to restrict the view count to only views in a specified date range?

dawehner’s picture

I don't know whether its each hit or unique, i guess first. But this is drupal core, so views cannot do anything.

There are also a view todays visits field.

If you need custom range, you could write your own field handler.

a.bond’s picture

How does one write their own field handler? Is there any documentation on this?

dawehner’s picture

Yes there is documentation in the advanced help of views or the same page on http://views-help.doc.logrus.com/help/views/api

If you want to jump in deeper into views read http://views.doc.logrus.com/ :)

a.bond’s picture

The advanced help and the api are useful, but very hard to read. It looks like I'd need to create a new module that's dependent on Views, and my new handler should be in a views_handler_field_handlername.inc ? Or since it's a new module, should it be modulename_handler_etc? Is there something that I need to put in the .module file to call the .inc file? If I want to make a handler that derives a node's view count from the accesslog in a way that it can be filtered by date or date range, is there an accesslog handler it can extend, or do I have to build it entirely as an extension of views_handler_field ?

Sorry for all the questions, but it's hard for me to find my way through all this.

dawehner’s picture

Yes you need to create a module exact.

There is something like a naming convention modulename_handler_type_handlername, where type is filter/field/sort.
To be able to use it you need hook_views_api and hook_views_handler and hook_views_data_alter

There is currently afaik no data for accesslogs

Anyway read http://views-help.doc.logrus.com/help/views/api-tables and http://views-help.doc.logrus.com/help/views/api-handlers
There is quite a lot documented.

mactoph’s picture

Status: Active » Closed (fixed)

Issue appears to be solved.