Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Using 404 Not Found For An Email Tracking Image / Webbug

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
12 Nov 2009CPOL2 min read 39.7K   247   27   21
The technique of inserting email tracking images, which avoids the give away querystring

Introduction

This code offers a very simple way to track emails and see whether or not they have been displayed by the recipient. Because this technique does not put a query string after an image, it is undetectable by spam engines, making it appropriate for legitimate newsletters, etc. that you want to track without being spam blocked.

Background

Tracking emails by logging the request for an image is a commonly used technique. This example varies from many in that it does not need a query string after the image to send back information about the user, e.g.

someimage.gif?userdata=12345 which is easily spotted by antispam engines.

Instead this method stores the user data in the name of the image, i.e.

myLogoUser12345.gif which is indistinguishable from any other image request.

In order to use this technique, we must re-configure IIS to call our page instead of the standard 404 page not found. Whenever a request comes for a gif- image that does not exist, we log that request and return a logo or in this case, a transparent 8x8 pixel GIF.

Using the Code

Simply put the following code in a page called 404.aspx, set up your access database (or change for SQL if you have a SQL Server at your disposal) and you can start tracking.

To track an email, simply include a remote image in your message. The remote image in the email points to an image on your server. The trick is that the image MUST NOT ACTUALLY EXIST. This causes our custom 404 handler to be called. Our custom handler then returns a transparent 8x8 pixel GIF, and the request is logged in a database.

VB.NET
<%@ Page Language="VB" %>

<script runat="server">

' this page is configured in IIS to replace the standard 404 page not found page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
Dim sa As String 

' at this point we already know the page / file does not exist, now we check to see
' if a gif was requested 
If Request.Url.ToString.EndsWith(".gif") Then 

' first we return the invisible 8x8 gif trtr.gig 
Response.ContentType = "image/gif" 
Response.WriteFile(("~/trtr.gif")) 

'next we log the request in our database, the name of the gif forms our tracking data 
sa = Request.Url.ToString.Remove(0, Request.Url.ToString.LastIndexOf("/") + 1) 
sa = sa.Replace(".gif", "") 
Dim ads As New AccessDataSource 

' replace this with the location of your writeable database 
ads.DataFile = "D:\websites\thear1\data\campaign.mdb" 
ads.InsertCommand = "INSERT INTO [tracking] ([keyword], [time], [date], [url],
    [referer]) VALUES (?, ?, ?, ?, ?)" 
ads.InsertParameters.Add("keyword", Request.ServerVariables("REMOTE_ADDR")) 
ads.InsertParameters.Add("time", Now.ToLongTimeString) 
ads.InsertParameters.Add("date", Now.Date) 
ads.InsertParameters.Add("keyword", sa) 
ads.InsertParameters.Add("referer", Request.ServerVariables("REMOTE_ADDR")) 
ads.Insert() Else 

' the missing file / page was not a gif, so return as page not found message 
sa = Request.Url.ToString.Remove(0,
    Request.Url.ToString.LastIndexOf("/") + 1) Response.Write("Page " &
    sa & " does not exist ") 

End If 

End Sub 

&</script>

Changing IIS to Use Your Page for 404 Requests

If you do not have access to your own server, then you will need to ask your hosting company to point your 404 page not found page to your custom 404.aspx page (which most companies will do without issue).

Launch inetmgr then go to the custom errors tab and modify the 404 entry:

changing IIS

History

Stay tuned for the next article which will show you how to display your data visually with pie charts and histograms.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
United Kingdom United Kingdom

Comments and Discussions

 
GeneralUsing Captcha to stop spam Pin
Jon Anthony20-Nov-09 10:31
Jon Anthony20-Nov-09 10:31 
GeneralRe: Using Captcha to stop spam Pin
Keith Yerian16-Dec-11 2:36
Keith Yerian16-Dec-11 2:36 
GeneralAnother approach. Pin
gstolarov19-Nov-09 4:39
gstolarov19-Nov-09 4:39 
Normally solution would involve placing 1*1 pixel image into the html email referencing to the custom handler. Replacing default page not found handler have site-wide effect and not too many people would go for it.
GeneralRe: Another approach. Pin
Jon Anthony20-Nov-09 10:11
Jon Anthony20-Nov-09 10:11 
GeneralI agree with another poster. It appears to me that you are encouraging spam Pin
Shane Story17-Nov-09 2:50
Shane Story17-Nov-09 2:50 
GeneralRe: I agree with another poster. It appears to me that you are encouraging spam Pin
Chris Maunder17-Nov-09 11:48
cofounderChris Maunder17-Nov-09 11:48 
GeneralRe: I agree with another poster. It appears to me that you are encouraging spam Pin
Shane Story17-Nov-09 13:22
Shane Story17-Nov-09 13:22 
GeneralRe: I agree with another poster. It appears to me that you are encouraging spam Pin
WPKF17-Nov-09 20:29
WPKF17-Nov-09 20:29 
GeneralRe: I agree with another poster. It appears to me that you are encouraging spam Pin
Shane Story18-Nov-09 2:49
Shane Story18-Nov-09 2:49 
GeneralRe: I agree with another poster. It appears to me that you are encouraging spam Pin
Chris Maunder22-Nov-09 12:54
cofounderChris Maunder22-Nov-09 12:54 
GeneralHow to stop spam with CAPTCHA challenge Pin
Jon Anthony20-Nov-09 10:30
Jon Anthony20-Nov-09 10:30 
General404 - DOS attack Pin
ComplexityChaos16-Nov-09 2:53
ComplexityChaos16-Nov-09 2:53 
GeneralRe: 404 - DOS attack Pin
Jon Anthony20-Nov-09 10:12
Jon Anthony20-Nov-09 10:12 
GeneralASP.NET Routing Pin
HightechRider13-Nov-09 6:22
HightechRider13-Nov-09 6:22 
GeneralRe: ASP.NET Routing Pin
Jon Anthony14-Nov-09 1:54
Jon Anthony14-Nov-09 1:54 
GeneralExample Database has information in the wrong fields Pin
Dewayne Dodd13-Nov-09 5:37
Dewayne Dodd13-Nov-09 5:37 
GeneralRe: Example Database has information in the wrong fields [modified] Pin
Jon Anthony14-Nov-09 1:51
Jon Anthony14-Nov-09 1:51 
RantIf you have to out-think spam filters, YOU'RE SPAM! Pin
dwilliss12-Nov-09 15:20
dwilliss12-Nov-09 15:20 
GeneralRe: If you have to out-think spam filters, YOU'RE SPAM! Pin
vmarcano13-Nov-09 0:04
vmarcano13-Nov-09 0:04 
Questionimage must not exist Pin
vmarcano12-Nov-09 14:03
vmarcano12-Nov-09 14:03 
AnswerRe: image must not exist Pin
Jon Anthony12-Nov-09 14:06
Jon Anthony12-Nov-09 14:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.