views:

105

answers:

5

Let me reformulate, as the answer http://stackoverflow.com/questions/951907/where-are-my-visitors-going was absolutely correct, but my question not precise enough ;)

How does one track with Java Script where the visitors are going? (From a technical standpoint.)

Is the idea to execute a code every time a link is pressed? If yes, does this have to be specified in the <a>-tag itself, i.e., <a href="..." onmousedown="return mycode(this)">, or can this be done globally without having to mention it for every link?

I don't want the specifics of any code (as there is GoogleAnalytics etc.), I just want to know generally how it could work.

Btw, you guys are really quick!

+2  A: 

You can write an event that records visitor activity per anchor tag or you can write a script that scans the document and does it for you (which is what Google Analytics does). If you choose to use a script, make sure you put it at the end of the document so that your web page is as responsive as possible.

You can easily iterate through anchor tags as follows (untested):

var tags = document.getElementsByTagName("a");
for (var i = 0; i < tags.length; i++) {
    tags[i].onclick = function() {...};
}
Kai
great advice, thanks a lot!
jacob
Jacob, if you use Google Analytics my answer below includes a link to a script that will capture all clicks on outgoing links.
Colin
+1  A: 

This link may give you some idea - http://blog.ndrix.com/2007/07/how-google-analytics-works.html

  • A visitor loads a page on your website
  • In the process, her browser loads and runs some Javascript from Google
  • That Javascript collects information about the visitor
  • The information is sent to Google by requesting a URI and passing the details as CGI parameters
NinethSense
+1  A: 

Tracking clicks that leave the website will require code for those specific links; there is no global way of tracking those clicks. If it's only to track internal links then an analysis of the web server log will provide that info globally, without any specific code for each link.

ftank99
+1  A: 

"Is the idea to execute a code every time a link is pressed?"

Yes, that's the general idea. And yes, you can implement something "globally" to capture all outgoing links.

This piece of Javascript should do the trick for you (if you're using Google Analytics). Every time an <a> tag is clicked that points to an external webpage, it is tracked by Google Analytics based on how you want outgoing links named (see line 29).

Colin
thanks for the link Colin!
jacob
+1  A: 

One possibility:

(from memory, probably has bugs in it...)

First, put a hidden div on the page and put an image inside it:

<div id="hiddenDiv" style="Display: None"><img id="someImage" /></div>

Next, put JS on each outgoing link that fetches a new image for that img tag:

    <a href="http://www.someothersite.com/"
onclick='javascript:getElementById("someImage").src = 
"http://www.yoursite.com/trackingimage.gif?ClickedSite=SomeOtherSite&amp;LinkID=LeftSOSLink"&gt;
Visit Some Other Site</a>

Now, just search your web log for requests for trackingimage.gif and figure out which links leading to which site were clicked.

Aric TenEyck
that's a nice hack :)
jacob
There might be a timing issue - you have to hope that the image is fetched before the browser moves on to the next page.
Aric TenEyck