Pageview Tracking

Pageview tracking forms the basis for a lot of web analytics, and collecting good pageview information gives you lots of opportunities to answer interesting questions.

Here is a good data model to base your pageview collection on:

{
    "url" : {
        "source" : "https://www.example.com/some/page?param1=foo&param2=bar/#someplace",
        "protocol" : "https://",
        "domain" : "www.example.com",
        "port" : null,
        "path" : "/some/page",
        "anchor" : "someplace"
    },
    "user_agent" : {
        "browser" : {
            "name" : "Chrome",
            "version" : "28.0.1500.95",
            "major" : 28
        },
        "engine" : {
            "name" : "WebKit",
            "version" : "537.36"
        },
        "os" : {
            "name" : "Mac OS X",
            "version" : "10.7.4"
        }
    },
    "referrer": {
          "source" : "https://www.referrer.com/some/ad?param1=foo&param2=bar/#someplace",
          "protocol" : "https://",
          "domain" : "www.referrer.com",
          "port" : null,
          "path" : "/some/ad",
          "anchor" : "someplace"
      },
    "session_id" : "randomly_generated_key",
    "permanent_tracker" : "randomly_generated_key",
    "user" : {
        "id" : "user_id_goes_here",
        "sign_up_date" : "2013-04-28",
        "more_user_properties" : "..."
    }
}

Parsing the URL and User-Agent into its components is very helpful for easy segmentation and analytics later on; the same goes for generating unique IDs for your current session, as well as a permanent id.

Here are some libraries that will do the heavy lifting for you:

The unique IDs should be kept in two separate cookies:

  1. The session cookie, which expires at whatever your definition of a session is. A common time is 25 minutes. Every time a page is viewed, you should reset the expiration time to be 25 minutes in the future.
  2. The second ID should be stored in a permanent cookie. This way you can track users from the time they first land on your site until they complete your signup process and become registered users.