开发者

Pageview implementation

开发者 https://www.devze.com 2022-12-23 00:12 出处:网络
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

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?


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.


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);
 }


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消