Webzähler

D

deJulio

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.

Enrico
 
L

Lasse Reichstein Nielsen

deJulio said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top