referer_tracking 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +85 -0
  3. data/Rakefile +39 -0
  4. data/app/assets/javascripts/referer_tracking/application.js +9 -0
  5. data/app/assets/stylesheets/referer_tracking/application.css +7 -0
  6. data/app/controllers/referer_tracking/application_controller.rb +4 -0
  7. data/app/helpers/referer_tracking/application_helper.rb +4 -0
  8. data/app/models/referer_tracking/referer_tracking.rb +5 -0
  9. data/app/views/layouts/referer_tracking/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/db/migrate/20120119124101_create_referer_tracking_referer_trackings.rb +13 -0
  12. data/lib/referer_tracking/controller_addons.rb +66 -0
  13. data/lib/referer_tracking/engine.rb +5 -0
  14. data/lib/referer_tracking/sweeper.rb +23 -0
  15. data/lib/referer_tracking/version.rb +3 -0
  16. data/lib/referer_tracking.rb +27 -0
  17. data/lib/tasks/referer_tracking_tasks.rake +4 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +9 -0
  20. data/test/dummy/app/assets/javascripts/users.js +2 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  22. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  23. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  24. data/test/dummy/app/controllers/application_controller.rb +6 -0
  25. data/test/dummy/app/controllers/users_controller.rb +86 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/helpers/users_helper.rb +2 -0
  28. data/test/dummy/app/models/user.rb +3 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/test/dummy/app/views/users/_form.html.erb +21 -0
  31. data/test/dummy/app/views/users/edit.html.erb +6 -0
  32. data/test/dummy/app/views/users/index.html.erb +23 -0
  33. data/test/dummy/app/views/users/new.html.erb +5 -0
  34. data/test/dummy/app/views/users/show.html.erb +10 -0
  35. data/test/dummy/config/application.rb +45 -0
  36. data/test/dummy/config/boot.rb +10 -0
  37. data/test/dummy/config/database.yml +25 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +30 -0
  40. data/test/dummy/config/environments/production.rb +60 -0
  41. data/test/dummy/config/environments/test.rb +39 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/inflections.rb +10 -0
  44. data/test/dummy/config/initializers/mime_types.rb +5 -0
  45. data/test/dummy/config/initializers/referer_tracking.rb +3 -0
  46. data/test/dummy/config/initializers/secret_token.rb +7 -0
  47. data/test/dummy/config/initializers/session_store.rb +8 -0
  48. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/test/dummy/config/locales/en.yml +5 -0
  50. data/test/dummy/config/routes.rb +6 -0
  51. data/test/dummy/config.ru +4 -0
  52. data/test/dummy/db/development.sqlite3 +0 -0
  53. data/test/dummy/db/migrate/20120119111039_create_users.rb +9 -0
  54. data/test/dummy/db/schema.rb +31 -0
  55. data/test/dummy/db/test.sqlite3 +0 -0
  56. data/test/dummy/log/development.log +117 -0
  57. data/test/dummy/log/test.log +5017 -0
  58. data/test/dummy/public/404.html +26 -0
  59. data/test/dummy/public/422.html +26 -0
  60. data/test/dummy/public/500.html +26 -0
  61. data/test/dummy/public/favicon.ico +0 -0
  62. data/test/dummy/script/rails +6 -0
  63. data/test/dummy/test/fixtures/users.yml +7 -0
  64. data/test/dummy/test/functional/users_controller_test.rb +49 -0
  65. data/test/dummy/test/unit/helpers/users_helper_test.rb +4 -0
  66. data/test/dummy/test/unit/user_test.rb +7 -0
  67. data/test/dummy/tmp/cache/assets/CAA/620/sprockets%2F87b209c0c9da28094a8d5581a21262c6 +0 -0
  68. data/test/dummy/tmp/cache/assets/CDF/070/sprockets%2F70e3c8a3916622c17858d520dcee0d92 +0 -0
  69. data/test/dummy/tmp/cache/assets/CF0/1D0/sprockets%2F6fc757c2c8329244ca95d6909865bbc2 +0 -0
  70. data/test/dummy/tmp/cache/assets/D11/D20/sprockets%2Fcac21eac42152981882bf9e489316af4 +0 -0
  71. data/test/dummy/tmp/cache/assets/D27/530/sprockets%2F4f6982cf32d995b6ac3bce79729b6435 +374 -0
  72. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  73. data/test/dummy/tmp/cache/assets/D39/8B0/sprockets%2Ff3fdaca7531a71f4014b287f392e7b08 +377 -0
  74. data/test/dummy/tmp/cache/assets/D46/650/sprockets%2Ff56253b5f374fff1a33fbbc9881c9124 +0 -0
  75. data/test/dummy/tmp/cache/assets/D54/ED0/sprockets%2F71c9fa01091d432b131da3bb73faf3d4 +0 -0
  76. data/test/dummy/tmp/cache/assets/D61/6B0/sprockets%2Fcb11afa26a559c0c626de8b4b720f554 +9271 -0
  77. data/test/dummy/tmp/cache/assets/D73/220/sprockets%2F3dbc0a37f98fb43ec819b85a64d32c55 +0 -0
  78. data/test/dummy/tmp/cache/assets/D84/210/sprockets%2Fabd0103ccec2b428ac62c94e4c40b384 +9651 -0
  79. data/test/dummy/tmp/cache/assets/D9A/830/sprockets%2F97b0ba4e6b920af1eb0c96a299f28b3b +9268 -0
  80. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  81. data/test/fixtures/referer_tracking/referer_trackings.yml +13 -0
  82. data/test/integration/navigation_test.rb +10 -0
  83. data/test/integration/referer_tracking/referer_session_test.rb +75 -0
  84. data/test/referer_tracking_test.rb +7 -0
  85. data/test/test_helper.rb +10 -0
  86. metadata +260 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # RefererTracking
