Skip to content
twoixter edited this page Feb 5, 2012 · 4 revisions

The information on this page is somewhat deprecated since the introduction of TZ (Timezones) in 3.0. However, it’s basically correct. I will update this page soon to include the TZ features.

You can use Trackoid in your existing Mongoid models by including module Mongoid::Tracking. Then, you can use track to define a tracking field.

Trackoid tracks events with a minimum resolution of days. That’s is, you can not track events by hour or minutes, only days. I think this is enough for most tracking needs.

Since the introduction of Time Zones in version 3, the standard resolution is now hours. Also, the days resolution is somewhat “floating”, meaning that there is a different “today” for users in different timezones.

Example including a “visits” tracking field


class BlogPost
  include Mongoid::Document
  include Mongoid::Tracking   # You need to include "Mongoid::Tracking" module

  field :title
  field :body

  track :visits   # Defining a 'visits' field for tracking
end

From now on you can use visits field to track and display analytics information.

Updating analytics

You can use inc, dec, add(n) and set(n) methods for updating tracking information.

Example of using tracking methods


# You can use the methods without parameters

@post.visits.inc    # Increments visits for today by one.
@post.visits.dec    # Decrements visits for today by one.
@port.visits.add    # Adds one visit for today (Add parameter defaults to 1)

# You can also use a parameter for date

@post.visits.inc( Date.parse("2010-05-01") )
@post.visits.inc( Date.today - 1 )

Retrieving information

You can use today, yesterday, last_days and on methods for retrieving information. All those methods returns a Number or an Array depending on how many events they are expecting to return. For example, today always returns a number and @last_days@ always returns an Array, even when the number of days is 1.


@post.visits.today    # Returns the number of visits for today (Number)

@post.visits.last_days(7)   # Returns an array of 7 numbers.

@post.visits.on("2010-06-01")   # Returns visits on the specified date

# The 'on' method also can be used on ranges
range = Date.today-5 ... Date.today
@post.visits.on(range)    # Returns an array of visits for the range

Clone this wiki locally