Added exclude method

Andrew Kane committed May 22, 2014
commit 4e4a0fb795cdf187f7ba88d733ab7be155d863f0
Showing 4 changed files with 33 additions and 16 deletions
CHANGELOG.md +4 -0
@@ @@ -1,3 +1,7 @@
+ ## 0.2.2 [unreleased]
+
+ - Added `exclude_method` option
+
## 0.2.1
- Fixed IE 8 error
README.md +8 -1
@@ @@ -367,6 +367,14 @@ Use a different model for events
Ahoy.subscribers << Ahoy::Subscribers::ActiveRecord.new(model: Event)
```
+ Exclude visits and events [master]
+
+ ```ruby
+ Ahoy.exclude_method = proc do |controller|
+ controller.request.ip == "192.168.1.1"
+ end
+ ```
+
Track bots
```ruby
@@ @@ -386,7 +394,6 @@ end
## TODO
- - custom hook to exclude visits and events
- handle batch events
- better readme
- simple dashboard
ahoy/tracker.rb b/lib/ahoy/tracker.rb +19 -15
@@ @@ -6,25 +6,29 @@ module Ahoy
end
def track(name, properties = {}, options = {})
- # publish to each subscriber
- options = options.dup
- if @controller
- options[:controller] ||= @controller
- options[:user] ||= Ahoy.fetch_user(@controller)
- if @controller.respond_to?(:current_visit)
- options[:visit] ||= @controller.current_visit
+ if !(@controller and Ahoy.exclude_method and Ahoy.exclude_method.call(@controller))
+ # publish to each subscriber
+ options = options.dup
+ if @controller
+ options[:controller] ||= @controller
+ options[:user] ||= Ahoy.fetch_user(@controller)
+ if @controller.respond_to?(:current_visit)
+ options[:visit] ||= @controller.current_visit
+ end
end
- end
- options[:time] ||= Time.zone.now
+ options[:time] ||= Time.zone.now
- subscribers = Ahoy.subscribers
- if subscribers.any?
- subscribers.each do |subscriber|
- subscriber.track(name, properties, options)
+ subscribers = Ahoy.subscribers
+ if subscribers.any?
+ subscribers.each do |subscriber|
+ subscriber.track(name, properties, options)
+ end
+ else
+ $stderr.puts "No subscribers"
end
- else
- $stderr.puts "No subscribers"
end
+
+ true
end
end
ahoy_matey.rb b/lib/ahoy_matey.rb +2 -0
@@ @@ -46,6 +46,8 @@ module Ahoy
(controller.respond_to?(:current_user) && controller.current_user) || (controller.respond_to?(:current_resource_owner, true) && controller.send(:current_resource_owner)) || nil
end
+ mattr_accessor :exclude_method
+
mattr_accessor :subscribers
self.subscribers = []