tpitale-mongolytics 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,102 @@
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 custom 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
+ Later, retrieve stats via (given UsersController is our controller):
48
+
49
+ # by path, useful for getting per-id stats e.g., /users/1
50
+ Mongolytics.stats_for_path user_path(@user) # using the path helper (not url helper)
51
+
52
+ # by controller and action keys, useful if you want all views of users/show page
53
+ Mongolytics.stats_for_keys :users, :show # using the controller and action
54
+
55
+ # by controller/action and session key/value hash
56
+ Mongolytics.stats_for_keys :users, :show, :user_id => 1 # note: session keys, not request params
57
+
58
+ You can always query the Statistics:
59
+
60
+ Mongolytics::Statistic.count({:user_id => 1})
61
+
62
+ ## Motivation
63
+
64
+ ### Why use MongoDB?
65
+
66
+ It's cool, I wanted to learn it, it's unbelievably fast, and they're working
67
+ on a restful API which might do cool things (pooled analytics tracking)
68
+
69
+ ### This is lame!
70
+
71
+ I know I'm only taking hit counts, and not really doing much to track unique
72
+ views, or user tracking, or determining user environments. The reason being:
73
+ that's not what I wanted to do!
74
+
75
+ If you want that stuff, and great reporting, and cool graphs: use Google Analytics.
76
+ It's perfect for that stuff! I just wanted counts on certain pages that weren't
77
+ trackable by GA because of their lack of unique url, or view (e.g., create/update/destroy).
78
+
79
+ ## License
80
+
81
+ Copyright (c) 2009 Tony Pitale
82
+
83
+ Permission is hereby granted, free of charge, to any person
84
+ obtaining a copy of this software and associated documentation
85
+ files (the "Software"), to deal in the Software without
86
+ restriction, including without limitation the rights to use,
87
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
88
+ copies of the Software, and to permit persons to whom the
89
+ Software is furnished to do so, subject to the following
90
+ conditions:
91
+
92
+ The above copyright notice and this permission notice shall be
93
+ included in all copies or substantial portions of the Software.
94
+
95
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
97
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
98
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
99
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
100
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
101
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
102
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -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,14 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'mongolytics/statistic'
4
+ require 'mongolytics/tracker'
5
+
6
+ module Mongolytics
7
+ def self.stats_for_path(path)
8
+ Statistic.stats_for_path(path)
9
+ end
10
+
11
+ def self.stats_for_keys(controller, action, session_key_hash = {})
12
+ Statistic.stats_for_keys(controller, action, session_key_hash)
13
+ end
14
+ end
@@ -0,0 +1,23 @@
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
+ BASE_KEYS = ["updated_at", "action", "_id", "controller", "path", "created_at"]
10
+
11
+ def self.session_keys
12
+ keys.map{|k,v| k} - BASE_KEYS
13
+ end
14
+
15
+ def self.stats_for_path(path)
16
+ count({:path => path})
17
+ end
18
+
19
+ def self.stats_for_keys(controller, action, session_key_hash = {})
20
+ count({:controller => controller.to_s, :action => action.to_s}.merge(session_key_hash))
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
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
+ session_options = Statistic.session_keys.inject({}) do |hash, key|
15
+ hash[key.to_sym] = session[key.to_sym] if session.has_key?(key.to_sym)
16
+ hash
17
+ end
18
+
19
+ Statistic.create(options.merge(session_options))
20
+ end
21
+
22
+ module ClassMethods
23
+ def track_stats_for(*actions)
24
+ after_filter :track_stat, :only => actions
25
+ end
26
+
27
+ def track_all_stats
28
+ after_filter :track_stat
29
+ end
30
+
31
+ def track_view_stats
32
+ track_stats_for :index, :show
33
+ end
34
+
35
+ def track_change_stats
36
+ track_stats_for :create, :update, :destroy
37
+ end
38
+
39
+ def track_session_key(key, type = String)
40
+ Statistic.key(key, type)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ module Mongolytics
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
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,27 @@
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
+
16
+ should "find count of stats for a given controller/action and session key hash" do
17
+ Statistic.expects(:count).with({:controller => 'users', :action => 'show', :user_id => 1}).returns(13131)
18
+ Statistic.stats_for_keys(:users, :show, :user_id => 1).should == 13131
19
+ end
20
+
21
+ should "know of any session keys" do
22
+ Statistic.key(:extra_key, String)
23
+ assert_equal ["extra_key"], Statistic.session_keys
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,66 @@
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 Statistic" do
31
+ Statistic.expects(:key).with(:username, String)
32
+ FauxController.track_session_key :username
33
+ end
34
+
35
+ should "allow override of class type for key added to Statistic" do
36
+ Statistic.expects(:key).with(:user_id, Integer)
37
+ FauxController.track_session_key :user_id, Integer
38
+ end
39
+ end
40
+
41
+ context "An instance of a Controller with Tracker mixed-in" do
42
+ should "create a statistic with controller, action, and path" do
43
+ controller = FauxController.new
44
+
45
+ controller.stubs(:request).returns(stub(:path => '/users/1'))
46
+ controller.stubs(:params).returns({:controller => 'faux', :action => 'show'})
47
+ controller.stubs(:session).returns({})
48
+
49
+ Statistic.expects(:create).with(:controller => 'faux', :action => 'show', :path => '/users/1').returns(true)
50
+ assert controller.track_stat
51
+ end
52
+
53
+ should "create a statistic with controller, action, path, and user_id" do
54
+ FauxController.track_session_key :user_id, Integer
55
+ controller = FauxController.new
56
+
57
+ controller.stubs(:request).returns(stub(:path => '/users/1'))
58
+ controller.stubs(:params).returns({:controller => 'faux', :action => 'show'})
59
+ controller.stubs(:session).returns({:user_id => 1})
60
+
61
+ Statistic.expects(:create).with(:controller => 'faux', :action => 'show', :path => '/users/1', :user_id => 1).returns(true)
62
+ assert controller.track_stat
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,20 @@
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
+
15
+ should "delegate stats_for_keys with session options to Statistic" do
16
+ Mongolytics::Statistic.expects(:stats_for_keys).with(:users, :show, :user_id => 1)
17
+ Mongolytics.stats_for_keys(:users, :show, :user_id => 1)
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpitale-mongolytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tony Pitale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-12 00:00:00 -07: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
37
+ - lib/mongolytics/statistic.rb
38
+ - lib/mongolytics/tracker.rb
39
+ - lib/mongolytics/version.rb
40
+ - lib/mongolytics.rb
41
+ - test/test_helper.rb
42
+ - test/unit
43
+ - test/unit/mongolytics
44
+ - test/unit/mongolytics/statistic_test.rb
45
+ - test/unit/mongolytics/tracker_test.rb
46
+ - test/unit/mongolytics_test.rb
47
+ has_rdoc: false
48
+ homepage: http://t.pitale.com
49
+ licenses:
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
+