SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to log page views using Javascript (like Mint)?

    Hi,

    I'm working on a project where people will be charged to post content on a website. I'd like to offer users the ability to track how popular their content is, but I don't want to log every page hit using PHP because that will also count visits from crawlers, etc.

    I'm considering using Javascript to call a PHP file which should eliminate the crawler problem. I know I won't be able to track users with Javascript disabled, but thatís OK.

    Shaun Inman has done something similar (and a lot more complex) with Mint, but I'm not really sure how it works.

    I'm sure this is really simple, but I want to get it right. Any thoughts?

  2. #2
    ¨.¨ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    He uses JavaScript to collect the information then outputs an image like:

    track/image.php?var=[data_from_js]

    And then the PHP just records that.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.

    So, if I understand correctly, I could pass the ID of the content (e.g. x, where the URL of the content is example.com/content/x) to a Javascript function and that would then output some HTML like <img src="http://example.com/process.php?id=x" width="0" height="0" />?

    Is that correct?

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to play around with session and cookie, in the case you need to track unique visit and duration

  5. #5
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point, although at this stage the total number of views should suffice.

  6. #6
    SitePoint Member
    Join Date
    Jun 2004
    Location
    UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, based on the example I gave earlier, this seems to work:

    Code:
    window.onload = function()
    {
    
        var url = window.location.href;
    
        var pattern = /content\/(\d+)$/;
    
        var match = pattern.exec(url);
    
        var path = 'http://example.com/process.php?id=' + match[1];
    
        var image = new Image();
    
        image.src = path;
    
    }
    Is this the correct/best way to do it?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •