May 31, 2010

finding a way to track website users

Question by sdr

I want to know that is there any way to know after which page an user is leaving the website.

So that I can track that page and work to improve the content of that page.

I am using PHP as backend code.

Answer by Starx

If you want to see which is the last page a user is visiting, you can do something like this

I am assuming that you have a mysql connection already provided.
Create a page like “session.php” and place this function

function trackpage($pagename) {
    if(!isset($_SESSION)) { 
       // if the user is visiting the website for the first time
       session_start();
       $query = "INSERT INTO `newusers` VALUES(NULL, '$pagename');";
       //In case table 'newuser' have two fields 'id' and 'page' to keep a track of page
       $result = mysql_query($query) or die(mysql_error());
       $_SESSION['userid'] = mysql_insert_id();
    }
    else {
       //if he is just visiting another page
       $query = "UPDATE `newusers` SET page='$pagename' WHERE id='".$_SESSION['userid']."';";
       $result = mysql_query($query) or die(mysql_error());   
    }
}

Npw you need to include this on every page. on the top write this

<?
include "session.php";
trackpage("index.php");
?>

Now you know on what page are people leaving the website

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!