Forum Moderators: coopster

Message Too Old, No Replies

Recording page views accurately

         

username

2:32 am on Oct 8, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, I have an application which records page views for unique pages. My issue revolves around accurately recording the views of each page, so as to avoid someone just reshreshing a page multiple times thus skewing the results.

At the moment, I am thinking of recording IP addresses in an XML document for each page using SimpleXML, and checking that. My issue is obtaining definite IP addresses for this solution. Currently I am checking IP's using:

/* Get IP Address of User */
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//to check ip is pass from proxy
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}

Can someone confirm whether this is actually working, because I have a feeling this is not going to work for work groups with the same IP address being used by multiple users, thereby skewing my results.

Thanks in advance.

PHP_Chimp

7:08 am on Oct 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cant use IP address to get accurate results.

Providers like AOL may change IP address with each request, due to the large number of proxies. IP addresses can be faked. IP address is advertised as more than just the 3 options that you have in your code below. People on the same network may well share the same external IP address. There are more reasons for the inaccuracy.

However you can use IP address to get results (so the problem is with how accurate you want, not the method of obtaining results). These will not be 100% accurate, but they are an easy way to track visitors. If you require an accurate track of visitors then you would need to use something like a session for each visitor.

The answer to your question lies within it.

so as to avoid someone just refreshing a page multiple times thus skewing the results.

As this is a client side activity you are trying to monitor you need a client side script.
The alternative is to use a session and only record the page view if the page before was a different page.

So the questions you need to answer is:
How accurate do you need?
Is it important if people can skew your results?

If your results are used for marketing then if a few people refresh there page then that will skew your results by 0.1%, not a problem. If you are running a pay per click (and you are doing the paying) then it is more important.

andrewsmd

1:38 pm on Oct 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using a session variable, this will remain the same for each user as long as they keep their browser open. IP addresses aren't the best way because anyone coming from the same LAN could potentially show the same IP. Meaning let's say you have 30 kids from the same college campus searching, to you it would look like one person searching 30 times. A session id is unique for each person, you could also use cookies to keep track after they close the browser but then you run into the problem of public computers used by more than one person. I would recommend sessions try something like
<?php
session_start();
$_SESSION['id'] = session_id();
echo($_SESSION['id']);
make sure on every page of your site you put that code in (except the echo that's just to show you what it looks like), probably wouldn't hurt to put it in a function and just call that function. Then you can save stuff to their session id and when they close the browser just drop the information. Just an FYI you could save all of their browsing data in session variables so when they close the browser you could destroy them.

username

9:54 pm on Oct 8, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, thanks for the replies.

I have actaully trialled a cookie based solution, which is highly innacurate so far, and thought that the IP solution would have been the go. Considering your advice, my issue with a Session based solution would be that for each person who accesses the site (remembering that they are not logged in), would be given a session id for a particular session, but upon returning to the same article at a later date after closing the browser, would register a second page view on that page with a new id, unless I can store a reference to their session id in a cookie or something, which I would not be favourable of.

Unless I am missing something, is this the most accurate solution possible, because I am trying to get something very accurate.

Your help would be appreciated. Thanks.

username

8:59 am on Oct 13, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Ok, so after further refinement, I went with a Session based solution. I stored and array of page id in a session variable, which allows checking and an easy path to updating the page view count. I would definitely recommend this method, it has worked out well, with a minimal amount of code.

penders

12:06 pm on Oct 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



From your above posts, I assume you are only wanting to record a page view of any particular page when a user visits that page for the very first time? Returning visits, even after some considerable time, are not counted.

Just curious... with your chosen method (using a Session), how are you achieving this? I would have thought you would require some kind of persistent cookie in order to remember the session/user?