• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

How to do page tracking by javascript and/or C#?

hi all,
our business requires us to track how many hits for each page in our website. it means, i have to record the PageName, the NumberOfHits in our database table.

p.s. most pages in our website are HTML not .aspx.

can anyone please help me? give me the sample code or points me to the right direction?

i want to use javascript and/or C# to achieve this.

thanks a lot
2 Solutions
HainKurtSr. System AnalystCommented:
create a basepage, say myPage
add a property say pageid

on load event add some code, to connect to db and call sp, say addMyPage(pageid)

this sp in your db will do this
insert into PageLog(pageid, visitdate) values(pageid, getdate())
where PageLog is a table with two columns: pageid, visitdate (default=getdate)

inherit your all pages from this instead of default ui.web.page
and set pageid=26735 on pageinit event
HainKurtSr. System AnalystCommented:
or just create a class/module with one shared/statuc method AddPageLog(pageid) which calls the sp i explained above
and run this method on all pages init vent like


also, create a Pages table in your db

Pages : PageID, PageName, Description, Path,....

Just a thought:

You can use Javascript to make a request to a c# page. You fire the javascript function when your html page loads; the c# page receives the request, saves the name of the html page in a db, or adds one to the visit counter if the record already exists. The C# page returns the number, and the javascript puts that into a DIV.

It's ajax.

A simple way to do it is using the JQuery Ajaxify plugin for making the request.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

If possible, did you try Google Analytics ?

Check this article : http://www.15seconds.com/issue/021119.htm
Dave BaldwinFixer of ProblemsCommented:
The server logs normally record all of this information already.  Do you have your own server and/or do you have access to the logs?
viola123Author Commented:
HainKurt: thanks a lot, but our most webpages are HTML not ASPX pages, so i think purquiz's idea is more suitable to me. is there any sample code you can show me?

purquiz: could you pls give me some reference link  or sample code?

leakim971: i know we can use third party tools, but we have to do it by ourselves,   this is a business decision.

DaveBaldwin: we already have a log file to log everything, but we don't wanna use it cos the file is big and messy.

thanks for all your answer

HainKurtSr. System AnalystCommented:
if it is asp/html, probably you have a footer.inc that you include in all pages...

what I think is, create a page, counter.asp, which creates a counter image (you can send 1x1 blank image if you wish). When called it will connect to db, run the sp, creates / sends the image to the page... from any page just use something like this

<img src="Counter.asp?PageID=234234&rt=<%= now() %>">

we are adding rt to make sure it is not cached...

if you wish you can use this

<link type=javascript src="Counter.asp?PageID=234234&rt=<%= now() %>"> and return nothing... or this
<script scr="Counter.asp?PageID=234234&rt=<%= now() %>"></script> if you do not use any image counter or blank images...
viola123Author Commented:
not quite work for my issue. but i have to close this question, so i can ask another one. anyway, thanks you all.
by the way, the website is outsourced, so no need to worry about it now.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now