Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Tracking Referring Domains and URLs to our Website using ASP.NET
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed
Arranging the Tables

The tables will now look like this.

SD_Tut_Tracker & SD_Tut_ReferringDomains

We'll now move to the 3rd and the last table.

iii. SD_Tut_ReferringURLs
Right-click in the empty white space of the diagram and from the options menu that pops-up, click 'New Table'. Then enter the name SD_Tut_ReferringURLs and hit 'Ok'.

Now add 3 columns to the table as column names, data types, length, and 'allow nulls' given below:

SD_Tut_ReferringURLs

As for the Primary Key, select TrackerDateID and ReferringURL columns and then click the 'Set Primary Key' button in the toolbar. And then click the 'Save' button in the toolbar to create this table in the database.

Explanation
The 3 columns of SD_Tut_ReferringURLs serve the same purpose as the ones in SD_Tut_ReferringDomains, except that the ReferringURL column contains the full referring URL whereas ReferringDomain column contained only the referring domain name. The logic for setting Primary Key on two columns is the same as was in SD_Tut_ReferringDomains table.

To give you an idea as to what kind of data this table will contain, have a look at the example row from this table:

SD_Tut_ReferringURLs

Now that we have created the 3 tables, the last remaining step is to add relationships between them. For that, the first thing to do is to switch the view of SD_Tut_ReferringURLs to 'Column Names' as was described earlier in this tutorial.

The 3 Tables of our Tracker Application

Creating Relationships between Tables
We'll be creating two 'One-to-Many' relationships with the column TrackerDateID of SD_Tut_Tracker as the Primary Key and TrackerDateID columns of SD_Tut_ReferringDomains and SD_Tut_ReferringURLs as the Foreign Keys.

To create these relationships in the diagram view of the SQL Server Enterprise Manager, first of all click the TrackerDateID column of SD_Tut_Tracker table. While keeping the left-mouse button pressed, drag that column to the TrackerDateID column of SD_Tut_ReferringDomains table. Now, release the button. A 'Create Relationship' dialogue box will appear on your screen as shown below:

Creating Relationships between Tables

Check both 'Cascade Update Related Fields' and 'Cascade Delete Related Records' checkboxes. This will make sure that no ghost records in the SD_Tut_ReferringDomains and SD_Tut_ReferringURLs tables will exist once the Primary Key of this relationship is updated or deleted in the SD_Tut_Tracker table. Now click 'Ok'.

Repeat the same procedure for creating a 'One-to-Many' relationship between SD_Tut_Tracker and SD_Tut_ReferringURLs tables where the TrackerDateID column of SD_Tut_Tracker will act as the Primary Key and TrackerDateID column of SD_Tut_ReferringURLs table as the Foreign Key. The procedure has been explained already in this tutorial. Also make sure that you check 'Cascade Update Related Fields' and 'Cascade Delete Related Records' checkboxes as was the case with the relationship between SD_Tut_Tracker and SD_Tut_ReferringDomains tables. Now click the 'Save' button in the toolbar to create these relationships in the database.

When you are finished creating these two relationships as was explained above, click the 'Arrange Tables' icon in the toolbar and you will have something like the following image displayed before your eyes:

Adding Relationships

Overview of the Stored Procedures
We will be creating a total of 14 stored procedures, out of which only one stored procedure will write to the 3 tables we created above, while the other thirteen stored procedures will be used to generate reports which will be displayed by ASP.NET pages later in this tutorial. Following is a list of names of the stored procedures that we will create:

  1. SD_Tut_GetDailyReport
  2. SD_Tut_GetDailyReportForReferringDomains
  3. SD_Tut_GetDailyReportForReferringURLs
  4. SD_Tut_GetDetailedDailyReportForReferringDomains
  5. SD_Tut_GetDetailedDailyReportForReferringURLs
  6. SD_Tut_GetDetailedMonthlyReportForReferringDomains
  7. SD_Tut_GetDetailedMonthlyReportForReferringURLs
  8. SD_Tut_GetMonthlyReport
  9. SD_Tut_GetMonthlyReportForReferringDomains
  10. SD_Tut_GetMonthlyReportForReferringURLs
  11. SD_Tut_GetYearlyReport
  12. SD_Tut_GetYearlyReportForReferringDomains
  13. SD_Tut_GetYearlyReportForReferringURLs
  14. SD_Tut_Track

Creating the Stored Procedures
Unlike the procedure for creating the tables, which was a bit lengthy to explain, we will create these stored procedures one-by-one using the SQL Server Query Analyzer, which is a simple and quick procedure and you will be able to execute it within seconds.

i. SD_Tut_GetDailyReport
This stored procedure takes two arguments which can be NULL. What it means is that if you don't supply any arguments, it will use current year and month as the arguments for generating the report. The code for this stored procedure is:

CREATE PROC [dbo].[SD_Tut_GetDailyReport]
	@ForYear int = NULL,
	@ForMonth int = NULL
AS
	SELECT @ForYear = ISNULL(@ForYear, YEAR(GETDATE()))
	SELECT @ForMonth = ISNULL(@ForMonth, MONTH(GETDATE()))

	SELECT DAY(TrackerDateID) AS 'Day', TrackerUniqueUsers AS 'Unique Users',
	TrackerPageViews AS 'Page Views' FROM SD_Tut_Tracker WHERE
	YEAR(TrackerDateID) = @ForYear AND MONTH(TrackerDateID) = @ForMonth
	ORDER BY DAY(TrackerDateID) ASC
GO

To create this stored procedure, first of all open SQL Server Query Analyzer and complete the 'log-in' procedure so that you connect to the database you are creating the tables/stored procedures in. Then simply copy the code given above and paste it in the Query Analyzer code window and execute it (by pressing the 'F5' key).

Explanation
It displays all the records of the given year and month from the SD_Tut_Tracker table in the format shown below:

SD_Tut_GetDailyReport

Previous ( 2 Gone )( 12 Remaining ) Next

Comments/Questions ( Threads: 4, Comments: 8 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. trying to follow instruction but ran into this error which I tried to gogle out to no avail
  2. How to track Users visit time and Page Viewstime on web pages ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  3. Maybe I am dumb ( 1 Reply )
  4. Great tutorials ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.