ahoy_matey 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 443dfa85abf63a4f515ea9c6865beb09f77404ab
4
- data.tar.gz: 9099a3d7a51f6426f0ebae09fd3d9c831385a1e5
3
+ metadata.gz: 93699ebff0f9bfc35b50d48e7ff477963114b1b2
4
+ data.tar.gz: 848e8110b3cedef57143e54d8415e23a5e650c66
5
5
  SHA512:
6
- metadata.gz: a0967ab9bfbb9fe5a22214d1eb74705edbeede65bdb82a07ecf2982e49a8ce8c74bd22ddfe2b302e16d63d602b1d5058c30322d1b5011879f3fc407dee9e1b37
7
- data.tar.gz: b1c10a691345e7078f017eb009b64f17106cb1324bac5432bd85cc4986a18016028c4a917fe58d8e1e664ab958db43989b0097e36666343755c2001c65df199d
6
+ metadata.gz: fbd26a7a790231a5b8e8b3dfa2d9b7b99b9e87cd862cffbd15d53ae4a003d1e18afea1f4f46c34576c3a5d6a5f25f4f9f39cfdbb94ab9e58a24488df85c18119
7
+ data.tar.gz: b4046b11ece28d61d46f852bf3b0a699f52d24d34faffad687dce093260fcc564d8a5aee29e3583ec3f354fa3fb17d1e68721e7c2419551ab7e0f0e006e46ee3
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  :fire: Simple, powerful visit tracking for Rails
4
4
 
5
+ Visits are stored in **your database** so you can easily combine them with other data.
6
+
5
7
  You get:
6
8
 
7
9
  - traffic source - referrer, referring domain, landing page, search keyword
@@ -9,23 +11,23 @@ You get:
9
11
  - technology - browser, OS, and device type
10
12
  - utm parameters - source, medium, term, content, campaign
11
13
 
12
- It’s all stored in **your database** so you can easily combine it with other data.
13
-
14
14
  See which campaigns generate the most revenue effortlessly.
15
15
 
16
16
  ```ruby
17
17
  Order.joins(:visit).group("utm_campaign").sum(:revenue)
18
18
  ```
19
19
 
