ffmike-user_event_logger 0.4.1
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/MIT-LICENSE +20 -0
- data/README +61 -0
- data/Rakefile +22 -0
- data/generators/user_event_logger/USAGE +10 -0
- data/generators/user_event_logger/templates/migration.rb +16 -0
- data/generators/user_event_logger/user_event_logger_generator.rb +7 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/models/event.rb +2 -0
- data/lib/user_event_logger.rb +12 -0
- data/lib/view_helpers.rb +15 -0
- data/tasks/user_event_logger_tasks.rake +4 -0
- data/test/database.yml +18 -0
- data/test/schema.rb +12 -0
- data/test/test_helper.rb +34 -0
- data/test/user_event_logger_test.rb +41 -0
- data/test/view_helpers_test.rb +41 -0
- data/uninstall.rb +1 -0
- metadata +72 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
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
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
UserEventLogger
|
2
|
+
===============
|
3
|
+
|
4
|
+
This plugin is extracted from several applications where I had similar problems:
|
5
|
+
tracking various things that users did within the application. For example, I've
|
6
|
+
used this to be able to log outgoing clicks without hooking up an analytics package,
|
7
|
+
to count the number of times an ad was displayed, or to keep track of how often
|
8
|
+
a particular page was visited.
|
9
|
+
|
10
|
+
To use the plugin, you must first create the table that it uses:
|
11
|
+
|
12
|
+
./script/generate user_event_logger add_event_table
|
13
|
+
rake db:migrate
|
14
|
+
|
15
|
+
After that, you can add information to the table from either your controllers or
|
16
|
+
your views:
|
17
|
+
|
18
|
+
log_user_event "In controller", "http://example.com", "27"
|
19
|
+
<%= log_user_event "In view", "http://example.com", "27" %>
|
20
|
+
|
21
|
+
The first argument is the event name, which is required. The second is the
|
22
|
+
destination URL, which is optional. The third is arbitrary extra data, which is
|
23
|
+
optional. The extra data is stored in a text column, so you can add as much
|
24
|
+
as you want here.
|
25
|
+
|
26
|
+
Reporting is left as an exercise for the user. The events table has these columns:
|
27
|
+
|
28
|
+
create_table :events do |t|
|
29
|
+
t.column :source_url, :string
|
30
|
+
t.column :destination_url, :string
|
31
|
+
t.column :remote_ip, :string
|
32
|
+
t.column :logged_at, :datetime
|
33
|
+
t.column :extra_data, :text
|
34
|
+
t.column :event_type, :string
|
35
|
+
end
|
36
|
+
|
37
|
+
Example
|
38
|
+
=======
|
39
|
+
|
40
|
+
To do outgoing link tracking, you can set up a controller method similar to this one:
|
41
|
+
|
42
|
+
def send_to
|
43
|
+
@advertiser = Advertiser.find(params[:id])
|
44
|
+
destination_url = @advertiser.website.sub(/^www/, "http://www")
|
45
|
+
log_user_event "Clicked through", destination_url, params[:id]
|
46
|
+
redirect_to destination_url
|
47
|
+
end
|
48
|
+
|
49
|
+
Gem installation
|
50
|
+
================
|
51
|
+
If you are running rails 2.1 or above you can choose between a standard plugin install and a gem install. To
|
52
|
+
install as a gem, add this to your environment.rb file:
|
53
|
+
|
54
|
+
config.gem 'ffmike-user_event_logger', :source => 'http://gems.github.com'
|
55
|
+
|
56
|
+
and then run:
|
57
|
+
|
58
|
+
rake gems:install
|
59
|
+
|
60
|
+
|
61
|
+
Copyright (c) 2008 Michael A. Gunderloy, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the user_event_logger plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the user_event_logger plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'UserEventLogger'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Description:
|
2
|
+
Create a migration that adds the user event logging table to the project
|
3
|
+
|
4
|
+
Example:
|
5
|
+
./script/generate user_event_logger add_event_table
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
db/migrate/TIMESTAMP_add_event_table
|
9
|
+
|
10
|
+
You will then need to run rake db:migrate to add the table to the database
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :events do |t|
|
4
|
+
t.column :source_url, :string
|
5
|
+
t.column :destination_url, :string
|
6
|
+
t.column :remote_ip, :string
|
7
|
+
t.column :logged_at, :datetime
|
8
|
+
t.column :extra_data, :text
|
9
|
+
t.column :event_type, :string
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :events
|
15
|
+
end
|
16
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/models/event.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module UserEventLogger
|
2
|
+
module UserEventLoggerMixin
|
3
|
+
def log_user_event(name, destination_url = nil, extra_data = nil)
|
4
|
+
event = Event.create(:source_url => request.path,
|
5
|
+
:destination_url => destination_url,
|
6
|
+
:remote_ip => request.remote_ip,
|
7
|
+
:logged_at => Time.now,
|
8
|
+
:extra_data => extra_data,
|
9
|
+
:event_type => name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/view_helpers.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module UserEventLogger
|
2
|
+
module UserEventLoggerViewHelper
|
3
|
+
def log_user_event(name, destination_url = nil, extra_data = nil)
|
4
|
+
returning "" do |result|
|
5
|
+
event = Event.create(:source_url => request.path,
|
6
|
+
:destination_url => destination_url,
|
7
|
+
:remote_ip => request.remote_ip,
|
8
|
+
:logged_at => Time.now,
|
9
|
+
:extra_data => extra_data,
|
10
|
+
:event_type => name
|
11
|
+
)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite:
|
2
|
+
:adapter: sqlite
|
3
|
+
:dbfile: user_event_logger_plugin.sqlite.db
|
4
|
+
sqlite3:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:dbfile: user_event_logger_plugin.sqlite3.db
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: user_event_logger_plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username: rails
|
17
|
+
:password:
|
18
|
+
:database: user_event_logger_plugin_test
|
data/test/schema.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
|
3
|
+
create_table "events", :force => true do |t|
|
4
|
+
t.string "source_url"
|
5
|
+
t.string "destination_url"
|
6
|
+
t.string "remote_ip"
|
7
|
+
t.datetime "logged_at"
|
8
|
+
t.text "extra_data"
|
9
|
+
t.string "event_type"
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
|
6
|
+
|
7
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
8
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
9
|
+
|
10
|
+
db_adapter = ENV['DB']
|
11
|
+
|
12
|
+
# no db passed, try one of these fine config-free DBs before bombing.
|
13
|
+
db_adapter ||= begin
|
14
|
+
require 'rubygems'
|
15
|
+
require 'sqlite'
|
16
|
+
'sqlite'
|
17
|
+
rescue MissingSourceFile
|
18
|
+
begin
|
19
|
+
require 'sqlite3'
|
20
|
+
'sqlite3'
|
21
|
+
rescue MissingSourceFile
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
if db_adapter.nil?
|
26
|
+
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
30
|
+
|
31
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
32
|
+
|
33
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
34
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
class UserEventLoggerTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@controller = ActionController::Base.new
|
9
|
+
r = mock('request')
|
10
|
+
r.stubs(:path).returns("the path")
|
11
|
+
r.stubs(:remote_ip).returns("the remote IP")
|
12
|
+
@controller.stubs(:request).returns(r)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_last_event
|
16
|
+
Event.find(:first, :order => "id DESC")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_log_user_event
|
20
|
+
@controller.log_user_event "the name", "the destination URL", "the extra data"
|
21
|
+
e = get_last_event
|
22
|
+
assert_equal "the path", e.source_url
|
23
|
+
assert_equal "the destination URL", e.destination_url
|
24
|
+
assert_equal "the name", e.event_type
|
25
|
+
assert_equal "the remote IP", e.remote_ip
|
26
|
+
assert_equal "the extra data", e.extra_data
|
27
|
+
assert_equal "the name", e.event_type
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_log_user_event_with_defaults
|
31
|
+
@controller.log_user_event "the name"
|
32
|
+
e = get_last_event
|
33
|
+
assert_equal "the path", e.source_url
|
34
|
+
assert_equal nil, e.destination_url
|
35
|
+
assert_equal "the name", e.event_type
|
36
|
+
assert_equal "the remote IP", e.remote_ip
|
37
|
+
assert_equal nil, e.extra_data
|
38
|
+
assert_equal "the name", e.event_type
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'mocha'
|
4
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
5
|
+
#include UserEventLogger::UserEventLoggerViewHelper
|
6
|
+
|
7
|
+
class ViewHelpersTest < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
@view = ActionView::Base.new
|
10
|
+
r = mock('request')
|
11
|
+
r.stubs(:path).returns("the path")
|
12
|
+
r.stubs(:remote_ip).returns("the remote IP")
|
13
|
+
@view.stubs(:request).returns(r)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_last_event
|
17
|
+
Event.find(:first, :order => "id DESC")
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_log_user_event
|
21
|
+
@view.log_user_event "the name", "the destination URL", "the extra data"
|
22
|
+
e = get_last_event
|
23
|
+
assert_equal "the path", e.source_url
|
24
|
+
assert_equal "the destination URL", e.destination_url
|
25
|
+
assert_equal "the name", e.event_type
|
26
|
+
assert_equal "the remote IP", e.remote_ip
|
27
|
+
assert_equal "the extra data", e.extra_data
|
28
|
+
assert_equal "the name", e.event_type
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_log_user_event_with_defaults
|
32
|
+
@view.log_user_event "the name"
|
33
|
+
e = get_last_event
|
34
|
+
assert_equal "the path", e.source_url
|
35
|
+
assert_equal nil, e.destination_url
|
36
|
+
assert_equal "the name", e.event_type
|
37
|
+
assert_equal "the remote IP", e.remote_ip
|
38
|
+
assert_equal nil, e.extra_data
|
39
|
+
assert_equal "the name", e.event_type
|
40
|
+
end
|
41
|
+
end
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ffmike-user_event_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Gunderloy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: User Event Logger provides a simple interface for tracking user actions such as outbound clicks.
|
17
|
+
email: MikeG1@larkfarm.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README
|
27
|
+
- Rakefile
|
28
|
+
- generators/user_event_logger
|
29
|
+
- generators/user_event_logger/templates
|
30
|
+
- generators/user_event_logger/templates/migration.rb
|
31
|
+
- generators/user_event_logger/USAGE
|
32
|
+
- generators/user_event_logger/user_event_logger_generator.rb
|
33
|
+
- init.rb
|
34
|
+
- install.rb
|
35
|
+
- lib/models/event.rb
|
36
|
+
- lib/user_event_logger.rb
|
37
|
+
- lib/view_helpers.rb
|
38
|
+
- tasks/user_event_logger_tasks.rake
|
39
|
+
- test/database.yml
|
40
|
+
- test/schema.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
- test/user_event_logger_test.rb
|
43
|
+
- test/view_helpers_test.rb
|
44
|
+
- uninstall.rb
|
45
|
+
has_rdoc: false
|
46
|
+
homepage: http://github.com/ffmike/user_event_logger
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Simple user-triggered event tracking for Rails
|
71
|
+
test_files: []
|
72
|
+
|