It depends on what you're trying to track. If you're trying to track the user on the fly as they move through your site, you'd be better of putting a small token into session - as Ricardo states in his answer.
If you want to track how particular links are performing on your site, or which links a user is following, then you'll need to use some JavaScript:
Basically, you'll need to create an onclick
event on the <a>
tags that calls into a service (or similar) on your server to track/log the fact that this click has happened.
If this method returns true
, then control will return to the browser, and the original href will be requested.
This example assumes the use of jQuery, but other libraries will allow you to perform similar things - you could use all native ASP.NET tools, I just had this code to hand:
<a href="/someUrl.aspx" onClick="javascript:TrackThis(this);return true;">text</a>
<script type="text/javascript">
function TrackThis(link){
$.ajax({
url: "/WebServices/ClickTracker.asmx/Track",
type: "POST",
dataType: "json",
data: "{\"url\":\"" + link + "\", \"page\":\"" + window.location + "\"}"
contentType: "application/json; charset=utf-8"
});
}
Then you can create a simple web service that takes the values you're passing in (in this case the value of the link the user clicked on, and the url of page they were on at the time), performs its processing, and keeps out of the way.
Google do something very similar to this with their search results - the URL in the browsers status bar and that you arrive at is the one from the indexed site, but in the middle you visit a tracking page on Google's servers to say that you clicked on it, which uses the onclick
event.