Hi All,

I'm implenting a visitor tracking tool on my website using sessions that not only tracks which page a visitor goes to, but also how long they spend on each page.

I've found that it's quite easy to do as they are moving around WITHIN the site, but I've run into a problem when the visitor actually leaves the site, either by closing his browser window or by going to an external page?

Is there any way for me to tell when a visitor has left my site?

Thanks for any help any one can provide!

JeNNiDeS

    Hi!

    I guess what you need is some javascript which will make a call to whatever you need when the user leaves the page.

    This is not, really not, the perfect way to go, but it may help you on your way!

      heres one way...

      you can make a log and log ips addys into it, then make a script that counts each line theny ou can see how many people visit that page...

      $date = date("F j, Y, g:i a");
      $client = "$REMOTE_ADDR | ". gethostbyaddr($REMOTE_ADDR);
      $FILE = fopen("custome_log.txt","a+"); 
      fputs($FILE,"($date) | $client | Logged!\r\n");
      fclose($FILE); 
      

      also if you are on a unix system you can type this in a shell account

      cat custome_log.txt | grep Logged! | wc -l
      

      or you can do somthing more advanced with a shell script

      #!/bin/bash
      if [ -z "$1" ]; then
      echo "Usage: ./hits <string>"
      exit
      fi
      
      cat custome_log.txt | grep Logged! | wc -l
      echo "Total hits from:  " $1
      

        Thanks for the suggestion, but that's not really what I'm looking for. I already log the visitors ip address (for security issues) when they first register to access the site.

        Now what I am doing is implementing a tracking system so I can determine which pages within the site the user visits, as well as how long they visit each page.

        Using sessions I log the page name of the page the user logs onto and the UNIX timestamp as of when they logged onto the page. Then when they click to a new page I check to see if ($HTTP_SESSION_VARS['login']) isset. If it is I log the UNIX timestamp again as my logout time for that particular page. I then subtract the Logout time from the login time and voila, I know the amount of time they spent on that particular page. I then store that info in a database.

        From that point I unregister (session_unregister()) the UNIX timestamp session variable and the page name variable and then reset them for the new page.

        My problem lies in how do I determine when the user leaves the site altogether???

        I could use javascript as deception54 suggested, but I'm not very javascript inclined. I guess I could trigger a small window to pop up when the user closes his browser or leaves the site saying something like "Thanks for visiting" but I don't like that idea very much.

        Any other suggestions?

        JeNNiDeS

          Originally posted by jennides
          Thanks for the suggestion, but that's not really what I'm looking for. I already log the visitors ip address (for security issues) when they first register to access the site.

          Now what I am doing is implementing a tracking system so I can determine which pages within the site the user visits, as well as how long they visit each page.

          Using sessions I log the page name of the page the user logs onto and the UNIX timestamp as of when they logged onto the page. Then when they click to a new page I check to see if ($HTTP_SESSION_VARS['login']) isset. If it is I log the UNIX timestamp again as my logout time for that particular page. I then subtract the Logout time from the login time and voila, I know the amount of time they spent on that particular page. I then store that info in a database.

          From that point I unregister (session_unregister()) the UNIX timestamp session variable and the page name variable and then reset them for the new page.

          My problem lies in how do I determine when the user leaves the site altogether???

          I could use javascript as deception54 suggested, but I'm not very javascript inclined. I guess I could trigger a small window to pop up when the user closes his browser or leaves the site saying something like "Thanks for visiting" but I don't like that idea very much.

          Any other suggestions?

          JeNNiDeS

          if you run apache, you can get all that info from the access_log

          it has all the pages user access with there ip.. may want to look into that

            heres a working counter for your page, very simple to modify it to show what directorys the users have been in etc...

            <?
            
            $fcontents = implode (" ", file ("custome_log.txt","r"));
            $test = explode(" ", $fcontents);
            $sizeof2 = count($test);
            ?>
            <p><font size="2" face="Arial, Helvetica, sans-serif">You are visiter number: 
              <font color="#003366"><? echo "$sizeof2"; ?></font></font></p>
            
            

            this will just count each line in the log file. the log file should be updated everytime a person hits a page..I have ther eip and the date they entered the page in the log file.

            I bet you could do a if statement or somthing to search for certain things in each lines like page names etc....

              Write a Reply...