mitamirri 0.13.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/.document +5 -0
  2. data/.gitignore +26 -0
  3. data/README.rdoc +117 -0
  4. data/Rakefile +26 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/application_controller.rb +10 -0
  7. data/app/controllers/trackable_actions_controller.rb +10 -0
  8. data/app/controllers/trackable_sessions_controller.rb +100 -0
  9. data/app/helpers/application_helper.rb +2 -0
  10. data/app/models/trackable_action.rb +193 -0
  11. data/app/models/trackable_location.rb +19 -0
  12. data/app/models/trackable_session.rb +352 -0
  13. data/app/models/trackable_stat.rb +64 -0
  14. data/app/views/layouts/application.html.erb +20 -0
  15. data/app/views/trackable_sessions/_keywords_graph.html.erb +24 -0
  16. data/app/views/trackable_sessions/_print_buttons.html.erb +3 -0
  17. data/app/views/trackable_sessions/_sessions.html.erb +26 -0
  18. data/app/views/trackable_sessions/_top_referrers.html.erb +24 -0
  19. data/app/views/trackable_sessions/_traffic_summary_all.html.erb +64 -0
  20. data/app/views/trackable_sessions/_traffic_summary_by_kind.html.erb +48 -0
  21. data/app/views/trackable_sessions/_visits_graph.html.erb +25 -0
  22. data/app/views/trackable_sessions/content.html.erb +119 -0
  23. data/app/views/trackable_sessions/explorer.html.erb +67 -0
  24. data/app/views/trackable_sessions/export.xls.erb +5 -0
  25. data/app/views/trackable_sessions/index.html.erb +47 -0
  26. data/app/views/trackable_sessions/intersite.html.erb +97 -0
  27. data/app/views/trackable_sessions/leads.html.erb +98 -0
  28. data/app/views/trackable_sessions/show.html.erb +113 -0
  29. data/app/views/trackable_sessions/visitor_profile.html.erb +114 -0
  30. data/config/boot.rb +110 -0
  31. data/config/database.yml +0 -0
  32. data/config/environment.rb +24 -0
  33. data/config/environments/development.rb +17 -0
  34. data/config/environments/production.rb +28 -0
  35. data/config/environments/test.rb +31 -0
  36. data/config/initializers/backtrace_silencers.rb +7 -0
  37. data/config/initializers/database.rb +1 -0
  38. data/config/initializers/inflections.rb +10 -0
  39. data/config/initializers/mime_types.rb +5 -0
  40. data/config/initializers/new_rails_defaults.rb +21 -0
  41. data/config/initializers/session_store.rb +15 -0
  42. data/config/locales/en.yml +5 -0
  43. data/config/routes.rb +11 -0
  44. data/db/development.sqlite3 +0 -0
  45. data/db/migrate/20100810173533_create_trackable_sessions.rb +23 -0
  46. data/db/migrate/20100810173605_create_trackable_actions.rb +20 -0
  47. data/db/test.sqlite3 +1 -0
  48. data/doc/README_FOR_APP +2 -0
  49. data/init.rb +8 -0
  50. data/lib/mitamirri/content_report.rb +75 -0
  51. data/lib/mitamirri/helper.rb +106 -0
  52. data/lib/mitamirri/intersite_traffic_report.rb +122 -0
  53. data/lib/mitamirri/leads_report.rb +90 -0
  54. data/lib/mitamirri/session_report.rb +219 -0
  55. data/lib/mitamirri/stat_report.rb +216 -0
  56. data/lib/mitamirri/tasks.rb +27 -0
  57. data/lib/mitamirri/visitor_profile_report.rb +127 -0
  58. data/lib/mitamirri.rb +15 -0
  59. data/log/development.log +0 -0
  60. data/log/production.log +0 -0
  61. data/log/server.log +0 -0
  62. data/log/test.log +0 -0
  63. data/mitamirri.gemspec +125 -0
  64. data/public/stylesheets/mitamirri.css +736 -0
  65. data/script/about +4 -0
  66. data/script/console +3 -0
  67. data/script/dbconsole +3 -0
  68. data/script/destroy +3 -0
  69. data/script/generate +3 -0
  70. data/script/performance/benchmarker +3 -0
  71. data/script/performance/profiler +3 -0
  72. data/script/plugin +3 -0
  73. data/script/runner +3 -0
  74. data/script/server +3 -0
  75. data/spec/controllers/trackable_actions_controller_spec.rb +5 -0
  76. data/spec/controllers/trackable_sessions_controller_spec.rb +5 -0
  77. data/spec/custom_matchers.rb +23 -0
  78. data/spec/helpers/application_helper_spec.rb +57 -0
  79. data/spec/lib/content_report_spec.rb +37 -0
  80. data/spec/lib/intersite_traffic_report_spec.rb +51 -0
  81. data/spec/lib/leads_report_spec.rb +46 -0
  82. data/spec/lib/session_report_spec.rb +107 -0
  83. data/spec/lib/stat_report_spec.rb +106 -0
  84. data/spec/models/trackable_action_spec.rb +42 -0
  85. data/spec/models/trackable_session_spec.rb +154 -0
  86. data/spec/rcov.opts +4 -0
  87. data/spec/schema.rb +34 -0
  88. data/spec/spec.opts +4 -0
  89. data/spec/spec_helper.rb +32 -0
  90. metadata +155 -0
