Monday, April 02, 2007

click_track plugin

Quick Start

This quick start guide will get you up and running with the click_track plugin in just a few minutes.
Installing the Plugin

You would normally use the script/plugin utility that comes with Rails to install a plugin. However, that script assumes a repository layout that is different than the one used for the click_track plugin.

Therefore, you can export the plugin from its Subversion repository, check it out, or add it as a svn:externals.

svn export \
https://crookedhideout.com/svn/oss/click_track/branches/rel/0.1/ \
vendor/plugins/click_track

Once you have the click_track plugin installed in your vendor/plugins directory, you’ll want to generate the migration for the database changes:

script/generate click_track add_clicks
rake db:migrate

Tracking a Controller
For each controller you want tracked do this:

class FoobarController < ApplicationController
click_track
end

The click_track call takes the same arguments as a before_filter.

All actions called on this controller will be tracked. Views for this controller can now use click_track(..) in place of link_to(..) for tracking when an external link is followed.
How to Use the Data
Here is the table behind the Click model:

create_table :clicks do |t|
t.column :controller, :string, :null=>false
t.column :action, :string, :null=>false
t.column :param_id, :integer
t.column :ssl, :boolean, :null=>false
t.column :http_method, :string, :null=>false
t.column :remote_ip, :string, :null=>false
t.column :request_uri, :string, :null=>false
t.column :user_agent, :string
t.column :browser, :string
t.column :browser_version, :string
t.column :browser_platform,:string
t.column :created_at, :datetime, :null=>false
end

Here are some examples of how one might use the data. These are taken from the statistics page of the crookedhidout.com site.

class StatisticsController < ApplicationController
def index
@clicks = (Click.in_last(7.days) || [])[0..12]
@popular_clicks = (Click.bucket_simple_uri_in_last(7.days) || [])[0..8]
@fan_clicks = (Click.bucket_remote_ip_in_last(30.days) || [])[0..8]
@last_24_count = Click.total_in_last 24.hours
end
end

This is going to grow my database forever!
Every one in ActionController::ClickTrack.chance times (default is 10,000) the DB will be cleared of all clicks older then ActionController::ClickTrack.oldest_click (default is 90.days). You can change these values in your config/environment.rb file. For example:

ActionController::ClickTrack.chance = 50000
ActionController::ClickTrack.oldest_click = 40.weeks

No comments :