views:

66

answers:

4

Hi everyone

I want to add a pageview feature on my current web application. This page view is based on the count of user viewing the page. It must be unique, i.e. I must not view a person's page 10000 times and record it as 10000 views, just record 1 view instead.

My question is, should I base my pageview count on IP address? If not, what is/are the best approach in doing this?

I know that if the person has logged in to my system, I can simply use the user id stored in the session and check on the record if the user has/hasn't viewed the page and update accordingly. But for "anonymous" viewers, what is the best approach?

Thanks.

PS How does Youtube does it?


After reading most of the comment here, I'm still not sure if the solution provided will help. Take a typical example: Youtube videos. They store pageviews in a persistent storage and they make sure that it doesn't record the same user twice. If there are anonymous viewers, it (somehow) makes sure that (I know it's not full proof, or is it) the anonymous viewer updates the pageview counts once. It can do it via cookie (but what if you delete it) or via IP address (but that won't help if you're sitting behind company firewall). Is there other strategy that can best help with this?

PS Especially for user pageview (i.e. your youtube account), it can tell how many viewers who viewed your profile. Does that have another approach to getting pageview?

+1  A: 

You could use a cookie to identify anon users. Some will disable cookies or delete them, but it will give you a better result and is quite cheap to implement.

The problem with IP based filtering is that a lot of people are behind firewalls and you may fail to count hits for multiple users behind a single firewall.

Kris
+1  A: 

You can create filter, map it to "/*". In this filter you can store information about user in session:

 HttpSession session = request.getSession(true);

 Integer ival = (Integer) session.getValue("knownuser");
 if (ival==null) {
     ival = new Integer(1);
     incrementUsersCounter ();
     session.putValue("knownuser", ival);
 }
Roman
Ok, and for anonymous users?
The Elite Gentleman
@The Elite Gentleman: session will be created regardless of whether the user is logged in or not. It's just cookies (or URL modification).
Roman
But for anonymous viewers, how can I determine if the viewer is unique and not flooding requests to increase viewer count?
The Elite Gentleman
We are discussing this about an hour already. You'd better read some intro manuals about servlets, sessions (I mean HttpSession in java), filters. After that you'll know the answer for sure.
Roman
+1  A: 

Maybe you could start looking into a totally different direction first: In case you're already using Google Analytics (or similar), you could use it's API to fetch page view data for the current page. The data won't be completely up to date, but the implementation is extremely simple and might already do what you need.

sfussenegger
A: 

JAVA SERVLET api offers us on this matter sessionCreated(HttpSessionEvent se) and sessionDestroyed(HttpSessionEvent se). These method will be called as a notification that a new session was created and the session was about to be destroyed respectively. Lets have servlet code as below

03.import javax.servlet.http.HttpSessionEvent;
04.import javax.servlet.http.HttpSessionListener;
05.import javax.servlet.http.HttpSession;
06.import java.util.List;
07.import java.util.ArrayList;
08. 
09.public class SessionCounter implements HttpSessionListener
10.{
11.    private List sessions = new ArrayList();
12. 
13.    public SessionCounter()
14.    {
15.    }
16. 
17.    public void sessionCreated(HttpSessionEvent event)
18.    {
19.        HttpSession session = event.getSession();
20.        sessions.add(session.getId());
21. 
22.        session.setAttribute("counter", this);
23.    }
24. 
25.    public void sessionDestroyed(HttpSessionEvent event)
26.    {
27.        HttpSession session = event.getSession();
28.        sessions.remove(session.getId());
29. 
30.        session.setAttribute("counter", this);
31.    }
32. 
33.    public int getActiveSessionNumber()
34.    {
35.        return sessions.size();
36.    }
37.}

to display number of users we will have a JSP page as follows

<html>
<head>
    <title>Session Counter</title>
</head>

<body>
<%
    SessionCounter counter = (SessionCounter) session
            .getAttribute("counter");
%>
Number of online user(s): <%= counter.getActiveSessionNumber() %>
</body>
</html>

And setting up web.xml file I hope it works

giri
Is that a good idea to keep all the Session in a List ? I think this may not be ideal? We could just have a count instead of the whole list.
Calm Storm
I agree Calm Storm. @Giri, what you did I've already have this done months ago. It's called session counter (in my case). Instead of holding a list, I have a `static long` variable that gets increased/decreased by 1 when a session is created/destroyed respectively.
The Elite Gentleman