"deJulio" <br**************@directbox.com> writes:
Ich wollte auf meiner Homepage eine Statistik führen, wie oft jeder
Link/Bild angeklickt wurde (also die Klicks auf jeden Link/Bild zählen).
Ich hoffe mir kann jemand helfen.
Schon mal danke im voraus.
I can almost read German, but I won't force you to endure my attempts
to write it :)
You will have to collect the statistics on the server in any case.
If the links you want to count are to your own pages, you chould be
able to get the same information from the server logs, or at least
by using a server-side script.
To count clicks to other pages, you need to communicate with the
server. Catching the click is easy:
<a href="some other site" onclick="reportClick(this.href)">link</a>
It is the contents of the reportClick function that can be
problematic.
The simplest method is to initiate a load of a server resource,
most likely a small image:
function reportClick(link) {
var img = new Image();
img.src="http://www.example.com/tinyimage.png?link="+
escape(link)+"&salt="+new Date().valueOf();
}
The "new Date" part is there to make each click unique and prevent
caching of the image from interfering with your statistics.
It is probably better to use the server to mark the image as
non-cacheable. It would be optimal if the browser only checks
whether the image has been updated each time, but doesn't fetch
the content, but I can't find an easy way to achive that.
The problem with this approach is that you will immediately start
loading a new page, which might stop the loading of the image
before it even connects to the server.
A more convoluted, and more time consuming, method is to wait for
the image before loading the new page:
<a href="some other site" onclick="return reportClick(this.href)">
link
</a>
and
function reportClick(link) {
var img = new Image();
img.onload = function(){location.href=link;};
img.src="http://www.example.com/tinyimage.png?link="+
escape(link)+"&salt="+new Date().valueOf();
return false;
}
This code should (warning:untested) load the image and *then* load
the requested page. That makes the user wait longer.
At least he will probably think it is the other site that is slow :)
In all cases, you *will* need server side scripting, or at least
access to the server logs.
All that said, you should tell people very explicitly that you are
tracking their browsing behavior. Otherwise I would consider it an
invasion of privacy and warn people against your site.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'