mongolytics 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,105 @@
1
+ # Mongolytics
2
+
3
+ ## Description
4
+
5
+ Simple analytics tracking for Rails using the awesomest: MongoDB
6
+
7
+ ## Dependencies
8
+
9
+ Rails
10
+ MongoDB
11
+ MongoMapper
12
+
13
+ ## Installation
14
+
15
+ sudo gem install mongolytics
16
+
17
+ ## Setup
18
+
19
+ Just be sure to setup MongoMapper to connect to MongoDB in environment.rb:
20
+
21
+ Rails::Initializer.run do |config|
22
+ config.gem 'mongomapper', :version => '>= 0.2.1'
23
+ end
24
+
25
+ MongoMapper.database = "databasename-#{Rails.env}"
26
+
27
+ [More Information](http://railstips.org/2009/7/23/getting-started-with-mongomapper-and-rails)
28
+
29
+ ## Usage
30
+
31
+ class ApplicationController < ActionController::Base
32
+ include Mongolytics::Tracker
33
+ end
34
+
35
+ In your controllers call any of these (creates an after_filter):
36
+
37
+ track_all_stats # tracks all actions
38
+ track_view_stats # tracks index, show
39
+ track_change_stats # tracks create, update, destroy
40
+ track_stats_for :new, :edit, :show, :destroy # track any action mix, other actions
41
+
42
+ If you want to track a session variable:
43
+
44
+ track_session_key :username # tracks session[:username] as a String
45
+ track_session_key :user_id, Integer # tracks session[:user_id] as an Integer
46
+
47
+ If you want to track a params variable:
48
+
49
+ track_params_key :username # tracks params[:username] as a String
50
+ track_params_key :user_id, Integer # tracks params[:user_id] as an Integer
51
+
52
+ Later, retrieve stats via (given UsersController is our controller):
53
+
54
+ # by path, useful for getting per-id stats e.g., /users/1
55
+ Mongolytics.stats_for_path user_path(@user) # using the path helper (not url helper)
56
+
57
+ # by controller and action keys, useful if you want all views of users/show page
58
+ Mongolytics.stats_for_keys :users, :show # using the controller and action
59
+
60
+ You can always query the Statistics:
61
+
62
+ Mongolytics::Statistic.count(:session => {:user_id => 1})
63
+ Mongolytics::Statistic.count(:param => {:user_id => 1})
64
+
65
+ ## Motivation
66
+
67
+ ### Why use MongoDB?
68
+
69
+ It's cool, I wanted to learn it, it's unbelievably fast, and they're working
70
+ on a restful API which might do cool things (pooled analytics tracking)
71
+
72
+ ### This is lame!
73
+
74
+ I know I'm only taking hit counts, and not really doing much to track unique
75
+ views, or user tracking, or determining user environments. The reason being:
76
+ that's not what I wanted to do!
77
+
78
+ If you want that stuff, and great reporting, and cool graphs: use Google Analytics.
79
+ It's perfect for that stuff! I just wanted counts on certain pages that weren't
80
+ trackable by GA because of their lack of unique url, or view (e.g., create/update/destroy).
81
+
82
+ ## License
83
+
84
+ Copyright (c) 2009 Tony Pitale
85
+
86
+ Permission is hereby granted, free of charge, to any person
87
+ obtaining a copy of this software and associated documentation
88
+ files (the "Software"), to deal in the Software without
89
+ restriction, including without limitation the rights to use,
90
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
91
+ copies of the Software, and to permit persons to whom the
92
+ Software is furnished to do so, subject to the following
93
+ conditions:
94
+
95
+ The above copyright notice and this permission notice shall be
96
+ included in all copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
99
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
100
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
101
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
102
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
103
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
104
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
105
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/mongolytics/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'mongolytics'
11
+ s.version = Mongolytics::Version.to_s
12
+ s.has_rdoc = false
13
+ s.summary = "Provide basic analytics tracking, server-side, using mongodb"
14
+ s.author = 'Tony Pitale'
15
+ s.email = 'tpitale@gmail.com'
16
+ s.homepage = 'http://t.pitale.com'
17
+ s.files = %w(README.md Rakefile) + Dir.glob("{lib,test}/**/*")
18
+
19
+ s.add_dependency('mongomapper', '>= 0.3.1')
20
+ end
21
+
22
+ Rake::GemPackageTask.new(spec) do |pkg|
23
+ pkg.gem_spec = spec
24
+ end
25
+
26
+ Rake::TestTask.new do |t|
27
+ t.libs << 'test'
28
+ t.test_files = FileList["test/**/*_test.rb"]
29
+ t.verbose = true
30
+ end
31
+
32
+ desc 'Generate the gemspec to serve this Gem from Github'
33
+ task :github do
34
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
35
+ File.open(file, 'w') {|f| f << spec.to_ruby }
36
+ puts "Created gemspec: #{file}"
37
+ end
38
+
39
+ begin
40
+ require 'rcov/rcovtask'
41
+
42
+ desc "Generate RCov coverage report"
43
+ Rcov::RcovTask.new(:rcov) do |t|
44
+ t.test_files = FileList['test/**/*_test.rb']
45
+ t.rcov_opts << "-x lib/mongolytics/version.rb"
46
+ end
47
+ rescue LoadError
48
+ nil
49
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'mongolytics/statistic'
4
+ require 'mongolytics/session'
5
+ require 'mongolytics/param'
6
+ require 'mongolytics/tracker'
7
+
8
+ module Mongolytics
9
+ def self.stats_for_path(path)
10
+ Statistic.stats_for_path(path)
11
+ end
12
+
13
+ def self.stats_for_keys(controller, action)
14
+ Statistic.stats_for_keys(controller, action)
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Mongolytics
2
+ class Param
3
+ include MongoMapper::EmbeddedDocument
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Mongolytics
2
+ class Session
3
+ include MongoMapper::EmbeddedDocument
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Mongolytics
2
+ class Statistic
3
+ include MongoMapper::Document
4
+
5
+ key :controller, String, :required => true
6
+ key :action, String, :required => true
7
+ key :path, String
8
+
9
+ many :sessions, :class_name => 'Mongolytics::Session'
10
+ many :params, :class_name => 'Mongolytics::Param'
11
+
12
+ def self.stats_for_path(path)
13
+ count({:path => path})
14
+ end
15
+
16
+ def self.stats_for_keys(controller, action)
17
+ count({:controller => controller.to_s, :action => action.to_s})
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module Mongolytics
2
+ module Tracker
3
+ def self.included(controller)
4
+ controller.extend(ClassMethods)
5
+ end
6
+
7
+ def track_stat
8
+ options = {
9
+ :controller => params[:controller].to_s,
10
+ :action => params[:action].to_s,
11
+ :path => request.path
12
+ }
13
+
14
+ Statistic.create(options.merge(:sessions => [session], :params => [params]))
15
+ end
16
+
17
+ module ClassMethods
18
+ def track_stats_for(*actions)
19
+ after_filter :track_stat, :only => actions
20
+ end
21
+
22
+ def track_all_stats
23
+ after_filter :track_stat
24
+ end
25
+
26
+ def track_view_stats
27
+ track_stats_for :index, :show
28
+ end
29
+
30
+ def track_change_stats
31
+ track_stats_for :create, :update, :destroy
32
+ end
33
+
34
+ def track_session_key(key, type = String)
35
+ Session.key(key, type)
36
+ end
37
+
38
+ def track_params_key(key, type = String)
39
+ Param.key(key, type)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,12 @@
1
+ module Mongolytics
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 2
6
+ TINY = 1
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ $:.reject! { |e| e.include? 'TextMate' }
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+ require 'matchy'
7
+ require 'mocha'
8
+ require 'mongomapper'
9
+
10
+ require File.dirname(__FILE__) + '/../lib/mongolytics'
11
+
12
+ # connect to mongodb?
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module Mongolytics
4
+ class StatisticTest < Test::Unit::TestCase
5
+ context 'The Statistic class' do
6
+ should "find count of stats for a given path" do
7
+ Statistic.expects(:count).with({:path => '/users/1'}).returns(10201)
8
+ Statistic.stats_for_path('/users/1').should == 10201
9
+ end
10
+
11
+ should "find count of stats for a given controller and action" do
12
+ Statistic.expects(:count).with({:controller => 'users', :action => 'show'}).returns(11211)
13
+ Statistic.stats_for_keys(:users, :show).should == 11211
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class FauxController
4
+ include Mongolytics::Tracker
5
+ end
6
+
7
+ module Mongolytics
8
+ class TrackerTest < Test::Unit::TestCase
9
+ context "A Controller with Tracker mixed-in" do
10
+ should "create an after_filter for the specified actions" do
11
+ FauxController.expects(:after_filter).with(:track_stat, :only => [:new, :edit])
12
+ FauxController.track_stats_for :new, :edit
13
+ end
14
+
15
+ should "create an after_filter for all actions" do
16
+ FauxController.expects(:after_filter).with(:track_stat)
17
+ FauxController.track_all_stats
18
+ end
19
+
20
+ should "create an after_filter for index, show" do
21
+ FauxController.expects(:after_filter).with(:track_stat, :only => [:index, :show])
22
+ FauxController.track_view_stats
23
+ end
24
+
25
+ should "create an after_filter for create, update, destroy" do
26
+ FauxController.expects(:after_filter).with(:track_stat, :only => [:create, :update, :destroy])
27
+ FauxController.track_change_stats
28
+ end
29
+
30
+ should "add a session key to Session" do
31
+ Session.expects(:key).with(:username, String)
32
+ FauxController.track_session_key :username
33
+ end
34
+
35
+ should "allow override of class type for a session key added to Session" do
36
+ Session.expects(:key).with(:user_id, Integer)
37
+ FauxController.track_session_key :user_id, Integer
38
+ end
39
+
40
+ should "add a params key to Session" do
41
+ Param.expects(:key).with(:username, String)
42
+ FauxController.track_params_key :username
43
+ end
44
+
45
+ should "allow override of class type for a params key added to Session" do
46
+ Param.expects(:key).with(:user_id, Integer)
47
+ FauxController.track_params_key :user_id, Integer
48
+ end
49
+ end
50
+
51
+ context "An instance of a Controller with Tracker mixed-in" do
52
+ should "create a statistic with controller, action, and path" do
53
+ controller = FauxController.new
54
+
55
+ params = {:controller => 'faux', :action => 'show'}
56
+ session = {}
57
+
58
+ controller.stubs(:request).returns(stub(:path => '/users/1'))
59
+ controller.stubs(:params).returns(params)
60
+ controller.stubs(:session).returns(session)
61
+
62
+ Statistic.expects(:create).with({
63
+ :controller => 'faux',
64
+ :action => 'show',
65
+ :path => '/users/1',
66
+ :params => [params],
67
+ :sessions => [session]
68
+ }).returns(true)
69
+
70
+ assert controller.track_stat
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class MongolyticsTest < Test::Unit::TestCase
4
+ context "The Mongolytics module" do
5
+ should "delegate stats_for_path to Statistic" do
6
+ Mongolytics::Statistic.expects(:stats_for_path).with('/users/1')
7
+ Mongolytics.stats_for_path('/users/1')
8
+ end
9
+
10
+ should "delegate stats_for_keys to Statistic" do
11
+ Mongolytics::Statistic.expects(:stats_for_keys).with(:users, :show)
12
+ Mongolytics.stats_for_keys(:users, :show)
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongolytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Tony Pitale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-29 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mongomapper
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ description:
26
+ email: tpitale@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/mongolytics/param.rb
37
+ - lib/mongolytics/session.rb
38
+ - lib/mongolytics/statistic.rb
39
+ - lib/mongolytics/tracker.rb
40
+ - lib/mongolytics/version.rb
41
+ - lib/mongolytics.rb
42
+ - test/test_helper.rb
43
+ - test/unit/mongolytics/statistic_test.rb
44
+ - test/unit/mongolytics/tracker_test.rb
45
+ - test/unit/mongolytics_test.rb
46
+ has_rdoc: true
47
+ homepage: http://t.pitale.com
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Provide basic analytics tracking, server-side, using mongodb
74
+ test_files: []
75
+