Wednesday, February 9, 2011

Best way to track page views

I'm working on a e-commerce system and I would like to track the product page views, but I'm not sure of the best way to do that... My boss suggested that I should count one view for the first time a user opens a product page and then store a cookie saying it was already viewed, but what if the user doesn't accept cookies? Another option would be to store the user's IP address, but what if a lot of different users in the same company opens the product pages? Just one view would be counted?

Any suggestions?

thanks!

  • Is this something you want to build into your application, or could you achieve your goal by simply using one of the free web log statistics tools? (Such as AWStats)

    There is a round-up of free tools here.

  • It sounds like you just want to know if a user has viewed this product already. For a non-critical application like this, cookies is the right answer.

    If you want to know which products are the most popular, use Google Analytics. For the absolute most information and control, implement an Intercepting Filter using the JavaEE technologies.

  • Cookies are a good way. If you want to make sure you can track any user for sure, even those not using cookies, try a session IDs. You need a database for that. Each time a new user enters your page, you generate a session ID, a random number (a laaaarge one) and one that is not found in the list of active session IDs. For every link on your page, you append this ID, so

    http://example.net/somepage.html
    

    becomes

    http://example.net/somepage.html?session_id=395993483829453949
    

    that means if the user clicks on any link on the page, all links have now a session ID. Thus whenever a user requests a page and has no session_id at the end of the URL, he's a new one (count him), if he has one and this ID is not in the database, count him and give all links on the page a new session_id, if he has one and the id is in the database, don't count him and make sure all links on the page he just requested contain the same session_id again.

    The database needs to store one table row per session, containing the session ID and a date/timestamp. When you create a new id, the timestamp is the date when this ID was created (NOW), otherwise whenever a user requests a page, the timestamp is updated again to the current time. So for as long as the user browses your page, the timestamp will never get older than a couple of minutes, maybe up to one or two hours.

    Now you only need a background task that runs every 30 minutes, finds ever session ID being older than, let's say 4 hours and deletes it. That way session IDs will expire and the same user, who has maybe bookmarked a page with the session ID in the URL might come back one day later, but his SID has expired and thus he's counted as a new visit.

    However, I guess that is overkill unless you run some really complicated site that really demands such a thing. Otherwise Google Analytics will do just fine for your task.

    Albert : so the random numbers don't look quite as huge and unfriendly, you can make them base 16. or base 64?
    From Mecki

0 comments:

Post a Comment