qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbffd60000000000bd00000001000200
Often with web applications you want to track traffic statistics to get a general idea of the number of visitors viewing a resource. Whether it be a web page, an RSS feed, an e-mail or whatever, you might want an inconspicuous way to determine that it has been "viewed". A common tactic for tracking visitors is the use of what is known as web bugs. Web bugs are small 1x1 pixel images that will be put on a web page or e-mail that will log information about the requestor each time it is requested or consumed.
Creating web bugs, or tracking images, in ASP.NET is easy and really does not require much effort. You just create a webform (aspx) that writes the byte array of the image to the response stream and sets the content type to "image/gif". There are few other things to take into account to make the information a little more accurate, such as using the If-Modified-Since header to limit the frequency of logging the resource visit, such as only recording the visit if the page has been not been viewed by a particular visitor within the last 24 hours, and otherwise sending back an HTTP status 304 ("not modified" status for a conditional GET) so you're not duplicating visit counts from quick clicks or repeats within the last 24 hours.
Let's take a look. What we'll do in this sample is use a static byte array that will represent a 1x1 pixel image (got that idea from .Text and is an easy route to take). You could create a byte array to represent any image you wanted, but we'll just keep it simple.
public class ResourceTrack : System.Web.UI.Page
{
private static byte[] _imgbytes =
Convert.FromBase64String("R0lGODlhAQABAIAAANvf7wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
// check if-modified-since header to determine if we
// should log again or send back a not modified result
if (useCached(this.Context.Request))
{
Response.StatusCode = 304;
Response.SuppressContent = true;
}
else
{
// add code to log visit here
// such as write to a database
Response.ContentType = "image/gif";
Response.AppendHeader("Content-Length", _imgbytes.Length.ToString());
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.BinaryWrite(_imgbytes);
}
}
private bool useCached(HttpRequest req)
{
string ifmod = req.Headers["If-Modified-Since"];
return ifmod == null ? false : DateTime.Parse(ifmod).AddHours(24) >= DateTime.Now;
}
}
Simple enough. Now you can add that page just as you would any regular image.
<img src="http://www.mysite.com/ResourceTrack.aspx" width=1 height=1>
That's great, but what if you need additional information? Keep in mind that this "image" is really just an aspx webform. You can get to whatever you need. If you need to collect the IP address, user agent, or whatever you can get to it. Also, you can even pass values to it in the QueryString to keep track of things such as an article or user ID. You'd just have to build in the ability to look for the value in the QueryString, then you'd just add it to the URI for the source of the image tag.
<img src="http://www.mysite.com/ResourceTrack.aspx?someid=34&otherid=78" width=1 height=1>
I use this kind of thing in RSS feeds from some of my websites to track the aggregate views and to track page views on a few sites also. To track counts for RSS feeds, I just add the image into the item description, ala .Text style. Works great. However, there are some things to consider when using this approach. If you are using web bugs in e-mails then you might as well forget it. This is a common approach taken by spammers to verify a valid e-mail address so e-mail clients, such as Outlook 2003, won't download images into the e-mail body by default. Your web bug image never gets pulled from the server so you won't get accurate counts logged. Most RSS readers will pull the images by default, however some do have options (such as RSS Bandit) to not pull down images from a post. I do think it is the best route for tracking RSS feed items (at least until something better comes along). For use on web pages, it would be a safe bet since the page will serve up images aleady.