20
+ Goes great with services like [Google Analytics](http://www.google.com/analytics/)
21
+
20
22
  ## Ready, Set, Go
21
23
 
22
24
  Add this line to your application’s Gemfile:
23
25
 
24
26
  ```ruby
25
- gem "ahoy_matey"
27
+ gem 'ahoy_matey'
26
28
  ```
27
29
 
28
- And run the generator. This creates a migration to store visits.
30
+ And run the generator. This creates a model to store visits.
29
31
 
30
32
  ```sh
31
33
  rails generate ahoy:install
@@ -71,22 +73,16 @@ class Order < ActiveRecord::Base
71
73
  end
72
74
  ```
73
75
 
74
- When a visitor places an order, the `visit_id` column will be automatically set. Magic!
76
+ When a visitor places an order, the `visit_id` column is automatically set.
75
77
 
76
- To see where most of your orders are coming from, create an association:
77
-
78
- ```ruby
79
- class Order < ActiveRecord::Base
80
- belongs_to :visit
81
- end
82
- ```
78
+ :tada: Magic!
83
79
 
84
- And query away:
80
+ To see where your orders are coming from, use:
85
81
 
86
82
  ```ruby
87
83
  Order.joins(:visit).group("referring_domain").count
88
- Order.joins(:visit).group("device_type").count
89
84
  Order.joins(:visit).group("city").count
85
+ Order.joins(:visit).group("device_type").count
90
86
  ```
91
87
 
92
88
  ## Users
@@ -128,7 +124,7 @@ or
128
124
  http://datakick.org/?utm_medium=twitter&utm_campaign=social&utm_source=tweet123
129
125
  ```
130
126
 
131
- ## Features
127
+ ## More Features
132
128
 
133
129
  - Excludes bots
134
130
  - Degrades gracefully when cookies are disabled
@@ -7,11 +7,11 @@ module Ahoy
7
7
  Ahoy.visit_model.new do |v|
8
8
  v.visit_token = params[:visit_token]
9
9
  v.visitor_token = params[:visitor_token]
10
- v.ip = request.remote_ip
11
- v.user_agent = request.user_agent
12
- v.referrer = params[:referrer]
13
- v.landing_page = params[:landing_page]
14
- v.user = current_user if respond_to?(:current_user)
10
+ v.ip = request.remote_ip if v.respond_to?(:ip=)
11
+ v.user_agent = request.user_agent if v.respond_to?(:user_agent=)
12
+ v.referrer = params[:referrer] if v.respond_to?(:referrer=)
13
+ v.landing_page = params[:landing_page] if v.respond_to?(:landing_page=)
14
+ v.user = current_user if respond_to?(:current_user) and v.respond_to?(:user=)
15
15
  end
16
16
 
17
17
  visit.save!
data/lib/ahoy/model.rb CHANGED
@@ -12,10 +12,12 @@ module Ahoy
12
12
  before_create :set_location
13
13
 
14
14
  def set_traffic_source
15
- referring_uri = Addressable::URI.parse(referrer) rescue nil
16
- self.referring_domain = referring_uri.try(:host)
17
- search_keyword = RefererParser::Referer.new(referrer).search_term rescue nil
18
- self.search_keyword = search_keyword.present? ? search_keyword : nil
15
+ referring_domain = Addressable::URI.parse(referrer).host.first(255) rescue nil
16
+ self.referring_domain = referring_domain if respond_to?(:referring_domain=)
17
+ # performance hack for referer-parser
18
+ search_keyword = Ahoy.referrer_parser.parse(referrer)[1].first(255) rescue nil
19
+ self.search_keyword = search_keyword.presence if respond_to?(:search_keyword=)
20
+ true
19
21
  end
20
22
 
21
23
  def set_utm_parameters
@@ -23,57 +25,64 @@ module Ahoy
23
25
  if landing_uri
24
26
  query_values = landing_uri.query_values || {}
25
27
  %w[utm_source utm_medium utm_term utm_content utm_campaign].each do |name|
26
- self[name] = query_values[name]
28
+ self[name] = query_values[name] if respond_to?(:"#{name}=")
27
29
  end
28
30
  end
31
+ true
29
32
  end
30
33
 
31
34
  def set_technology
32
- browser = Browser.new(ua: user_agent)
35
+ if respond_to?(:user_agent)
36
+ browser = Browser.new(ua: user_agent)
33
37
 
34
- self.browser = browser.name
38
+ self.browser = browser.name if respond_to?(:browser=)
35
39
 
36
- # TODO add more
37
- self.os =
38
- if browser.android?
39
- "Android"
40
- elsif browser.ios?
41
- "iOS"
42
- elsif browser.windows_phone?
43
- "Windows Phone"
44
- elsif browser.blackberry?
45
- "Blackberry"
46
- elsif browser.chrome_os?
47
- "Chrome OS"
48
- elsif browser.mac?
49
- "Mac"
50
- elsif browser.windows?
51
- "Windows"
52
- elsif browser.linux?
53
- "Linux"
54
- end
40
+ # TODO add more
41
+ self.os =
42
+ if browser.android?
43
+ "Android"
44
+ elsif browser.ios?
45
+ "iOS"
46
+ elsif browser.windows_phone?
47
+ "Windows Phone"
48
+ elsif browser.blackberry?
49
+ "Blackberry"
50
+ elsif browser.chrome_os?
51
+ "Chrome OS"
52
+ elsif browser.mac?
53
+ "Mac"
54
+ elsif browser.windows?
55
+ "Windows"
56
+ elsif browser.linux?
57
+ "Linux"
58
+ end if respond_to?(:os=)
55
59
 
56
- self.device_type =
57
- if browser.tv?
58
- "TV"
59
- elsif browser.console?
60
- "Console"
61
- elsif browser.tablet?
62
- "Tablet"
63
- elsif browser.mobile?
64
- "Mobile"
65
- else
66
- "Desktop"
67
- end
60
+ self.device_type =
61
+ if browser.tv?
62
+ "TV"
63
+ elsif browser.console?
64
+ "Console"
65
+ elsif browser.tablet?
66
+ "Tablet"
67
+ elsif browser.mobile?
68
+ "Mobile"
69
+ else
70
+ "Desktop"
71
+ end if respond_to?(:device_type=)
72
+ end
73
+ true
68
74
  end
69
75
 
70
76
  def set_location
71
- location = Geocoder.search(ip).first rescue nil
72
- if location
73
- self.country = location.country.presence
74
- self.region = location.state.presence
75
- self.city = location.city.presence
77
+ if respond_to?(:ip) and [:country=, :region=, :city=].any?{|method| respond_to?(method) }
78
+ location = Geocoder.search(ip).first rescue nil
79
+ if location
80
+ self.country = location.country.presence if respond_to?(:country=)
81
+ self.region = location.state.presence if respond_to?(:region=)
82
+ self.city = location.city.presence if respond_to?(:city=)
83
+ end
76
84
  end
85
+ true
77
86
  end
78
87
 
79
88
  end # end class_eval
data/lib/ahoy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ahoy
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/ahoy_matey.rb CHANGED
@@ -14,6 +14,12 @@ module Ahoy
14
14
  ::Visit
15
15
  end
16
16
 
17
+ # TODO private
18
+ # performance hack for referer-parser
19
+ def self.referrer_parser
20
+ @referrer_parser ||= RefererParser::Referer.new("https://github.com/ankane/ahoy")
21
+ end
22
+
17
23
  end
18
24
 
19
25
  ActionController::Base.send :include, Ahoy::Controller
@@ -1,10 +1,13 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :visits do |t|
4
- # cookies
4
+ # cookies *required*
5
5
  t.string :visit_token
6
6
  t.string :visitor_token
7
7
 
8
+ # the rest are recommended but optional
9
+ # simply remove the columns you don't want
10
+
8
11
  # standard
9
12
  t.string :ip
10
13
  t.text :user_agent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ahoy_matey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-19 00:00:00.000000000 Z
11
+ date: 2014-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable