[Zope] Counting hits

Cliff Ford Cliff.Ford at ed.ac.uk
Wed Sep 29 14:21:50 EDT 2004


Assuming you want to display a number on a page, which is typically what 
underlings ask for, here is one way to do it. This is for a site that 
has a single master page template containing macros called by a simple 
template for each page. Put this in the master template:

<div tal:condition="template/PageCount | nothing" tal:omit-tag="">
<p align="center" tal:content="structure here/page_count_tool">Page 
Counter</p>
</div>

In the slave template create a PageCount boolean property, which is true 
(1) to count or false (0) to not count. Our users can click a check box 
to switch counting on or off as required.

This is the page_count_tool dtml method:

<dtml-call "REQUEST.set('url', URL0)">

<dtml-try>
<dtml-call page_count_initialise>
<dtml-except>
</dtml-try>

<dtml-try>
<dtml-call page_count_update>
<dtml-in page_count_list>
<span class=whatever><dtml-var COUNT></span>
</dtml-in>
<dtml-except>
</dtml-try>

This may not be the most efficient way to code - it was done when I was 
learning Zope - and I did not really pluck up the courage to be a helper 
  untill following the recent "disgraceful" thread. Still not sure if 
the blind should be leading the blind. Nevertheless, to continue..

The page count is kept in a database. So you need a PAGECOUNT table:

URL varchar(255) PRIMARY KEY
COUNT int(11)

and separate insert and update ZSQL Methods:

INSERT INTO PAGECOUNT (COUNT, URL) VALUES (0, <dtml-sqlvar url type=nb>)

UPDATE PAGECOUNT SET COUNT = COUNT + 1 WHERE URL = <dtml-sqlvar url type=nb>

I don't use page counts much myself, but occasionally it is useful to 
know if a page is worth maintaining at all!

Regards

Cliff Ford


John Poltorak wrote:
> How do people count the number of hits they have on a Zope website?
> 
> 


More information about the Zope mailing list