Page 1 of 1

Real time Page view counter...help appreciated

Posted: Tue Jun 30, 2009 11:48 pm
by scarface222
I was just wondering if anyone knew if it is possible to count how many people are viewing a certain page at one time not just the whole site, an individual page and if so a suggestion on how to approach this.

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 3:34 am
by iamngk
why can't you try google analytics (http://www.google.com/analytics/)

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 9:38 am
by BornForCode
Avoid putting 3'rd party code on your site, this is my big advice. I hate to see that sometimes analytics are not so well responsive and a page is stuck in waiting a response from them.

Use your own server logs or implement an observer that will handle this and attach it to your controller.

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 3:37 pm
by scarface222
Thanks for the suggestions guys but iamngk is right I do not want to use google analytics to do this and do not want to see the results for myself I want to update the views in real time which I know how to do and possibly have it inserted into my database so I want to create a simple system to work within my own server. I just want to know how to measure the views I am unsure how to approach this. I have never done this before, can you expand on your response? How do I attach an observer to a controller?

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 3:46 pm
by BornForCode
Damn sometimes i think too far :), so forget about controllers and observers.

For the beginning make a simple script that is increasing the view number. The script is increasing the view numbers only if you don't have a record for the session in the last 30 minutes. This should to the job.

In a table keep the pages, you will have a structure like: Id, Page, VisitCount and in the other one you will have a structure like: Id, PageId, SessionCode, DateAdded.

When someone access your page, you increase the VisitCount and add a record in the second table with the visitor session (if he didn't visited that page in the last 30 mins)

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 5:24 pm
by scarface222
Thanks bro that sounds like what I need. I am just curious though how would you approach a session say if I am using this counter script

Code: Select all

<?php
// Connects to your Database
mysql_connect("...", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
 
//Adds one to the counter
mysql_query("UPDATE counter SET counter = counter + 1");
 
//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT counter FROM counter"));
 
//Displays the count on your site
print "$count[0]";
?>

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 5:52 pm
by Eric!
Do you really want to track the sessions too? That seems like a lot of work just to filter out some page refreshes and people who view the page several times within a short time frame.

If you want to setup a session on each page and track it and expire and reset the session after 30 minutes, then set a session variable when you create a session for that visitor. When they comeback check that variable. something like this but this applies to your entire site, so only visits to the first page in your site will be counted. You have to create a seperate variable per page if you want to sort out page by page visits.

Code: Select all

   // session expiration time: 30 min 
session_start();
 if(isset($_SESSION['expire']) && (date("U") - $_SESSION['expire'] > 60*30))
   {
        //session expired
       //call routine to update page count
       $_SESSION['expire'] = date("U");//reset timer
    }
   if(!isset($_SESSION['expire'])) {
     $_SESSION['expire'] = date("U");
    //first visit call routine to update count
}

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 8:33 pm
by scarface222
Isn't it better to use a database, then that way if that view a different page they won't still be counted on the other page?

Re: Real time Page view counter...help appreciated

Posted: Wed Jul 01, 2009 10:04 pm
by Eric!
scarface222 wrote:Isn't it better to use a database, then that way if that view a different page they won't still be counted on the other page?
You can track them with the session_id() and a session array with previously viewed pages. Then have php update the database page count based on the users current page status. If you store the expiration times in the database you'll just need each script to scan for expired sessions every time it is run to adjust the counts. Putting expiration time in the database will probably give you a better real time picture.

Re: Real time Page view counter...help appreciated

Posted: Thu Jul 02, 2009 10:08 pm
by scarface222
Thanks for the reply. It sounds like what I need however I don't quite understand the session_id and session array idea about tracking previous pages. Also I want to display page views for each page on the homepage so how do I approach that? Will the session page tracking idea cover that? I am still a pretty big amateur at programming but have the basic idea of what you are saying. Could you maybe illustrate with an example so I can look at it and read what you are suggesting? Again I appreciate your time.

Re: Real time Page view counter...help appreciated

Posted: Fri Jul 03, 2009 9:07 am
by Eric!
Here's a rough idea

Code: Select all

session_start();  // need this on each page you want to track
$sess_id=session_id();  // this is you users session number
$page=$_SERVER[’PHP_SELF’]; // page file currently viewed ignoring parameters like ?do=search
$expire = date("U");//time right now
 
ok now go into the database and see if this session id exists. if not add it to your list with the page and expire time, update page count. If it does exist, check the last page this id was viewing and decrease that count. increase the count for the current page, update the expire time to now. do some database maintenance, scan for ids that have not been updated for 30+ minutes and adjust the page count and drop that id from the table. You could also do a tally for your page counts and display them.

You could make a seperate table with pages and counts to make sorting quicker. Have fun!

Re: Real time Page view counter...help appreciated

Posted: Fri Jul 03, 2009 4:12 pm
by scarface222
appreciate it man