locked
page views -visits and counts RRS feed

  • Question

  • User-371148474 posted

    Hi,

    Am adding extra features to a blog website -- they need help counting how many times a post/article is being viewed to generate other info like most read post,most Favorited posts etc..

    How do i design the db table to accommodate this and how do i construct and implement the above features from the code end..

    Friday, October 17, 2014 2:37 PM

All replies

  • User379720387 posted

    pageview counter

    Everytime there is a page requested you increment the counter.

    Friday, October 17, 2014 2:47 PM
  • User1308270069 posted

    Create a table in Database that has an article ID and a count column with 0 as a default value. every time the page is being viewed add 1 to that count column.

    Monday, October 20, 2014 7:35 AM
  • User-735851359 posted

    Hello,

    Add one integer column (Visited) to the ARTICLES table with Default Value 0 (zero).

    On the Article Show page, add the following code.

        var id = UrlData[0];
        var data = Database.Open("db");
        var sqlQ = "SELECT ARTICLEID, Title, InfoShort, ... , Visited FROM ARTICLES WHERE ARTICLEID = @0";
        var info = data.QuerySingle(sqlQ,id);  
    
        int incVisited = info.Visited; 
        incVisited++;
        var v1 = "UPDATE ARTICLES SET Visited=@0 WHERE ARTICLEID= @1";
        data.Execute(v1, incVisited, id);

    Mark as answer if it helped

    Monday, October 20, 2014 12:54 PM
  • User-2051275383 posted

    Create a table in Database that has an article ID and a count column with 0 as a default value. every time the page is being viewed add 1 to that count column.



    This method is easy to code up and employ however this will only work if you want total hits - Not unique hits, further user validation would be required to capture only unique (per user traffic)..

    Wednesday, October 22, 2014 6:23 AM
  • User-371148474 posted

    A-rad,

    Thanks for the insight, but how do I only count from unique hits and not just total hits

    Wednesday, October 22, 2014 8:34 AM
  • User-2051275383 posted

    A-rad,

    Thanks for the insight, but how do I only count from unique hits and not just total hits



    This might be complicated for a beginner. You would need to build something that relates to the session of the user (their unique IP inclusive). There are people that have tried this (http://stackoverflow.com/questions/7005904/how-to-capture-unique-user-sessions-in-webmatrix-razor-asp-net-web-pages) but there also might be an out-of-box open source solution to this too.

    I also found this code where someone built a desktop application to monitor the traffic on their website (razor site), components of this may assist you in your enquiry: http://www.codeproject.com/Tips/479303/Easy-Desktop-Web-Stats

     

    Sunday, November 2, 2014 1:59 AM
  • User-371148474 posted

    In the complexity of learning this algorithm, I think I have found the closest to identifying a unique visitor using Session-State..

    Can I have this interpreted into Razor Syntax

    string vidID = Request.QueryString["v"];
    
            SqlConnection conn;
            SqlCommand cmd;
    
            if (!Page.IsPostBack)
            {
    			if(!string.IsNullOrEmpty(vidID)) //kicjk it if the quesystring is not passed
    				if(Session[vidID] == null) //Session is null means user is visiting for first time
    				{
    					conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["XXX"].ConnectionString);
    					cmd = new SqlCommand("UPDATE TabVideos SET VideoHits = VideoHits+1 WHERE VideoID=@VideoID", conn);
    					cmd.CommandType = CommandType.Text;
    
    					cmd.Parameters.AddWithValue("@VideoID", vidID);
    
    					using (conn)
    					{
    						conn.Open();
    						cmd.ExecuteNonQuery();
    					}
    					
    					Session[vidID]  = vidID;
    				}
    			}
            }
    Sunday, November 2, 2014 4:01 PM
  • User-821857111 posted

    Add Google Analytics to the site and then query it using their Reporting API.

    Sunday, November 2, 2014 5:10 PM