Forums / Developer / count accesses to a page ezpublish 4.5

count accesses to a page ezpublish 4.5

Author Message

alessio asistar

Tuesday 24 May 2011 8:00:03 am

I need to count the accesses to a page.
When a user (also anonymous) displays a page I have to increase a value.
Can anyone help me?

Alessio

Igor Vrdoljak

Tuesday 24 May 2011 3:31:47 pm

If you just need to get most viewed nodes you can try using view_top_list fetch function.

Keep in mind that it relies on a cronjob script (updateviewcount.php) that parses Apache log files and updates ezview_counter table (which is than used to determine the most accessed nodes).

Cheers

http://www.netgen.hr/eng
http://twitter.com/ivrdoljak

Sebastiaan van der Vliet

Wednesday 25 May 2011 12:05:35 am

If you do not want to parse files, you could do something like this. Create a template operator called "viewcounter" and add the following code to your pagelayout

{cache-block expiry=1}{if and($current_node_id,gt($current_node_id,2))}{""|viewcounter($current_node_id)}{/if}{/cache-block}

The template operator modify function should look something like this:

function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, $operatorValue, $namedParameters )
{
$nodeID = $namedParameters['node_id'];

switch ( $operatorName )
{
case 'viewcounter':
{
if ( is_numeric( $nodeID ) AND $nodeID > 0 )
$nodeObject = eZContentObjectTreeNode::fetch( $nodeID );
if ( $nodeObject != null )
{
$counter = eZViewCounter::fetch( $nodeID );
if ( $counter == null )
$counter = eZViewCounter::create( $nodeID );
$counter->increase();
}
}
} break;
}
}
}

The code is for older versions of eZ, so it probably has to be updated.

Certified eZ publish developer with over 9 years of eZ publish experience. Available for challenging eZ publish projects as a technical consultant, project manager, trouble shooter or strategic advisor.

Igor Vrdoljak

Wednesday 25 May 2011 2:02:44 am

Also, you should take into consideration the effects of different caching approaches.

For example, if you make use of static cache, it might happen that your template code does not get executed.

Also, if you use some kind of reverse proxy / HTTP accelerator like Varnish, it could happen that visit does not get into Apache log either.

You can try some kind of client side/Java Script approach (for example by utilising Google Analytics custom variables). By this you can also filter out visits made by web bots/crawlers.

http://www.netgen.hr/eng
http://twitter.com/ivrdoljak

Marko Žmak

Wednesday 25 May 2011 4:39:47 am

Hi Alessio.

I have made an extension that counts visits using a template operator and gives live stats in realtime. In practice it counts only the content that is not in static cache, so it's usually used to get realtime visits for content like articles and news. A very usefull tools for news portals.

Since this counter is a part of broader extension, it's not yet available to public but if you want you can contact me personally and I can help you setting it up.

--
Nothing is impossible. Not if you can imagine it!

Hubert Farnsworth