2
+
3
+ Referer tracking is saves referrer url to session and automates better tracking of who creates models in your Rails app.
4
+
5
+ Http referer_url and first_url are saved to session in controller before_filter. When creating a new model these values are
6
+ saved to referer_trackings table.
7
+
8
+ [<img src="https://secure.travis-ci.org/holli/referer_tracking.png" />](http://travis-ci.org/holli/referer_tracking)
9
+
10
+ ## Install
11
+
12
+ ```
13
+ in /gemfile
14
+ gem 'referer_tracking'
15
+
16
+ # Run following
17
+ # bundle
18
+ # bundle exec rake railties:install:migrations FROM=referer_tracking
19
+ # rake db:migrate
20
+
21
+ ```
22
+
23
+ ## Configure
24
+
25
+ ```
26
+
27
+ ApplicationController
28
+ include RefererTracking::ControllerAddons
29
+ end
30
+
31
+ OtherController
32
+ # This monitors saved items and creates RefererTracking items in db, enable in controllers you want it to be used
33
+ cache_sweeper RefererTracking::Sweeper
34
+ end
35
+
36
+ /config/initializers/referer_tracking.rb
37
+ # Referer tracking is enabled for these models
38
+ RefererTracking.add_tracking_to(User)
39
+ # RefererTracking.add_tracking_to(User, Messages) # for multiple models
40
+
41
+ ```
42
+
43
+ ## Extras
44
+
45
+ You can add own info to referer_tracking table by creating a column in the table and in your controller giving the
46
+ value to referer_tracking.
47
+
48
+ ```
49
+ UserController
50
+ # Example of adding info to referer_tracking, remember to create migration to add column first
51
+ before_filter do |controller|
52
+ referer_tracking_add_info('referer_id', session[:referer_id])
53
+ end
54
+ end
55
+
56
+ ```
57
+
58
+ **Helpers include**
59
+
60
+ - referer_tracking_add_info(key, value) # only set in the first time called
61
+ - referer_tracking_set_info(key, value) # change value always
62
+ - referer_tracking_first_request?
63
+ - referer_tracking_get_key(key)
64
+
65
+ ## Inside
66
+
67
+ RefererTracking::ControllerAddons creates before_filter that saves referer_information to session. Direct access
68
+ is not recommended but possible with session[:referer_tracking]. Information is not saved for known bots.
69
+
70
+ ## Requirements
71
+
72
+ Gem has been tested with ruby 1.8.7, 1.9.2 and Rails 3.1.
73
+
74
+ [<img src="https://secure.travis-ci.org/holli/referer_tracking.png" />](http://travis-ci.org/holli/referer_tracking)
75
+
76
+
77
+ ## Support
78
+
79
+ Submit suggestions or feature requests as a GitHub Issue or Pull Request. Remember to update tests. Tests are quite extensive.
80
+
81
+
82
+ ## Licence
83
+
84
+ This project rocks and uses MIT-LICENSE. (http://www.opensource.org/licenses/mit-license.php)
85
+
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'RefererTracking'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rake/testtask'
30
+
31
+ Rake::TestTask.new(:test) do |t|
32
+ t.libs << 'lib'
33
+ t.libs << 'test'
34
+ t.pattern = 'test/**/*_test.rb'
35
+ t.verbose = false
36
+ end
37
+
38
+
39
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,4 @@
1
+ module RefererTracking
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RefererTracking
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,5 @@
1
+ module RefererTracking
2
+ class RefererTracking < ActiveRecord::Base
3
+ belongs_to :trackable, :polymorphic => true
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RefererTracking</title>
5
+ <%= stylesheet_link_tag "referer_tracking/application" %>
6
+ <%= javascript_include_tag "referer_tracking/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RefererTracking::Engine.routes.draw do
2
+ end
@@ -0,0 +1,13 @@
1
+ class CreateRefererTrackingRefererTrackings < ActiveRecord::Migration
2
+ def change
3
+ create_table :referer_tracking_referer_trackings do |t|
4
+ t.integer :trackable_id
5
+ t.string :trackable_type
6
+ t.text :referer_url
7
+ t.text :first_url
8
+ t.string :user_agent
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,66 @@
1
+ module RefererTracking::ControllerAddons
2
+
3
+ #before_filter :before_filter_referer_tracking_save_to_session
4
+
5
+ def before_filter_referer_tracking_save_to_session
6
+ if session[:referer_tracking].nil? && !request_is_from_a_known_bot?
7
+ @referer_tracking_first_request = true
8
+ session[:referer_tracking] = hash = Hash.new
9
+ request_ref = "unknown"
10
+ request_ref = request.headers["HTTP_REFERER"] if !request.headers["HTTP_REFERER"].blank?
11
+
12
+ hash[:referer_url] = request_ref
13
+ hash[:first_url] = request.url
14
+
15
+ logger.info( "REFERER_TRACKING_FIRST: ver03 (ref|first) ||| #{hash[:referer_url]} ||| #{hash[:first_url]}" )
16
+ end
17
+ end
18
+
19
+ # Add only if referer_tracking already in session and key has not been changed before
20
+ def referer_tracking_add_info(key, value)
21
+ if !session[:referer_tracking].nil? && session[:referer_tracking][key.to_sym].nil?
22
+ session[:referer_tracking][key.to_sym] = value
23
+ end
24
+ end
25
+
26
+ def referer_tracking_set_info(key, value)
27
+ if !session[:referer_tracking].nil?
28
+ session[:referer_tracking][key.to_sym] = value
29
+ end
30
+ end
31
+
32
+ def referer_tracking_get_info(key)
33
+ unless session[:referer_tracking].nil?
34
+ session[:referer_tracking][key.to_sym]
35
+ else
36
+ nil
37
+ end
38
+ end
39
+
40
+ def referer_tracking_first_request?
41
+ @referer_tracking_first_request
42
+ end
43
+
44
+
45
+ def request_is_from_a_known_bot?
46
+ bot_user_agents = /\b(GoogleBot|Mediapartners-Google|msnbot|TwengaBot|DigExt; DTS Agent|YandexImages)\b/i
47
+ request.user_agent =~ bot_user_agents
48
+ end
49
+
50
+ def request_is_from_a_possible_bot?
51
+ request.user_agent =~ /bot/i
52
+ end
53
+
54
+
55
+ def self.included(base)
56
+ base.class_eval do
57
+ before_filter :before_filter_referer_tracking_save_to_session
58
+ helper_method :'referer_tracking_first_request?'
59
+ helper_method :'referer_tracking_add_info'
60
+ helper_method :'referer_tracking_set_info'
61
+ helper_method :'referer_tracking_get_info'
62
+ end
63
+ end
64
+
65
+ end
66
+
@@ -0,0 +1,5 @@
1
+ module RefererTracking
2
+ class Engine < Rails::Engine
3
+ isolate_namespace RefererTracking
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ class RefererTracking::Sweeper < ActionController::Caching::Sweeper
2
+ def after_create(record)
3
+
4
+ if session && session["referer_tracking"]
5
+ ses = session["referer_tracking"]
6
+
7
+
8
+ ref_mod = RefererTracking::RefererTracking.new(
9
+ :trackable_id => record.id, :trackable_type => record.class.to_s)
10
+
11
+ ses.each_pair do |key, value|
12
+ ref_mod[key] = value if ref_mod.has_attribute?(key)
13
+ end
14
+
15
+ ref_mod[:user_agent] = request.env['HTTP_USER_AGENT']
16
+ ref_mod.save
17
+ end
18
+
19
+ rescue Exception => e
20
+ Rails.logger.info "RefererTracking::Sweeper.after_create problem with creating record: #{e}"
21
+ end
22
+ end
23
+
@@ -0,0 +1,3 @@
1
+ module RefererTracking
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require "referer_tracking/engine"
2
+ require "referer_tracking/controller_addons"
3
+ require "referer_tracking/sweeper"
4
+
5
+ module RefererTracking
6
+
7
+ def self.add_tracking_to(*models_list)
8
+ models_list.each do |model|
9
+ model.class_eval do
10
+ include RefererTracking::TrackableModule
11
+ end
12
+ end
13
+
14
+ RefererTracking::Sweeper.class_eval do
15
+ observe models_list
16
+ end
17
+ end
18
+
19
+ module TrackableModule
20
+ def self.included(base)
21
+ base.class_eval do
22
+ has_one :referer_tracking, :class_name => "RefererTracking::RefererTracking", :as => :trackable
23
+ end
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :referer_tracking do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,56 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ div.field, div.actions {
20
+ margin-bottom: 10px;
21
+ }
22
+
23
+ #notice {
24
+ color: green;
25
+ }
26
+
27
+ .field_with_errors {
28
+ padding: 2px;
29
+ background-color: red;
30
+ display: table;
31
+ }
32
+
33
+ #error_explanation {
34
+ width: 450px;
35
+ border: 2px solid red;
36
+ padding: 7px;
37
+ padding-bottom: 0;
38
+ margin-bottom: 20px;
39
+ background-color: #f0f0f0;
40
+ }
41
+
42
+ #error_explanation h2 {
43
+ text-align: left;
44
+ font-weight: bold;
45
+ padding: 5px 5px 5px 15px;
46
+ font-size: 12px;
47
+ margin: -7px;
48
+ margin-bottom: 0px;
49
+ background-color: #c00;
50
+ color: #fff;
51
+ }
52
+
53
+ #error_explanation ul li {
54
+ font-size: 12px;
55
+ list-style: square;
56
+ }