visitor_sources 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -0
- data/RakeFile +53 -0
- data/VERSION +1 -1
- data/lib/visitor_sources/traffic_source.rb +5 -4
- data/lib/visitor_sources/traffic_source_middleware.rb +1 -0
- data/pkg/visitor_sources-0.0.0.gem +0 -0
- data/test/app.rb +1 -1
- data/test/test_helper.rb +1 -0
- data/test/traffic_source_test.rb +57 -43
- data/visitor_sources.gemspec +63 -0
- metadata +5 -2
data/README.md
CHANGED
@@ -9,3 +9,8 @@ Track where a visitor has come from and kep a record of each visit in the cookie
|
|
9
9
|
|
10
10
|
# requires the use of Rack::Session::Cookie
|
11
11
|
|
12
|
+
TODO
|
13
|
+
----
|
14
|
+
- Allow clearing of all the users history (if they purchase for example)
|
15
|
+
- If user clicks an add twice it only shows up once
|
16
|
+
|
data/RakeFile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "visitor_sources"
|
8
|
+
gem.summary = "Use a cookie to record the sources that each visitor came to your site"
|
9
|
+
gem.description = "Use a cookie to record the sources that each visitor came to your site"
|
10
|
+
gem.email = "mail@matthewfawcett.co.uk"
|
11
|
+
gem.homepage = "http://github.com/mattfawcett/visitor_sources"
|
12
|
+
gem.authors = ["Matt Fawcett"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "visitor_sources #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
@@ -14,7 +14,8 @@
|
|
14
14
|
|
15
15
|
require "uri"
|
16
16
|
class TrafficSource
|
17
|
-
attr_accessor :encoder_version, :unix_timestamp, :medium, :term, :source, :campaign, :content,
|
17
|
+
attr_accessor :encoder_version, :unix_timestamp, :medium, :term, :source, :campaign, :content,
|
18
|
+
:custom_parameter_mapping, :env
|
18
19
|
|
19
20
|
COOKIE_LINE_PARAMETERS = ['encoder_version', 'unix_timestamp', 'medium', 'term', 'source', 'campaign', 'content']
|
20
21
|
STANDARD_PARAMETER_MAPPING = {:medium => :utm_medium, :term => :utm_term, :source => :utm_source, :campaign => :utm_campaign, :content => :utm_content}
|
@@ -25,13 +26,13 @@ class TrafficSource
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
def self.updated_rack_environment(old_env, custom_parameter_mapping = {})
|
29
|
+
def self.updated_rack_environment(old_env, custom_parameter_mapping = {}, ignore_duplicate_source_within=120)
|
29
30
|
old_env["rack.session"][:traffic_sources] ||= ''
|
30
31
|
traffic_sources = TrafficSources.new(old_env["rack.session"][:traffic_sources])
|
31
32
|
latest_source = TrafficSource.initialize_with_rack_env(old_env, custom_parameter_mapping)
|
32
33
|
return old_env if latest_source.to_s.nil?
|
33
34
|
if traffic_sources.length > 0
|
34
|
-
return old_env if latest_source.same_as?(traffic_sources.last)
|
35
|
+
return old_env if latest_source.same_as?(traffic_sources.last) && latest_source.unix_timestamp-traffic_sources.last.unix_timestamp < ignore_duplicate_source_within
|
35
36
|
end
|
36
37
|
traffic_sources << latest_source
|
37
38
|
old_env["rack.session"][:traffic_sources] = traffic_sources.to_s
|
@@ -40,7 +41,7 @@ class TrafficSource
|
|
40
41
|
|
41
42
|
def TrafficSource.initialize_with_rack_env(env, custom_parameter_mapping = {})
|
42
43
|
traffic_source = self.new(:env => env, :custom_parameter_mapping => custom_parameter_mapping,
|
43
|
-
:unix_timestamp => Time.now.to_i, :encoder_version => 1)
|
44
|
+
:unix_timestamp => Time.now.to_i, :encoder_version => 1)
|
44
45
|
|
45
46
|
COOKIE_LINE_PARAMETERS.last(5).each do |attribute|
|
46
47
|
traffic_source.send("#{attribute}=", traffic_source.query_string_value_for(attribute.to_sym))
|
@@ -6,6 +6,7 @@ class TrafficSourceMiddleware
|
|
6
6
|
|
7
7
|
def call(env)
|
8
8
|
@options[:custom_parameter_mapping] ||= {}
|
9
|
+
@options[:ignore_duplicate_source_within] ||= 120 #2 mins
|
9
10
|
env["rack.request.query_hash"] = Rack::Utils.parse_query(env["QUERY_STRING"])
|
10
11
|
env = TrafficSource.updated_rack_environment(env, @options[:custom_parameter_mapping])
|
11
12
|
env[:traffic_sources] = TrafficSources.new(env["rack.session"][:traffic_sources])
|
Binary file
|
data/test/app.rb
CHANGED
@@ -3,7 +3,7 @@ require 'sinatra'
|
|
3
3
|
require File.expand_path(File.dirname(__FILE__) + '/../lib/visitor_sources')
|
4
4
|
|
5
5
|
use Rack::Session::Cookie
|
6
|
-
use TrafficSourceMiddleware, :custom_parameter_mapping => {:campaign => :campaign}
|
6
|
+
use TrafficSourceMiddleware, :custom_parameter_mapping => {:campaign => :campaign, :ignore_duplicate_source_within => 120}
|
7
7
|
|
8
8
|
|
9
9
|
|
data/test/test_helper.rb
CHANGED
data/test/traffic_source_test.rb
CHANGED
@@ -11,8 +11,8 @@ class TrafficSourceTest < Test::Unit::TestCase
|
|
11
11
|
@rack_env = EXAMPLE_RACK_ENV.dup
|
12
12
|
@custom_variable_matches = {:campaign => :custom_campaign, :term => :custom_keywords, :medium => :custom_medium}
|
13
13
|
end
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
context "initialize_with_rack_env" do
|
16
16
|
context "when there are custom variables in the url present" do
|
17
17
|
should "use the custom variables to set the correct TrafficSource" do
|
18
18
|
@rack_env["rack.request.query_hash"] = {"custom_campaign" => "MyCamp1", "custom_keywords" => "Product One", "gclid" => "AutoAdwordsTaggingClid"}
|
@@ -136,6 +136,9 @@ class TrafficSourceTest < Test::Unit::TestCase
|
|
136
136
|
end
|
137
137
|
|
138
138
|
context "updated_rack_environment" do
|
139
|
+
setup do
|
140
|
+
@rack_env["rack.session"][:traffic_sources] = ''
|
141
|
+
end
|
139
142
|
|
140
143
|
should "not update the enviroment if the current TrafficSource#to_s is nil" do
|
141
144
|
@rack_env["HTTP_REFERER"] = "http://localhost:9393/some/path"
|
@@ -143,13 +146,27 @@ class TrafficSourceTest < Test::Unit::TestCase
|
|
143
146
|
assert_equal "", rack_env["rack.session"][:traffic_sources]
|
144
147
|
end
|
145
148
|
|
146
|
-
should "not update the enviroment if the last TrafficSource#to_s is the same as the current traffic source" do
|
149
|
+
should "not update the enviroment if the last TrafficSource#to_s is the same as the current traffic source and the last one was recent" do
|
147
150
|
@rack_env["HTTP_REFERER"] = nil
|
151
|
+
timestamp_of_first_request = Time.now.to_i - 60
|
152
|
+
Delorean.time_travel_to("1 minute ago") do
|
153
|
+
rack_env = TrafficSource.updated_rack_environment(@rack_env)
|
154
|
+
assert_equal "1|#{timestamp_of_first_request}|direct", rack_env["rack.session"][:traffic_sources]
|
155
|
+
end
|
148
156
|
rack_env = TrafficSource.updated_rack_environment(@rack_env)
|
149
|
-
assert_equal "1|#{
|
150
|
-
|
157
|
+
assert_equal "1|#{timestamp_of_first_request}|direct", rack_env["rack.session"][:traffic_sources]
|
158
|
+
end
|
159
|
+
|
160
|
+
should "update the enviroment if the last TrafficSource#to_s is the same as the current traffic source but the last source was a while ago" do
|
161
|
+
@rack_env = EXAMPLE_RACK_ENV.dup
|
162
|
+
@rack_env["HTTP_REFERER"] = nil
|
163
|
+
timestamp_of_first_request = Time.now.to_i - 180
|
164
|
+
Delorean.time_travel_to("3 minute ago") do
|
165
|
+
rack_env = TrafficSource.updated_rack_environment(@rack_env)
|
166
|
+
assert_equal "1|#{timestamp_of_first_request}|direct", rack_env["rack.session"][:traffic_sources]
|
167
|
+
end
|
151
168
|
rack_env = TrafficSource.updated_rack_environment(@rack_env)
|
152
|
-
assert_equal "1|#{Time.now.to_i}|direct", rack_env["rack.session"][:traffic_sources]
|
169
|
+
assert_equal "1|#{timestamp_of_first_request}|direct,1|#{Time.now.to_i}|direct", rack_env["rack.session"][:traffic_sources]
|
153
170
|
end
|
154
171
|
|
155
172
|
should "update the traffic source if the current one is differenty from the last one" do
|
@@ -164,51 +181,48 @@ class TrafficSourceTest < Test::Unit::TestCase
|
|
164
181
|
end
|
165
182
|
end
|
166
183
|
|
167
|
-
end
|
168
184
|
|
169
185
|
|
170
186
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
187
|
+
context "same_as?" do
|
188
|
+
setup do
|
189
|
+
@source_1 = TrafficSource.new(:encoder_version => 1, :unix_timestamp => Time.now.to_i, :medium => 'cpc',
|
190
|
+
:term => 'myterm', :source => 'google', :campaign => 'mycamp')
|
191
|
+
@source_2 = @source_1.dup
|
192
|
+
end
|
177
193
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
194
|
+
should "be true if all the main fields are the same" do
|
195
|
+
assert @source_1.same_as?(@source_2)
|
196
|
+
@source_2.encoder_version = 2
|
197
|
+
@source_2.unix_timestamp = 1234
|
198
|
+
assert @source_1.same_as?(@source_2)
|
199
|
+
end
|
184
200
|
|
185
|
-
|
186
|
-
|
187
|
-
|
201
|
+
should "be false if one of the fields are different" do
|
202
|
+
@source_2.medium = "direct"
|
203
|
+
assert !@source_1.same_as?(@source_2)
|
204
|
+
end
|
188
205
|
end
|
189
|
-
end
|
190
206
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
207
|
+
context "initialize_from_string" do
|
208
|
+
should "create a new TrafficSource using the correct parameters from the string" do
|
209
|
+
string = "1|123456|organic|mysearchterms|google"
|
210
|
+
source = TrafficSource.initialize_from_string(string)
|
211
|
+
assert_equal 1, source.encoder_version
|
212
|
+
assert_equal 123456, source.unix_timestamp
|
213
|
+
assert_equal "organic", source.medium
|
214
|
+
assert_equal "mysearchterms", source.term
|
215
|
+
assert_equal "google", source.source
|
216
|
+
assert_nil source.campaign
|
217
|
+
assert_nil source.content
|
218
|
+
end
|
202
219
|
end
|
203
|
-
end
|
204
220
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
221
|
+
context "date" do
|
222
|
+
should "return a date object using the timestamp" do
|
223
|
+
@source = TrafficSource.new(:unix_timestamp => 1267804832)
|
224
|
+
assert_equal Time.at(1267804832), @source.date
|
225
|
+
end
|
209
226
|
end
|
210
|
-
end
|
211
|
-
|
212
|
-
|
213
|
-
|
227
|
+
end
|
214
228
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in RakeFile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{visitor_sources}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Matt Fawcett"]
|
12
|
+
s.date = %q{2010-03-08}
|
13
|
+
s.description = %q{Use a cookie to record the sources that each visitor came to your site}
|
14
|
+
s.email = %q{mail@matthewfawcett.co.uk}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README.md",
|
20
|
+
"RakeFile",
|
21
|
+
"VERSION",
|
22
|
+
"lib/visitor_sources.rb",
|
23
|
+
"lib/visitor_sources/search_engine.rb",
|
24
|
+
"lib/visitor_sources/traffic_source.rb",
|
25
|
+
"lib/visitor_sources/traffic_source_middleware.rb",
|
26
|
+
"lib/visitor_sources/traffic_sources.rb",
|
27
|
+
"pkg/visitor_sources-0.0.0.gem",
|
28
|
+
"test/app.rb",
|
29
|
+
"test/search_engine_test.rb",
|
30
|
+
"test/test_helper.rb",
|
31
|
+
"test/traffic_source_middleware_test.rb",
|
32
|
+
"test/traffic_source_test.rb",
|
33
|
+
"test/traffic_sources_test.rb",
|
34
|
+
"visitor_sources.gemspec"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/mattfawcett/visitor_sources}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{Use a cookie to record the sources that each visitor came to your site}
|
41
|
+
s.test_files = [
|
42
|
+
"test/app.rb",
|
43
|
+
"test/search_engine_test.rb",
|
44
|
+
"test/test_helper.rb",
|
45
|
+
"test/traffic_source_middleware_test.rb",
|
46
|
+
"test/traffic_source_test.rb",
|
47
|
+
"test/traffic_sources_test.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: visitor_sources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Fawcett
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-03-
|
12
|
+
date: 2010-03-08 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,18 +32,21 @@ extra_rdoc_files:
|
|
32
32
|
- README.md
|
33
33
|
files:
|
34
34
|
- README.md
|
35
|
+
- RakeFile
|
35
36
|
- VERSION
|
36
37
|
- lib/visitor_sources.rb
|
37
38
|
- lib/visitor_sources/search_engine.rb
|
38
39
|
- lib/visitor_sources/traffic_source.rb
|
39
40
|
- lib/visitor_sources/traffic_source_middleware.rb
|
40
41
|
- lib/visitor_sources/traffic_sources.rb
|
42
|
+
- pkg/visitor_sources-0.0.0.gem
|
41
43
|
- test/app.rb
|
42
44
|
- test/search_engine_test.rb
|
43
45
|
- test/test_helper.rb
|
44
46
|
- test/traffic_source_middleware_test.rb
|
45
47
|
- test/traffic_source_test.rb
|
46
48
|
- test/traffic_sources_test.rb
|
49
|
+
- visitor_sources.gemspec
|
47
50
|
has_rdoc: true
|
48
51
|
homepage: http://github.com/mattfawcett/visitor_sources
|
49
52
|
licenses: []
|