Skip to content. | Skip to navigation

Personal tools


You are here: Home / Tips / Page Hit or View Counting

Page Hit or View Counting

Incremental Count for Pages

Counters based on the BTree.Length implementation are used in various places of Zope since a decade.

>>> def nextval(sequence, value=None, start=1): 
...     try: 
...         current = sequence.maxKey() 
...     except ValueError: 
...         sequence[start] = value # sequence BTree is empty 
...         return start 
...     else: 
...         next = current + 1 
...         sequence[next] = value 
...         return next 
>>> from BTrees.IOBTree import IOBTree 
>>> sequence = IOBTree() 
>>> nextval(sequence) 
>>> nextval(sequence) 

Portal Properties

For the purposes of this blog entry, let's assume that we want to track "hit counts" in our Plone site. Let's forget for the moment that Google Analytics is a far better solution.

In a typical web application, we might turn to the relational database to store application data like this, and increment it with an UPDATE query with each page it.

In Plone, we could do the same, but a much simpler option, which actually leverages the ZODB, is to use portal properites.

Visit portal_properties -> site_properties from your ZMI root and add a property called "hit_count" (integer).

If you create the following script, and call it with each page hit, your hit counter would be incremented by 1 each time:

hit_count = context.portal_properties.site_properties.hit_count
context.portal_properties.site_properties.manage_changeProperties(hit_count = hit_count + 1)

So, you try this out and realize that you get an authorization error. Make sure that the python script which executes this code has been proxied as a manager.

There are two ways to do this. If you have created the script via the TTW interface (ZMI), you can simple visit the "Proxy" tab and assign it a value of "Manager".

If you have created this script on the file system, we need to create a metadata file which contains the following:


Views Hits Counter for Videos

collective.flowplayer collective.flowplayerclipviews