Home

twoixter edited this page Sep 14, 2010 · 3 revisions

Welcome to the Trackoid Wiki. Here you will find information about the project and helpful information for installing and configuring this little beast. :-) Use the following table of contents or navigate using links on the right column.

Background

Handling analytics data is one of the most difficult things on web applications, believe it or not. Sure you can come up with simple statistical analytics like page views and download counts, but when scalability is a consideration you will run into design problems. First of all is the underlying database structure. With SQL, It’s easy to make a simple table-like analytics, and I bet you at least once did one of such table schema.

Then, they come aggregations… Ok, let’s track page views, but I also want to track page views with aggregated browser data, and also referrer data, and also time of day data, and also… It could become a nightmare.

You then have two options: either use Google Analytics or Trackoid… :-)

Ok, let’s assume I’m not pretending this to be better than Google Analytics. Read the Theory of Operation to move on.

Enough blah, blah, let’s get our hands dirty

Let’s pretend you have a blog system with Page models and a Page controller:

The Page model

Below is the definition of a fairly standard Page model. Note the inclusion of Mongoid::Tracking. You need to include this module to use Trackoid methods.

class Page
    include Mongoid::Document
    include Mongoid::Tracking
    field :name
    track :visits   # Adds a "visits" tracking field
end

This is the most basic usage of Trackoid. By using track :visits we are adding a new special field called visits which is a Trackoid tracking field.

Tracking visits

Now, in your controllers or anywhere in your code you can count visits like this:

@page = Page.where( ... selection ... )
@page.visits.inc

That’s all… You are now tracking statistical information. Note that visits is a real field on the model, so you can track visits anywhere you have a page instance:

Page.where(:url => "/").first.visits.inc

Retrieving information

This is the fun part… Retrieving the information is not only easy, but fun… Consider this examples:

# Retrieve visits for today
@page.visits.today    # Returns a number. (Fixnum)
# Retrieve visits for yesterday
@page.visits.yesterday   # Returns a number.
# Retrieve visits in last 7 days
@page.visits.last_days(7)   # Returns an Array. Example: => [12, 5, 45, 15, 20, 30, 25]
# Retrieve visits in a specific date
@page.visits.on("2010-06-06")    # Returns a number, like in "today"
# The 'on' selector also accepts an object descendant from Date
@page.visits.on( Date.today - 1)   # Equivalent to "yesterday"