mxmCounter - A Simple Zope Hit Counter
Current Version
"1.1.0"
Installation
You should install this product as usual. Unpack the archive with
all contained directory information into your Zope products
directory
Create an instance in the root of your zope server through the zmi. It will automatically get the id "mxm_counter".
Usage
On any page where you want a hit counter you should insert a few lines of code.
The most simple ZPT version is:
<span tal:replace="python:here.mxm_counter.count(here)"/>
A more involved, but more graphically pleasing version could be:
Hits:
<span style="font-family: courier;
border: 1px solid black;
background-color: lightgray;
padding: 0px 4px;"
tal:define="count python:here.mxm_counter.count(here)"
tal:content="python:'%06i' % count">
0000001
</span>
In dtml it can look like:
<dtml-var "mxm_counter.count(this())">
In rare situations you might want to show the count while not increasing it. Then you can write:
<span tal:replace="python:here.mxm_counter.get_count(here)"/>
You should notice that you can insert the counter code in something like a footer script, and the counter will work automatically on all files that uses the footer.
Properties
title
Well, what can I say ....
ips_to_ignore
List of ip adresses to ignore. Meaning that they don't increase the counter when they visit a page.
Examples of valid patterns:
127.0.0.1
192.168.0.42
192.168.0
192
count_once
When this is checked it only counts a browser once for each browser session. It stores a cookie in the browser that expires when the browser is restarted. There is not really any right way to do this. This was just the way I choosed to do it.
save_interval
The counter saves the hits in memory when the number of uncomitted hits reaches the save_interval. Any hits that are not saved when zope is closed down, are lost. With the default setting of 100, it means that it saves the hits to disk once every 100 hits.
It also means that you can loose up to 100 registered hits when zope is shut down.
On the positive side it is a fast approach that doesn't grow the zodb.
The counters are stored in the var directory in a file called:
counters.dict
If not loosing hits is very important to you, you can set the
value to 1. But then you will write the counter.dict for
every page count.
This should be fine for small sites with few hits.
The interval is overall for the site. Not pr. page. You don't need 100 hits on one page before it saves the counters, but 100 hits on any of the counted pages combined.
counter management/overview page
The view tab in the zmi will show a very simple page, with paths and counts. Sorted by number of hits in reverse order. You can use this if you don't want visible counters on the pages.
You can avoid showing the count on a page by using this code:
<span tal:define="count python:here.mxm_counter.count(here)"/>
It will still increase the page count, but it will not be visible.
You can then see it in the view tab of mxm_counter.
You can also set the count of a page to any value. If you set the count to 0, the page is deleted from the counter, until someone visits it again.
Tips: When you press "Set" the counts are saved to disk. So if you want to shut down your site, and don't want to risk loosing any counts, you can select a page and give it that same count as it allready has.
Methods
You can call the following methods on the tool:
def count(self, obj, REQUEST=None):
"""
Increases The count For The Object. It will loose count
if object is moved.
Returns the new count for that page.
"""
def get_count(self, obj, REQUEST=None):
"returns the count without incrementing"
def total_hits(self):
"Returns the total number of hits on all counted pages"
Important notice
There can be only one!
Notice that only one instance if this product can be installed on a zope server!!!! Trying to install more than one instance will silently fail.
If you install the instance in a subfolder, you cannot later install it in the root folder. So install it there to begin with.
If you accidently install it in a subfolder, and has counts you
don't want to loose, you can do it by first renaming the
counters.dict in the var directory. Deleting the mxm_counter
instance, rename the counters.dict back to the original name,
and create a new mxmCounter instance in the root folder. It will
then use the old counters.dict.
Probably this product should be made placeless. Well that is for another version.
There might be issues with (multiple) ZEO clients
I don't know. I have not tested it in this setup yet. Please try it and give me feedback.
License: GPL
Full license text here
download
You can get it here: mxmCounter.1.1.0.tar.gz
No counters.dict
On our server there's no "counters.dict" created. So after every restart, the counters are lost. We use the following configuration:
Zope Version
(Zope 2.6.4 (source release, python 2.1, linux2), python 2.2.3, linux2)
Python Version
2.2.3+ (#1, Jan 16 2004, 08:08:58) [GCC 3.3.3 20040110 (prerelease) (Debian)]
I installed mxmCounter in the zope root.
On a system with the same database but under W2k it works fine.
Replies to this comment
two tiny observations
2. I've inserted the code to Archetypes objects, which basically can have at least three urls: MYOBJECT and MYOBJECT/view and MYOBJECT/myobject_custom_view (and then the edit views...) Would be nice to think about some acquisition settings or something, otherwise these hits won't be computed...
Anyway, thanks for the nice product!
Jároli József (jaroli at foek dot hu)
Replies to this comment
use Virtual Host
"/"(root) is "example1.com".
"/test" is "example2.com".
I set dtml tags in "/index_html " and "/test/index_html".
Access http://example2.com, I get counts example1.com's.
Though in "set counters" tab shows 2 pages.
Additional method for 'proxy' objects?
Here is a method for the mxmCounter class that I use to expose increase_count() so I can call it from PLFNG:
def proxyObject_increase_count(self,url_path):
return increase_count(url_path, self.save_interval)
Would you consider adding something like this to the mxmCounter baseline? If so, I would eagerly add the hooks in PLFNG (which I am the current maintainer of) to utilize the method. thx!
Problems at inappropriate Zope restart
Thx> yo @@@ jaroli.de
2004-07-26T09:33:51 ERROR(200) Zope Could not import Products.mxmCounter
Traceback (most recent call last):
File "/usr/local/Zope-2.7.0/lib/python/OFS/Application.py", line 654, in import_product
product=__import__(pname, global_dict, global_dict, silly)
File "/usr/local/fenntarthato.hu/Products/mxmCounter/__init__.py", line 1, in ?
import mxmCounter
File "/usr/local/fenntarthato.hu/Products/mxmCounter/mxmCounter.py", line 52, in ?
counters = get_from_file()
File "/usr/local/fenntarthato.hu/Products/mxmCounter/mxmCounter.py", line 43, in get_from_file
c = cPickle.load(f)
EOFError
Replies to this comment
Methods
Thanks you
Replies to this comment
Counter of visits on footer
I would like put on the counter on footer. I have modified the footer adding to
him '<span tal:replace="python:here.mxm_counter.count(here)"/>' but this one
does not appear in the foot of the page. Somebody knows that I must do?
Note: I want to put the counter of my page just as the counter of this page, that is to say, a counter in the foot for each page.
Allowing arbitrary keys
def count_for_key(self, key):
"Variant on mxmCounter.count which allows us to use an arbitrary key."
req = self.REQUEST
... continue as mxmCounter replacing 'url_path' with 'key' ...
...
def get_count_for_key(self, key):
"Variant on mxmCounter.get_count which allows us to use an arbitrary key."
return get_count(key)
(obviously count() and get_ccount() could then be recoded to use these)
This would make it easy for a user to use UIDs for Archetypes or whatever they wish. For example, I use a key which is a combination of the object's UID and an action name such as 'view'.
getting error
i installed mxm_counter in zope, the folder name is mxm_counter, that is the product name displayed in the zmi, its title under properties is "Installed product mxm_counter (1.1.0)" so that looked ok
i then added the "more graphically pleasing version" code to a custom version of 'document_byline' to get a count on all my pages. but get an error:
This site encountered an error trying to fulfill your request. The errors were:
Error Type
AttributeError
Error Value
mxm_counter
the documentation seemed so straight forward i'm embarrassed to have such a basic problem, if anybody can offer some help i would greatly appreciate it.
can't seem to get installed
but still trying to install mxmCounter
i have tried this now on 2 separate plone installations on 2 different pc's with same result. i would give up except this product is ideal for me and need to understand the product install process for future reference as well
what i have done:
1. create and populate C:\Program Files\Plone 2\Zope\lib\python\products\mxm_counter\ folder with product files
2. restart plone, it appears i have a 'good install' i say this because mxmcounter shows up in the zope/control panel/products, and i can get properties, (version 1.1.0) and the readme has the readme.txt contents.
3. go to my plone site 'root' and go to add mxmCounter by selecting it from the drop down box, here's the strange part, after clicking mxmcounter, the screen flashes and just goes back to the starting point, the mxmcounter doesn't stay high lighted allowing me to select/click the 'add' button so i can complete the process??
i was expecting something similar to the add process for the fsCounter product where after you select/click the product in the dropdown box, it stays highlighted, then you select/click the add button, and you get a new screen where there are some id's/start counts/etc to answer, and then the installed product shows up with all the other current plone site objects?
did i miss some intermediate step after the 'zope' install and then adding to my plone site??
sorry this is dragging on, would appreciate very much help on this.
bbxrider
trouble shoot mxmcounter install
ips_to_ignore not working for me
I would appreciate any suggestions.
Replies to this comment
Another great product
To restrict the viewing of the counter to reviewers/managers add:
tal:condition="python:'Reviewer' in member.getRolesInContext(here) or 'Manager' in member.getRoles()"
Simon
Counter is not incrementing
Please help me.
Top 5 Sites in a Portlet
i miss the function, that i can display the Top 5 (visited Sites)in a portlet with direct link or something. Does Anybody knows how it goes?
So Greets
Have you thought of using UIDs instead of paths? Since we probably will all move to AT based objects, all objects will have a UID. It will keep the count when an object is moved, which is better imho.
Danny
(danny dot bloemendaal at companion dot nl)
Replies to this comment