ahoy_matey 1.0.2 → 1.1.0

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: 2650b75c743530def251ca5e74c48c5a0a6654cc
4
- data.tar.gz: 2dae4a806be1b4bd07d11f355440dedb2af33d85
3
+ metadata.gz: 242d802ebd49a361498abd778f6584270403a044
4
+ data.tar.gz: fbac0f07a91a227d362f81c59b9e5407f12a1914
5
5
  SHA512:
6
- metadata.gz: 04ffd75191af402b3d371f3f641feda3b56482658163b6f4c00838da8c31debd812747199f707a7b87885291cab37855457d76b67a060dd07698bc298993d047
7
- data.tar.gz: 28b522d37a313db0a4987bef9b7521642e0f5c6c44f01e71f3332df4095804d03b2ac68e20ebf106b7ebb6020a84c3a5c189910d0fe0d7cd7d2f3435c89be329
6
+ metadata.gz: 480eb913a57be006b6ed8a9387381f97fa90093ddf81f7509279e1821da88af79a50bb48811f61133efc480018e2ee09bca1f194e9a1a66ae130322abaf7f9d4
7
+ data.tar.gz: ad94528f28a55a852638cea255b0e17e0a1ecc941231f2bf2830faaa77541c736a17196baab0279771cdca5c20bb172d437b0f33f833c0e6dd5bcbfc7db46787
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 1.1.0 [unreleased]
2
+
3
+ - Added `geocode` option
4
+ - Report errors to service by default
5
+ - Fixed association mismatch
6
+
1
7
  ## 1.0.2
2
8
 
3
9
  - Fixed BSON for Mongoid 3
data/README.md CHANGED
@@ -6,6 +6,8 @@ Ahoy provides a solid foundation to track visits and events in Ruby, JavaScript,
6
6
 