data/config/boot.rb ADDED
@@ -0,0 +1,110 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ Rails::GemDependency.add_frozen_gem_path
48
+ end
49
+ end
50
+
51
+ class GemBoot < Boot
52
+ def load_initializer
53
+ self.class.load_rubygems
54
+ load_rails_gem
55
+ require 'initializer'
56
+ end
57
+
58
+ def load_rails_gem
59
+ if version = self.class.gem_version
60
+ gem 'rails', version
61
+ else
62
+ gem 'rails'
63
+ end
64
+ rescue Gem::LoadError => load_error
65
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
66
+ exit 1
67
+ end
68
+
69
+ class << self
70
+ def rubygems_version
71
+ Gem::RubyGemsVersion rescue nil
72
+ end
73
+
74
+ def gem_version
75
+ if defined? RAILS_GEM_VERSION
76
+ RAILS_GEM_VERSION
77
+ elsif ENV.include?('RAILS_GEM_VERSION')
78
+ ENV['RAILS_GEM_VERSION']
79
+ else
80
+ parse_gem_version(read_environment_rb)
81
+ end
82
+ end
83
+
84
+ def load_rubygems
85
+ min_version = '1.3.2'
86
+ require 'rubygems'
87
+ unless rubygems_version >= min_version
88
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
89
+ exit 1
90
+ end
91
+
92
+ rescue LoadError
93
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
94
+ exit 1
95
+ end
96
+
97
+ def parse_gem_version(text)
98
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
99
+ end
100
+
101
+ private
102
+ def read_environment_rb
103
+ File.read("#{RAILS_ROOT}/config/environment.rb")
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ # All that for this:
110
+ Rails.boot!
File without changes
@@ -0,0 +1,24 @@
1
+ # Be sure to restart your server when you modify this file
2
+
3
+ # Specifies gem version of Rails to use when vendor/rails is not present
4
+ # RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
5
+
6
+ # Bootstrap the Rails environment, frameworks, and default configuration
7
+ require File.join(File.dirname(__FILE__), 'boot')
8
+
9
+ Rails::Initializer.run do |config|
10
+ config.frameworks -= [:active_record]
11
+ config.gem 'mitamirri'
12
+ config.gem 'mongo_mapper'
13
+ config.time_zone = 'Central Time (US & Canada)'
14
+ config.action_controller.session = {
15
+ :session_key => '_seologic_session',
16
+ :secret => 'be43561c9a1980a7230fd9530aed9f4d1a29f780cd80afc9091180d01d17980308d583f59d3c8d6832c26d92d1c6d5ebc21b9d5565d1e04136b198882d01f303'
17
+ }
18
+ # config.action_controller.session_store = :active_record_store
19
+ end
20
+
21
+ if RAILS_ENV == 'test'
22
+ require 'spec/spec_helper'
23
+ # require 'spec/blueprints'
24
+ end
@@ -0,0 +1,17 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # In the development environment your application's code is reloaded on
4
+ # every request. This slows down response time but is perfect for development
5
+ # since you don't have to restart the webserver when you make code changes.
6
+ config.cache_classes = false
7
+
8
+ # Log error messages when you accidentally call methods on nil.
9
+ config.whiny_nils = true
10
+
11
+ # Show full error reports and disable caching
12
+ config.action_controller.consider_all_requests_local = true
13
+ config.action_view.debug_rjs = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
@@ -0,0 +1,28 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The production environment is meant for finished, "live" apps.
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.action_controller.consider_all_requests_local = true # FIXME: false causes weird errors in the CMS
9
+ config.action_controller.perform_caching = true
10
+ config.action_view.cache_template_loading = true
11
+
12
+ # See everything in the log (default is :info)
13
+ # config.log_level = :debug
14
+
15
+ # Use a different logger for distributed setups
16
+ # config.logger = SyslogLogger.new
17
+
18
+ # Use a different cache store in production
19
+ # config.cache_store = :mem_cache_store
20
+
21
+ # Enable serving of images, stylesheets, and javascripts from an asset server
22
+ # config.action_controller.asset_host = "http://assets.example.com"
23
+
24
+ # Disable delivery errors, bad email addresses will be ignored
25
+ # config.action_mailer.raise_delivery_errors = false
26
+
27
+ # Enable threaded mode
28
+ # config.threadsafe!
@@ -0,0 +1,31 @@
1
+ # Settings specified here will take precedence over those in config/environment.rb
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+ config.cache_classes = true
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.action_controller.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+ config.action_view.cache_template_loading = true
16
+
17
+ # Disable request forgery protection in test environment
18
+ config.action_controller.allow_forgery_protection = false
19
+
20
+ # Tell Action Mailer not to deliver emails to the real world.
21
+ # The :test delivery method accumulates sent emails in the
22
+ # ActionMailer::Base.deliveries array.
23
+ config.action_mailer.delivery_method = :test
24
+
25
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
26
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
27
+ # like if you have constraints or database-specific column types
28
+ # config.active_record.schema_format = :sql
29
+
30
+ config.gem 'notahat-machinist', :lib => 'machinist'
31
+ config.gem 'faker'
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
7
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1 @@
1
+ MongoMapper.database = "mitamirri_#{Rails.env}_#{Time.zone.now.to_i}"
@@ -0,0 +1,10 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new inflection rules using the following format
4
+ # (all these examples are active by default):
5
+ # ActiveSupport::Inflector.inflections do |inflect|
6
+ # inflect.plural /^(ox)$/i, '\1en'
7
+ # inflect.singular /^(ox)en/i, '\1'
8
+ # inflect.irregular 'person', 'people'
9
+ # inflect.uncountable %w( fish sheep )
10
+ # end
@@ -0,0 +1,5 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
5
+ # Mime::Type.register_alias "text/html", :iphone
@@ -0,0 +1,21 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # These settings change the behavior of Rails 2 apps and will be defaults
4
+ # for Rails 3. You can remove this initializer when Rails 3 is released.
5
+
6
+ if defined?(ActiveRecord)
7
+ # Include Active Record class name as root for JSON serialized output.
8
+ ActiveRecord::Base.include_root_in_json = true
9
+
10
+ # Store the full class name (including module namespace) in STI type column.
11
+ ActiveRecord::Base.store_full_sti_class = true
12
+ end
13
+
14
+ ActionController::Routing.generate_best_match = false
15
+
16
+ # Use ISO 8601 format for JSON serialized times and dates.
17
+ ActiveSupport.use_standard_json_time_format = true
18
+
19
+ # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
20
+ # if you're including raw json in an HTML page.
21
+ ActiveSupport.escape_html_entities_in_json = false
@@ -0,0 +1,15 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying cookie session data integrity.
4
+ # If you change this key, all old sessions will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ ActionController::Base.session = {
8
+ :key => '_sample_session',
9
+ :secret => 'e56cda156307f98e3f1e3903f92158510cbc2b55956026f2aaff5cba3604db72bad97d4002e9198f8015ed1e0ced4356857a40ef04426ba01865b424beddb921'
10
+ }
11
+
12
+ # Use the database for sessions instead of the cookie-based default,
13
+ # which shouldn't be used to store highly confidential information
14
+ # (create the session table with "rake db:sessions:create")
15
+ # ActionController::Base.session_store = :active_record_store
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
data/config/routes.rb ADDED
@@ -0,0 +1,11 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.resources :trackable_actions
3
+ map.resources :trackable_sessions
4
+ map.tracking 'tracking', :controller => 'trackable_sessions'
5
+ map.explorer 'tracking/explorer', :controller => 'trackable_sessions', :action => 'explorer'
6
+ map.leads 'tracking/leads', :controller => 'trackable_sessions', :action => 'leads'
7
+ map.intersite 'tracking/intersite', :controller => 'trackable_sessions', :action => 'intersite'
8
+ map.content 'tracking/content', :controller => 'trackable_sessions', :action => 'content'
9
+ map.traffic_export 'tracking/export', :controller => 'trackable_sessions', :action => 'export'
10
+ map.visitor_profile 'tracking/visitors', :controller => 'trackable_sessions', :action => 'visitor_profile'
11
+ end
Binary file
@@ -0,0 +1,23 @@
1
+ class CreateTrackableSessions < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :trackable_sessions do |t|
4
+ t.string :kind
5
+ t.string :ip_address
6
+ t.string :site
7
+ t.string :referrer
8
+ t.string :session_id
9
+ t.boolean :new_visit
10
+ t.timestamps
11
+ end
12
+ add_index :trackable_sessions, :kind
13
+ add_index :trackable_sessions, :ip_address
14
+ add_index :trackable_sessions, :referrer
15
+ add_index :trackable_sessions, :site
16
+ add_index :trackable_sessions, :session_id
17
+ add_index :trackable_sessions, :new_visit
18
+ end
19
+
20
+ def self.down
21
+ drop_table :trackable_sessions
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ class CreateTrackableActions < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :trackable_actions do |t|
4
+ t.integer :trackable_session_id
5
+ t.string :kind
6
+ t.string :label
7
+ t.string :referrer
8
+ t.string :url
9
+ t.timestamps
10
+ end
11
+ add_index :trackable_actions, :kind
12
+ add_index :trackable_actions, :label
13
+ add_index :trackable_actions, :url
14
+ add_index :trackable_actions, :trackable_session_id
15
+ end
16
+
17
+ def self.down
18
+ drop_table :trackable_actions
19
+ end
20
+ end
data/db/test.sqlite3 ADDED
@@ -0,0 +1 @@
1
+ S
@@ -0,0 +1,2 @@
1
+ Use this README file to introduce your application and point to useful places in the API for learning more.
2
+ Run "rake doc:app" to generate API documentation for your models, controllers, helpers, and libraries.
data/init.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'mitamirri'
2
+
3
+ ActiveSupport::Dependencies.load_once_paths.delete lib_path
4
+ ActionView::Base.send :include, Mitamirri::Helper
5
+
6
+ if RAILS_ENV == 'development'
7
+ ActiveSupport::Dependencies.load_once_paths.reject!{|x| x =~ /^#{Regexp.escape(File.dirname(__FILE__))}/}
8
+ end
@@ -0,0 +1,75 @@
1
+ module Mitamirri
2
+
3
+ class ContentReport
4
+
5
+ # Search params
6
+ attr_accessor :site
7
+ attr_accessor :time_period
8
+ attr_accessor :visit_kind
9
+
10
+ # Initialization
11
+
12
+ def initialize(args = {})
13
+ args.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}
14
+ self.time_period = args[:time_period] || 'past 3 months'
15
+ self.sessions
16
+ end
17
+
18
+ def sessions
19
+ @sessions ||= TrackableSession.search(
20
+ :site => self.site,
21
+ :time_period => self.time_period,
22
+ :visit_kind => self.visit_kind
23
+ )
24
+ end
25
+
26
+ # Generate an array of dates spanning the appropriate time period.
27
+ def dates
28
+ return @dates if @dates
29
+ @dates = []
30
+ case self.time_period
31
+ when 'past month'
32
+ _increment = 1
33
+ when 'past 3 months'
34
+ _increment = 2
35
+ when 'past 6 months'
36
+ _increment = 5
37
+ when 'past 12 months'
38
+ _increment = 11
39
+ else 'all time'
40
+ _increment = 24
41
+ end
42
+
43
+ (1.._increment).each{ |i| @dates << (Time.zone.now - i.months).beginning_of_month }
44
+ @dates.reverse!
45
+ @dates << Time.zone.now.end_of_month
46
+ @dates
47
+ end
48
+
49
+ # Graphing data
50
+
51
+ def pages_series(args)
52
+ TrackableAction.pages_histogram(args).map{|datum| ContentStat.new(:url => datum[0], :views => datum[1].to_i)}
53
+ end
54
+
55
+ def entrance_pages_series(args)
56
+ TrackableSession.entrance_pages_histogram(args).map{|datum| ContentStat.new(:url => datum[0], :views => datum[1].to_i)}
57
+ end
58
+
59
+ def exit_pages_series(args)
60
+ TrackableSession.exit_pages_histogram(args).map{|datum| ContentStat.new(:url => datum[0], :views => datum[1].to_i)}
61
+ end
62
+
63
+ # Inner stat classes
64
+
65
+ class ContentStat
66
+ attr_accessor :url
67
+ attr_accessor :views
68
+ def initialize(args)
69
+ args.each{ |k,v| self.send("#{k}=", v) }
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,106 @@
1
+ module Mitamirri
2
+ unloadable
3
+
4
+ module Helper
5
+ unloadable
6
+
7
+ # DRY way to return a legend tag that renders correctly in all browsers
8
+ def legend_tag(text, args={})
9
+ args[:id] ||= text.downcase.gsub(/ /,'_')
10
+ if args[:help]
11
+ _html = %{<div id="#{args[:id]}" class="faux_legend">#{text}<span class="help_icon" onclick="$('#{args[:id]}_help').toggle();">?</span></div>\r}
12
+ _html << %{<div id="#{args[:id]}_help" class="popup_help" style="display: none;">#{args[:help]}<br /></div>\r}
13
+ else
14
+ _html = %{<div id="#{args[:id]}" class="faux_legend">#{text}</div>\r}
15
+ end
16
+ _html.gsub!(/ id=""/,'')
17
+ _html.gsub!(/ class=""/,'')
18
+ _html
19
+ end
20
+
21
+ def page_break
22
+ %{<div class="page_break" /><p>(Continued on next page)</p></div>}
23
+ end
24
+
25
+ def track_action(kind, args={})
26
+
27
+ action_label = args[:label]
28
+ action_label ||= kind == 'view' ? 'Page View' : 'Click'
29
+ destination = args[:destination]
30
+ referrer = request.referrer
31
+ remote_ip = request.remote_ip
32
+ user_agent = request.user_agent
33
+ site = URI.parse(request.url).host
34
+ url = request.url
35
+ session_id = request.session_options[:id]
36
+ elem_id = args[:observe]
37
+ begin
38
+ query = CGI.parse(URI.parse(request.referrer).query) if request.referrer
39
+ keywords = query['q'].blank? ? query['p'] : query['q']
40
+ rescue
41
+ keywords = nil
42
+ end
43
+
44
+ if kind == 'view'
45
+ TrackableAction.create_from_params(
46
+ :kind => 'view',
47
+ :label => action_label,
48
+ :referrer => referrer,
49
+ :referring_keywords => keywords,
50
+ :session_id => session_id,
51
+ :user_agent => user_agent,
52
+ :remote_ip => remote_ip,
53
+ :site => site,
54
+ :url => url
55
+ )
56
+ ""
57
+ elsif kind == 'conversion'
58
+ TrackableAction.create_from_params(
59
+ :kind => 'conversion',
60
+ :label => action_label,
61
+ :referrer => referrer,
62
+ :referring_keywords => keywords,
63
+ :session_id => session_id,
64
+ :remote_ip => remote_ip,
65
+ :site => site,
66
+ :url => url
67
+ )
68
+ ""
69
+ elsif kind == 'clickthrough'
70
+ track_event_code(kind, 'mousedown', elem_id, action_label, referrer, remote_ip, site, url, session_id, destination)
71
+ elsif kind == 'click'
72
+ track_event_code(kind, 'mousedown', elem_id, action_label, referrer, remote_ip, site, url, session_id)
73
+ elsif kind == 'mouseover'
74
+ track_event_code(kind, 'mouseover', elem_id, action_label, referrer, remote_ip, site, url, session_id)
75
+ elsif kind == 'scroll'
76
+ track_window_event_code(kind, 'Scrolled Page', referrer, remote_ip, site, url, session_id)
77
+ end
78
+ end
79
+
80
+ def track_event_code(kind, event, elem_id, action_label, referrer, remote_ip, site, url, session_id, destination = nil)
81
+ %{<script type="text/javascript">
82
+ Event.observe(window, 'load', function() {
83
+ Event.observe('#{elem_id}', '#{event}', track_#{event}_#{elem_id}_event);
84
+ });
85
+ function track_#{event}_#{elem_id}_event() {
86
+ new Ajax.Updater('tracking', '/trackable_actions/', {asynchronous:true, evalScripts:true, parameters:'action=create&kind=#{kind}&label=#{action_label}&referrer=#{referrer}&remote_ip=#{remote_ip}&site=#{site}&url=#{url}&session_id=#{session_id}&destination=#{destination}'})
87
+ Event.stopObserving('#{elem_id}', '#{event}', track_#{event}_#{elem_id}_event);
88
+ }
89
+ </script>}
90
+ end
91
+
92
+ def track_window_event_code(kind, action_label, referrer, remote_ip, site, url, session_id)
93
+ %{<script type="text/javascript">
94
+ Event.observe(window, 'load', function() {
95
+ Event.observe(window, '#{kind}', track_window_action);
96
+ });
97
+ function track_window_action() {
98
+ new Ajax.Updater('tracking', '/trackable_actions/', {asynchronous:true, evalScripts:true, parameters:'action=create&kind=#{kind}&label=#{action_label}&referrer=#{referrer}&remote_ip=#{remote_ip}&site=#{site}&url=#{url}&session_id=#{session_id}'});
99
+ Event.stopObserving(window, '#{kind}', track_window_action);
100
+ }
101
+ </script>}
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,122 @@
1
+ module Mitamirri
2
+
3
+ class IntersiteTrafficReport
4
+
5
+ # Search params
6
+ attr_accessor :site
7
+ attr_accessor :time_period
8
+ attr_accessor :visit_kind
9
+
10
+ # Initialization
11
+
12
+ def initialize(args = {})
13
+ args.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}
14
+ self.time_period = args[:time_period] || 'past 6 months'
15
+ self.sessions
16
+ end
17
+
18
+ def destination_histogram
19
+ @destination_histogram ||= TrackableSession.clickthroughs_histogram(self.sessions.to_hash)
20
+ end
21
+
22
+ def destination_sites
23
+ self.destination_histogram.keys
24
+ end
25
+
26
+ def sessions
27
+ @sessions ||= TrackableSession.search(
28
+ :site => self.site,
29
+ :time_period => self.time_period,
30
+ :visit_kind => self.visit_kind,
31
+ :action_kind => 'clickthrough'
32
+ )
33
+ end
34
+
35
+ # Generate an array of dates spanning the appropriate time period.
36
+ def dates
37
+ return @dates if @dates
38
+ @dates = []
39
+ case self.time_period.downcase
40
+ when 'past month'
41
+ _increment = 4
42
+ when 'past 3 months'
43
+ _increment = 12
44
+ when 'past 6 months'
45
+ _increment = 24
46
+ when 'past 12 months'
47
+ _increment = 52
48
+ else
49
+ _increment = 52
50
+ end
51
+
52
+ (1.._increment).each{ |i| @dates << (Time.zone.now - i.weeks).end_of_week }
53
+ @dates.reverse!
54
+ @dates << Time.zone.now.end_of_week
55
+ @dates
56
+ end
57
+
58
+ # Graphing data
59
+
60
+ def total_visits(args)
61
+ [Visit.new(:stats => visit_stats(args))]
62
+ end
63
+
64
+ def total_visit_stats(args)
65
+ _visit_stats = []
66
+ dates.each do |date|
67
+ _visit_stats << VisitStat.new(:date => date, :clickthroughs => TrackableSession.search_without_date(args).for_week(date).with_clickthroughs.count)
68
+ end
69
+ _visit_stats
70
+ end
71
+
72
+ def destinations(args)
73
+ _destinations = []
74
+ self.destination_sites.each do |site|
75
+ _destinations << Destination.new(:site => site, :clickthroughs => TrackableSession.search(args).with_clickthroughs.count)
76
+ end
77
+ _destinations
78
+ end
79
+
80
+ def visit_stats(args)
81
+ _visit_stats = []
82
+ dates.each do |date|
83
+ _visit_stats << VisitStat.new(:date => date, :clickthroughs => TrackableSession.search_without_date(args).for_week(date).with_clickthroughs.count)
84
+ end
85
+ _visit_stats
86
+ end
87
+
88
+ # Inner stat classes
89
+
90
+ class Destination
91
+ attr_accessor :site
92
+ attr_accessor :clickthroughs
93
+ def initialize(args)
94
+ args.each{ |k,v| self.send("#{k}=", v) }
95
+ end
96
+ end
97
+
98
+ class Visit
99
+ attr_accessor :site
100
+ attr_accessor :stats
101
+ def initialize(args)
102
+ args.each{ |k,v| self.send("#{k}=", v) }
103
+ end
104
+ end
105
+
106
+ class VisitStat
107
+ attr_accessor :date
108
+ attr_accessor :clickthroughs
109
+
110
+ def initialize(args)
111
+ args.each{ |k,v| self.send("#{k}=", v) }
112
+ end
113
+
114
+ def short_date
115
+ self.date.to_s(:concise)
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+ end