How to Implement Landing Page Hit Tracking in ASP.NET (Part 4)
- by Scott Whigham on January 23, 2009 10:50 AMIn Part 1 we talked about the goals of the project and the two base tables. In Part 2 we talked about the LandingPageHit table. Part 3 covered the classes, LandingPage and WebsiteUtilities.
In Part 4 of this long series about how to do conversion tracking with ASP.NET and SQL Server, we'll wire it all up and show you how to track users. So far, here's what we've done:
Just to make sure you're still with me, here's what we've done so far:
- Step 1: Create a new SQL Server database. Execute the commands below in that database
- Step 2: Create/open website project and add references
- Step 3: Create a test page
- Step 4: Create the LandingPageHit table and supporting objects
- Step 5: Create the LandingPage class
- Step 6: Create the WebsiteUtilities class
Step 7: Add our tracking to our pages!
Add the following to your .aspx test page that you created in Step 3:
protected void Page_Load(object sender, EventArgs e)
{
WebsiteUtilities.SetLandingPageHitCookie(Request);
}
Now go browse to that page...
Next, log into your SQL Server and run the following queries:
SELECT * FROM dbo.LandingPage
SELECT * FROM dbo.LandingPageHit
Do you see your hit? Awesome! Go browse to that page again - hit refresh a few times even - and then come back and re-run the queries.
- Are there more rows than there were before? (there shouldn't be - the cookie stored the LandingPageHitId and then, because it exists already, it doesn't re-audit the hit)
Now, go browse to that page using a different browser (or from a different computer). Re-run your queries - now you should still see only one LandingPage but you should now have two rows in the LandingPageHit table!
Step 8 (Final step): Add the code into your registration routine to associate a LandingPageHitId with a UserId
Our final step is to modify your registration routine so that, after you have added your User to the "Users" table and returned your UserId, you can associate the LandingPageHitId with that UserId. We do it like this:
// Your code to add a user and return the UserId goes here
var tracker = new LandingPage();
string landingPageHitId = WebsiteUtilities.GetLandingPageHitId(Request);
if (!String.IsNullOrEmpty(landingPageHitId))
tracker.AssociateSubscriberWithLandingPage(subscriberId, new Guid(landingPageHitId),
WebsiteUtilities.ConnectionString);
// Your code to redirect your user after login goes here
And that's it - once you implement this code, you can do conversion tracking with SQL and ASP.NET. You can write reports that trace users to specific landing pages and find out which of your pages convert the best.
A Few Words on Security
Do yourself a favor - don't just blindly insert the code that I've posted into your website. There are lots of bad things that could happen if you don't protect yourself. There are lots of things that I have in my production code that I didn't post here (for the sake of brevity) - things like:
- SQL Injection
- Cookie stealing
- XSS
- Limiting cookies to this domain only
- String sanitization
- Encryption
- Url encoding and decoding
And so much more... This post is about How to Audit Users on a Website using ASP.NET and SQL Server - it's not about every little possible thing.




Merely want to say your article is stunning. The clearness in your post is simply spectacular and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with incoming post. Thanks a million and please keep up the strong work.
I am relieved to find out that there is actually a little good content left on the internet. I've gotten so fed up with google delivering me garbage.
Twitter are a MUST for webmasters!
If the economy turns up, the government stands to gain due to increased tax revenues. The people can figure on another seven years of famine, though.
Great Post. I would love to read more in future. keep up the good work. Yearly hosting http://www.intelweb.biz