7
7
  :postbox: To track emails, check out [Ahoy Email](https://github.com/ankane/ahoy_email).
8
8
 
9
+ Never build an analytics platform from scratch again.
10
+
9
11
  See [upgrade instructions](#upgrading) on how to move to 1.0.
10
12
 
11
13
  ## Installation
@@ -213,7 +215,9 @@ end
213
215
 
214
216
  Exceptions are rescued so analytics do not break your app.
215
217
 
216
- To report them to a service, use:
218
+ Ahoy uses [Errbase](https://github.com/ankane/errbase) to try to report them to a service by default.
219
+
220
+ To customize this, use:
217
221
 
218
222
  ```ruby
219
223
  class Ahoy::Store < Ahoy::Stores::ActiveRecordStore
@@ -327,6 +331,20 @@ class ApplicationController < ActionController::Base
327
331
  end
328
332
  ```
329
333
 
334
+ ### Geocoding
335
+
336
+ By default, geocoding is performed inline. For performance, move it to the background. Add [Active Job](https://github.com/ankane/activejob_backport) and set:
337
+
338
+ ```ruby
339
+ Ahoy.geocode = :async
340
+ ```
341
+
342
+ Or disable it with:
343
+
344
+ ```ruby
345
+ Ahoy.geocode = false
346
+ ```
347
+
330
348
  ### Track Visits Immediately
331
349
 
332
350
  Visitor and visit ids are generated on the first request (so you can use them immediately), but the `track_visit` method isn’t called until the JavaScript library posts to the server. This prevents browsers with cookies disabled from creating multiple visits and ensures visits are not created for API endpoints. Change this with:
@@ -335,7 +353,7 @@ Visitor and visit ids are generated on the first request (so you can use them im
335
353
  Ahoy.track_visits_immediately = true
336
354
  ```
337
355
 
338
- **Note:** At the moment, geocoding is performed in the foreground, which can slow down the first page load.
356
+ **Note:** It’s highly recommended to perform geocoding in the background with this option.
339
357
 
340
358
  You can exclude API endpoints and other actions with:
341
359
 
data/ahoy_matey.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "user_agent_parser"
26
26
  spec.add_dependency "request_store"
27
27
  spec.add_dependency "uuidtools"
28
+ spec.add_dependency "errbase"
28
29
 
29
30
  spec.add_development_dependency "bundler", "~> 1.5"
30
31
  spec.add_development_dependency "rake"
@@ -0,0 +1,14 @@
1
+ module Ahoy
2
+ class GeocodeJob < ActiveJob::Base
3
+ queue_as :ahoy
4
+
5
+ def perform(visit)
6
+ deckhand = Deckhands::LocationDeckhand.new(visit.ip)
7
+ Ahoy::VisitProperties::LOCATION_KEYS.each do |key|
8
+ visit.send(:"#{key}=", deckhand.send(key)) if visit.respond_to?(:"#{key}=")
9
+ end
10
+ visit.save!
11
+ end
12
+
13
+ end
14
+ end
@@ -11,14 +11,13 @@ module Ahoy
11
11
  v.started_at = options[:started_at]
12
12
  end
13
13
 
14
- visit_properties.keys.each do |key|
15
- visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=")
16
- end
14
+ set_visit_properties(visit)
17
15
 
18
16
  yield(visit) if block_given?
19
17
 
20
18
  begin
21
19
  visit.save!
20
+ geocode(visit)
22
21
  rescue ActiveRecord::RecordNotUnique
23
22
  # do nothing
24
23
  end
@@ -11,14 +11,13 @@ module Ahoy
11
11
  v.created_at = options[:started_at]
12
12
  end
13
13
 
14
- visit_properties.keys.each do |key|
15
- visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=")
16
- end
14
+ set_visit_properties(visit)
17
15
 
18
16
  yield(visit) if block_given?
19
17
 
20
18
  begin
21
19
  visit.save!
20
+ geocode(visit)
22
21
  rescue ActiveRecord::RecordNotUnique
23
22
  # do nothing
24
23
  end
@@ -18,12 +18,17 @@ module Ahoy
18
18
  def authenticate(user)
19
19
  @user = user
20
20
  if visit and visit.respond_to?(:user) and !visit.user
21
- visit.user = user
22
- visit.save!
21
+ begin
22
+ visit.user = user
23
+ visit.save!
24
+ rescue ActiveRecord::AssociationTypeMismatch
25
+ # do nothing
26
+ end
23
27
  end
24
28
  end
25
29
 
26
30
  def report_exception(e)
31
+ Errbase.report(e)
27
32
  end
28
33
 
29
34
  def user
@@ -41,7 +46,7 @@ module Ahoy
41
46
  protected
42
47
 
43
48
  def bot?
44
- @bot ||= Browser.new(ua: request.user_agent).bot?
49
+ @bot ||= request ? Browser.new(ua: request.user_agent).bot? : false
45
50
  end
46
51
 
47
52
  def request
@@ -60,6 +65,20 @@ module Ahoy
60
65
  ahoy.visit_properties
61
66
  end
62
67
 
68
+ def set_visit_properties(visit)
69
+ keys = visit_properties.keys
70
+ keys -= Ahoy::VisitProperties::LOCATION_KEYS if Ahoy.geocode != true
71
+ keys.each do |key|
72
+ visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") && visit_properties[key]
73
+ end
74
+ end
75
+
76
+ def geocode(visit)
77
+ if Ahoy.geocode == :async
78
+ Ahoy::GeocodeJob.perform_later(visit)
79
+ end
80
+ end
81
+
63
82
  end
64
83
  end
65
84
  end
@@ -11,13 +11,12 @@ module Ahoy
11
11
  v.started_at = options[:started_at]
12
12
  end
13
13
 
14
- visit_properties.keys.each do |key|
15
- visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") && visit_properties[key]
16
- end
14
+ set_visit_properties(visit)
17
15
 
18
16
  yield(visit) if block_given?
19
17
 
20
18
  visit.upsert
19
+ geocode(visit)
21
20
  end
22
21
 
23
22
  def track_event(name, properties, options, &block)
data/lib/ahoy/tracker.rb CHANGED
@@ -119,7 +119,12 @@ module Ahoy
119
119
  end
120
120
 
121
121
  def report_exception(e)
122
- @store.report_exception(e)
122
+ begin
123
+ @store.report_exception(e)
124
+ rescue
125
+ # fail-safe
126
+ $stderr.puts "Error reporting exception"
127
+ end
123
128
  if Rails.env.development?
124
129
  raise e
125
130
  end
@@ -130,11 +135,11 @@ module Ahoy
130
135
  end
131
136
 
132
137
  def existing_visit_id
133
- @existing_visit_id ||= request.headers["Ahoy-Visit"] || request.cookies["ahoy_visit"]
138
+ @existing_visit_id ||= request && (request.headers["Ahoy-Visit"] || request.cookies["ahoy_visit"])
134
139
  end
135
140
 
136
141
  def existing_visitor_id
137
- @existing_visitor_id ||= request.headers["Ahoy-Visitor"] || request.cookies["ahoy_visitor"]
142
+ @existing_visitor_id ||= request && (request.headers["Ahoy-Visitor"] || request.cookies["ahoy_visitor"])
138
143
  end
139
144
 
140
145
  def ensure_uuid(id)
data/lib/ahoy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ahoy
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/ahoy.rb CHANGED
@@ -5,6 +5,7 @@ require "referer-parser"
5
5
  require "user_agent_parser"
6
6
  require "request_store"
7
7
  require "uuidtools"
8
+ require "errbase"
8
9
 
9
10
  require "ahoy/version"
10
11
  require "ahoy/tracker"
@@ -24,6 +25,14 @@ require "ahoy/stores/mongoid_store"
24
25
  require "ahoy/engine"
25
26
  require "ahoy/warden" if defined?(Warden)
26
27
 
28
+ # background jobs
29
+ begin
30
+ require "active_job"
31
+ rescue LoadError
32
+ # do nothing
33
+ end
34
+ require "ahoy/geocode_job" if defined?(ActiveJob)
35
+
27
36
  # deprecated
28
37
  require "ahoy/subscribers/active_record"
29
38
 
@@ -44,6 +53,9 @@ module Ahoy
44
53
  mattr_accessor :quiet
45
54
  self.quiet = true
46
55
 
56
+ mattr_accessor :geocode
57
+ self.geocode = true
58
+
47
59
  def self.ensure_uuid(id)
48
60
  valid = UUIDTools::UUID.parse(id) rescue nil
49
61
  if valid
@@ -3,8 +3,8 @@ module Ahoy
3
3
  self.table_name = "ahoy_events"
4
4
 
5
5
  belongs_to :visit
6
- belongs_to :user
6
+ belongs_to :user<% if options["database"] != "postgresql" %>
7
7
 
8
- <% if options["database"] != "postgresql" %>serialize :properties, JSON<% end %>
8
+ serialize :properties, JSON<% end %>
9
9
  end
10
10
  end
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: 1.0.2
4
+ version: 1.1.0
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-07-10 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: errbase
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: bundler
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +176,7 @@ files:
162
176
  - lib/ahoy/deckhands/traffic_source_deckhand.rb
163
177
  - lib/ahoy/deckhands/utm_parameter_deckhand.rb
164
178
  - lib/ahoy/engine.rb
179
+ - lib/ahoy/geocode_job.rb
165
180
  - lib/ahoy/model.rb
166
181
  - lib/ahoy/stores/active_record_store.rb
167
182
  - lib/ahoy/stores/active_record_token_store.rb