ahoy_matey 1.5.5 → 4.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +184 -34
- data/CONTRIBUTING.md +42 -0
- data/LICENSE.txt +1 -1
- data/README.md +464 -407
- data/app/controllers/ahoy/base_controller.rb +23 -15
- data/app/controllers/ahoy/events_controller.rb +8 -2
- data/app/controllers/ahoy/visits_controller.rb +8 -1
- data/app/jobs/ahoy/geocode_job.rb +11 -0
- data/app/jobs/ahoy/geocode_v2_job.rb +31 -0
- data/config/routes.rb +1 -1
- data/lib/ahoy/base_store.rb +101 -0
- data/lib/ahoy/controller.rb +23 -16
- data/lib/ahoy/database_store.rb +94 -0
- data/lib/ahoy/engine.rb +14 -7
- data/lib/ahoy/helper.rb +40 -0
- data/lib/ahoy/model.rb +5 -27
- data/lib/ahoy/query_methods.rb +88 -0
- data/lib/ahoy/tracker.rb +105 -51
- data/lib/ahoy/utils.rb +7 -0
- data/lib/ahoy/version.rb +1 -1
- data/lib/ahoy/visit_properties.rb +99 -37
- data/lib/ahoy.rb +83 -93
- data/lib/ahoy_matey.rb +1 -1
- data/lib/generators/ahoy/activerecord_generator.rb +67 -0
- data/lib/generators/ahoy/base_generator.rb +13 -0
- data/lib/generators/ahoy/install_generator.rb +44 -0
- data/lib/generators/ahoy/mongoid_generator.rb +16 -0
- data/lib/generators/ahoy/templates/active_record_event_model.rb.tt +10 -0
- data/lib/generators/ahoy/templates/active_record_migration.rb.tt +62 -0
- data/lib/generators/ahoy/templates/active_record_visit_model.rb.tt +6 -0
- data/lib/generators/ahoy/templates/base_store_initializer.rb.tt +25 -0
- data/lib/generators/ahoy/templates/database_store_initializer.rb.tt +10 -0
- data/lib/generators/ahoy/{stores/templates/mongoid_event_model.rb → templates/mongoid_event_model.rb.tt} +4 -2
- data/lib/generators/ahoy/{stores/templates/mongoid_visit_model.rb → templates/mongoid_visit_model.rb.tt} +15 -9
- data/vendor/assets/javascripts/ahoy.js +271 -133
- metadata +37 -273
- data/.gitignore +0 -17
- data/Gemfile +0 -6
- data/Rakefile +0 -8
- data/ahoy_matey.gemspec +0 -38
- data/lib/ahoy/deckhands/location_deckhand.rb +0 -49
- data/lib/ahoy/deckhands/request_deckhand.rb +0 -52
- data/lib/ahoy/deckhands/technology_deckhand.rb +0 -47
- data/lib/ahoy/deckhands/traffic_source_deckhand.rb +0 -22
- data/lib/ahoy/deckhands/utm_parameter_deckhand.rb +0 -23
- data/lib/ahoy/geocode_job.rb +0 -13
- data/lib/ahoy/logger_silencer.rb +0 -75
- data/lib/ahoy/properties.rb +0 -58
- data/lib/ahoy/stores/active_record_store.rb +0 -61
- data/lib/ahoy/stores/active_record_token_store.rb +0 -114
- data/lib/ahoy/stores/base_store.rb +0 -88
- data/lib/ahoy/stores/bunny_store.rb +0 -33
- data/lib/ahoy/stores/fluentd_store.rb +0 -17
- data/lib/ahoy/stores/kafka_store.rb +0 -40
- data/lib/ahoy/stores/kinesis_firehose_store.rb +0 -42
- data/lib/ahoy/stores/log_store.rb +0 -53
- data/lib/ahoy/stores/mongoid_store.rb +0 -63
- data/lib/ahoy/stores/nats_store.rb +0 -34
- data/lib/ahoy/stores/nsq_store.rb +0 -36
- data/lib/ahoy/subscribers/active_record.rb +0 -19
- data/lib/ahoy/throttle.rb +0 -17
- data/lib/generators/ahoy/stores/active_record_events_generator.rb +0 -53
- data/lib/generators/ahoy/stores/active_record_generator.rb +0 -16
- data/lib/generators/ahoy/stores/active_record_visits_generator.rb +0 -43
- data/lib/generators/ahoy/stores/bunny_generator.rb +0 -15
- data/lib/generators/ahoy/stores/custom_generator.rb +0 -15
- data/lib/generators/ahoy/stores/fluentd_generator.rb +0 -15
- data/lib/generators/ahoy/stores/kafka_generator.rb +0 -15
- data/lib/generators/ahoy/stores/kinesis_firehose_generator.rb +0 -15
- data/lib/generators/ahoy/stores/log_generator.rb +0 -15
- data/lib/generators/ahoy/stores/mongoid_events_generator.rb +0 -19
- data/lib/generators/ahoy/stores/mongoid_generator.rb +0 -14
- data/lib/generators/ahoy/stores/mongoid_visits_generator.rb +0 -27
- data/lib/generators/ahoy/stores/nats_generator.rb +0 -15
- data/lib/generators/ahoy/stores/nsq_generator.rb +0 -15
- data/lib/generators/ahoy/stores/templates/active_record_event_model.rb +0 -12
- data/lib/generators/ahoy/stores/templates/active_record_events_migration.rb +0 -19
- data/lib/generators/ahoy/stores/templates/active_record_initializer.rb +0 -3
- data/lib/generators/ahoy/stores/templates/active_record_visit_model.rb +0 -4
- data/lib/generators/ahoy/stores/templates/active_record_visits_migration.rb +0 -57
- data/lib/generators/ahoy/stores/templates/bunny_initializer.rb +0 -9
- data/lib/generators/ahoy/stores/templates/custom_initializer.rb +0 -10
- data/lib/generators/ahoy/stores/templates/fluentd_initializer.rb +0 -3
- data/lib/generators/ahoy/stores/templates/kafka_initializer.rb +0 -9
- data/lib/generators/ahoy/stores/templates/kinesis_firehose_initializer.rb +0 -17
- data/lib/generators/ahoy/stores/templates/log_initializer.rb +0 -3
- data/lib/generators/ahoy/stores/templates/mongoid_initializer.rb +0 -3
- data/lib/generators/ahoy/stores/templates/nats_initializer.rb +0 -9
- data/lib/generators/ahoy/stores/templates/nsq_initializer.rb +0 -9
- data/test/properties/mysql_json_test.rb +0 -18
- data/test/properties/mysql_text_test.rb +0 -19
- data/test/properties/postgresql_hstore_test.rb +0 -18
- data/test/properties/postgresql_json_test.rb +0 -18
- data/test/properties/postgresql_jsonb_test.rb +0 -18
- data/test/properties/postgresql_text_test.rb +0 -19
- data/test/test_helper.rb +0 -99
- data/test/visit_properties_test.rb +0 -44
data/lib/ahoy/logger_silencer.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
# from https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/extension/logger_silencer.rb
|
2
|
-
require "thread"
|
3
|
-
require "active_support/core_ext/class/attribute_accessors"
|
4
|
-
require "active_support/core_ext/module/aliasing"
|
5
|
-
require "active_support/core_ext/module/attribute_accessors"
|
6
|
-
require "active_support/concern"
|
7
|
-
|
8
|
-
module Ahoy
|
9
|
-
module LoggerSilencer
|
10
|
-
extend ActiveSupport::Concern
|
11
|
-
|
12
|
-
included do
|
13
|
-
cattr_accessor :silencer
|
14
|
-
self.silencer = true
|
15
|
-
alias_method :level_without_threadsafety, :level
|
16
|
-
alias_method :level, :level_with_threadsafety
|
17
|
-
alias_method :add_without_threadsafety, :add
|
18
|
-
alias_method :add, :add_with_threadsafety
|
19
|
-
end
|
20
|
-
|
21
|
-
def thread_level
|
22
|
-
Thread.current[thread_hash_level_key]
|
23
|
-
end
|
24
|
-
|
25
|
-
def thread_level=(level)
|
26
|
-
Thread.current[thread_hash_level_key] = level
|
27
|
-
end
|
28
|
-
|
29
|
-
def level_with_threadsafety
|
30
|
-
thread_level || level_without_threadsafety
|
31
|
-
end
|
32
|
-
|
33
|
-
def add_with_threadsafety(severity, message = nil, progname = nil, &block)
|
34
|
-
if (defined?(@logdev) && @logdev.nil?) || (severity || UNKNOWN) < level
|
35
|
-
true
|
36
|
-
else
|
37
|
-
add_without_threadsafety(severity, message, progname, &block)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Silences the logger for the duration of the block.
|
42
|
-
def silence_logger(temporary_level = Logger::ERROR)
|
43
|
-
if silencer
|
44
|
-
begin
|
45
|
-
self.thread_level = temporary_level
|
46
|
-
yield self
|
47
|
-
ensure
|
48
|
-
self.thread_level = nil
|
49
|
-
end
|
50
|
-
else
|
51
|
-
yield self
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
for severity in Logger::Severity.constants
|
56
|
-
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
57
|
-
def #{severity.downcase}? # def debug?
|
58
|
-
Logger::#{severity} >= level # DEBUG >= level
|
59
|
-
end # end
|
60
|
-
EOT
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def thread_hash_level_key
|
66
|
-
@thread_hash_level_key ||= :"ThreadSafeLogger##{object_id}@level"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class NilLogger
|
72
|
-
def self.silence_logger
|
73
|
-
yield
|
74
|
-
end
|
75
|
-
end
|
data/lib/ahoy/properties.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Properties
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def where_properties(properties)
|
7
|
-
relation = self
|
8
|
-
column_type = columns_hash["properties"].type
|
9
|
-
adapter_name = connection.adapter_name.downcase
|
10
|
-
case adapter_name
|
11
|
-
when /mysql/
|
12
|
-
if column_type == :json
|
13
|
-
properties.each do |k, v|
|
14
|
-
if v.nil?
|
15
|
-
v = "null"
|
16
|
-
elsif v == true
|
17
|
-
v = "true"
|
18
|
-
end
|
19
|
-
|
20
|
-
relation = relation.where("JSON_UNQUOTE(properties -> ?) = ?", "$.#{k.to_s}", v.as_json)
|
21
|
-
end
|
22
|
-
else
|
23
|
-
properties.each do |k, v|
|
24
|
-
relation = relation.where("properties REGEXP ?", "[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]")
|
25
|
-
end
|
26
|
-
end
|
27
|
-
when /postgres|postgis/
|
28
|
-
if column_type == :jsonb || column_type == :json
|
29
|
-
properties.each do |k, v|
|
30
|
-
relation =
|
31
|
-
if v.nil?
|
32
|
-
relation.where("properties ->> ? IS NULL", k.to_s)
|
33
|
-
else
|
34
|
-
relation.where("properties ->> ? = ?", k.to_s, v.as_json.to_s)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
elsif column_type == :hstore
|
38
|
-
properties.each do |k, v|
|
39
|
-
relation =
|
40
|
-
if v.nil?
|
41
|
-
relation.where("properties -> ? IS NULL", k.to_s)
|
42
|
-
else
|
43
|
-
relation.where("properties -> ? = ?", k.to_s, v.to_s)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
else
|
47
|
-
properties.each do |k, v|
|
48
|
-
relation = relation.where("properties SIMILAR TO ?", "%[{,]#{{k.to_s => v}.to_json.sub(/\A\{/, "").sub(/\}\z/, "").gsub("+", "\\\\+")}[,}]%")
|
49
|
-
end
|
50
|
-
end
|
51
|
-
else
|
52
|
-
raise "Adapter not supported: #{adapter_name}"
|
53
|
-
end
|
54
|
-
relation
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class ActiveRecordStore < BaseStore
|
4
|
-
def track_visit(options, &block)
|
5
|
-
@visit =
|
6
|
-
visit_model.new do |v|
|
7
|
-
v.id = ahoy.visit_id
|
8
|
-
v.visitor_id = ahoy.visitor_id
|
9
|
-
v.user = user if v.respond_to?(:user=)
|
10
|
-
v.started_at = options[:started_at]
|
11
|
-
end
|
12
|
-
|
13
|
-
set_visit_properties(visit)
|
14
|
-
|
15
|
-
yield(visit) if block_given?
|
16
|
-
|
17
|
-
begin
|
18
|
-
visit.save!
|
19
|
-
geocode(visit)
|
20
|
-
rescue *unique_exception_classes
|
21
|
-
# reset to nil so subsequent calls to track_event will load visit from DB
|
22
|
-
@visit = nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def track_event(name, properties, options, &block)
|
27
|
-
event =
|
28
|
-
event_model.new do |e|
|
29
|
-
e.id = options[:id]
|
30
|
-
e.visit_id = ahoy.visit_id
|
31
|
-
e.user = user if e.respond_to?(:user=)
|
32
|
-
e.name = name
|
33
|
-
e.properties = properties
|
34
|
-
e.time = options[:time]
|
35
|
-
end
|
36
|
-
|
37
|
-
yield(event) if block_given?
|
38
|
-
|
39
|
-
begin
|
40
|
-
event.save!
|
41
|
-
rescue *unique_exception_classes
|
42
|
-
# do nothing
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def visit
|
47
|
-
@visit ||= visit_model.where(id: ahoy.visit_id).first if ahoy.visit_id
|
48
|
-
end
|
49
|
-
|
50
|
-
protected
|
51
|
-
|
52
|
-
def visit_model
|
53
|
-
::Visit
|
54
|
-
end
|
55
|
-
|
56
|
-
def event_model
|
57
|
-
::Ahoy::Event
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,114 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class ActiveRecordTokenStore < BaseStore
|
4
|
-
def track_visit(options, &block)
|
5
|
-
@visit =
|
6
|
-
visit_model.new do |v|
|
7
|
-
v.visit_token = ahoy.visit_token
|
8
|
-
v.visitor_token = ahoy.visitor_token
|
9
|
-
v.user = user if v.respond_to?(:user=)
|
10
|
-
v.started_at = options[:started_at] if v.respond_to?(:started_at)
|
11
|
-
v.created_at = options[:started_at] if v.respond_to?(:created_at)
|
12
|
-
end
|
13
|
-
|
14
|
-
set_visit_properties(visit)
|
15
|
-
|
16
|
-
yield(visit) if block_given?
|
17
|
-
|
18
|
-
begin
|
19
|
-
visit.save!
|
20
|
-
geocode(visit)
|
21
|
-
rescue *unique_exception_classes
|
22
|
-
# reset to nil so subsequent calls to track_event will load visit from DB
|
23
|
-
@visit = nil
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def track_event(name, properties, options, &block)
|
28
|
-
if self.class.uses_deprecated_subscribers?
|
29
|
-
options[:controller] ||= controller
|
30
|
-
options[:user] ||= user
|
31
|
-
options[:visit] ||= visit
|
32
|
-
options[:visit_token] ||= ahoy.visit_token
|
33
|
-
options[:visitor_token] ||= ahoy.visitor_token
|
34
|
-
|
35
|
-
subscribers = Ahoy.subscribers
|
36
|
-
if subscribers.any?
|
37
|
-
subscribers.each do |subscriber|
|
38
|
-
subscriber.track(name, properties, options.dup)
|
39
|
-
end
|
40
|
-
else
|
41
|
-
$stderr.puts "No subscribers"
|
42
|
-
end
|
43
|
-
else
|
44
|
-
event =
|
45
|
-
event_model.new do |e|
|
46
|
-
e.visit_id = visit.try(:id)
|
47
|
-
e.user = user if e.respond_to?(:user=)
|
48
|
-
e.name = name
|
49
|
-
e.properties = properties
|
50
|
-
e.time = options[:time]
|
51
|
-
end
|
52
|
-
|
53
|
-
yield(event) if block_given?
|
54
|
-
|
55
|
-
event.save!
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def visit
|
60
|
-
@visit ||= (visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token)
|
61
|
-
end
|
62
|
-
|
63
|
-
def exclude?
|
64
|
-
(!Ahoy.track_bots && bot?) ||
|
65
|
-
(
|
66
|
-
if Ahoy.exclude_method
|
67
|
-
warn "[DEPRECATION] Ahoy.exclude_method is deprecated - use exclude? instead"
|
68
|
-
if Ahoy.exclude_method.arity == 1
|
69
|
-
Ahoy.exclude_method.call(controller)
|
70
|
-
else
|
71
|
-
Ahoy.exclude_method.call(controller, request)
|
72
|
-
end
|
73
|
-
else
|
74
|
-
false
|
75
|
-
end
|
76
|
-
)
|
77
|
-
end
|
78
|
-
|
79
|
-
def user
|
80
|
-
@user ||= begin
|
81
|
-
user_method = Ahoy.user_method
|
82
|
-
if user_method.respond_to?(:call)
|
83
|
-
user_method.call(controller)
|
84
|
-
elsif user_method
|
85
|
-
controller.send(user_method)
|
86
|
-
else
|
87
|
-
super
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
class << self
|
93
|
-
def uses_deprecated_subscribers
|
94
|
-
warn "[DEPRECATION] Ahoy subscribers are deprecated"
|
95
|
-
@uses_deprecated_subscribers = true
|
96
|
-
end
|
97
|
-
|
98
|
-
def uses_deprecated_subscribers?
|
99
|
-
@uses_deprecated_subscribers || false
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
protected
|
104
|
-
|
105
|
-
def visit_model
|
106
|
-
Ahoy.visit_model || ::Visit
|
107
|
-
end
|
108
|
-
|
109
|
-
def event_model
|
110
|
-
::Ahoy::Event
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
@@ -1,88 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class BaseStore
|
4
|
-
def initialize(options)
|
5
|
-
@options = options
|
6
|
-
end
|
7
|
-
|
8
|
-
def track_visit(options)
|
9
|
-
end
|
10
|
-
|
11
|
-
def track_event(name, properties, options)
|
12
|
-
end
|
13
|
-
|
14
|
-
def visit
|
15
|
-
end
|
16
|
-
|
17
|
-
def authenticate(user)
|
18
|
-
@user = user
|
19
|
-
if visit && visit.respond_to?(:user) && !visit.user
|
20
|
-
begin
|
21
|
-
visit.user = user
|
22
|
-
visit.save!
|
23
|
-
rescue ActiveRecord::AssociationTypeMismatch
|
24
|
-
# do nothing
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def report_exception(e)
|
30
|
-
raise e
|
31
|
-
end
|
32
|
-
|
33
|
-
def user
|
34
|
-
@user ||= (controller.respond_to?(:current_user) && controller.current_user) || (controller.respond_to?(:current_resource_owner, true) && controller.send(:current_resource_owner)) || nil
|
35
|
-
end
|
36
|
-
|
37
|
-
def exclude?
|
38
|
-
bot?
|
39
|
-
end
|
40
|
-
|
41
|
-
def generate_id
|
42
|
-
SecureRandom.uuid
|
43
|
-
end
|
44
|
-
|
45
|
-
protected
|
46
|
-
|
47
|
-
def bot?
|
48
|
-
@bot ||= request ? Browser.new(request.user_agent).bot? : false
|
49
|
-
end
|
50
|
-
|
51
|
-
def request
|
52
|
-
@request ||= @options[:request] || controller.try(:request)
|
53
|
-
end
|
54
|
-
|
55
|
-
def controller
|
56
|
-
@controller ||= @options[:controller]
|
57
|
-
end
|
58
|
-
|
59
|
-
def ahoy
|
60
|
-
@ahoy ||= @options[:ahoy]
|
61
|
-
end
|
62
|
-
|
63
|
-
def visit_properties
|
64
|
-
ahoy.visit_properties
|
65
|
-
end
|
66
|
-
|
67
|
-
def set_visit_properties(visit)
|
68
|
-
keys = visit_properties.keys
|
69
|
-
keys.each do |key|
|
70
|
-
visit.send(:"#{key}=", visit_properties[key]) if visit.respond_to?(:"#{key}=") && visit_properties[key]
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def geocode(visit)
|
75
|
-
if Ahoy.geocode == :async
|
76
|
-
Ahoy::GeocodeJob.set(queue: Ahoy.job_queue).perform_later(visit)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def unique_exception_classes
|
81
|
-
classes = []
|
82
|
-
classes << ActiveRecord::RecordNotUnique if defined?(ActiveRecord::RecordNotUnique)
|
83
|
-
classes << PG::UniqueViolation if defined?(PG::UniqueViolation)
|
84
|
-
classes
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class BunnyStore < LogStore
|
4
|
-
def log_visit(data)
|
5
|
-
post(visits_queue, data)
|
6
|
-
end
|
7
|
-
|
8
|
-
def log_event(data)
|
9
|
-
post(events_queue, data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def channel
|
13
|
-
@channel ||= begin
|
14
|
-
conn = Bunny.new
|
15
|
-
conn.start
|
16
|
-
conn.create_channel
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def post(queue, message)
|
21
|
-
channel.queue(queue, durable: true).publish(message.to_json)
|
22
|
-
end
|
23
|
-
|
24
|
-
def visits_queue
|
25
|
-
"ahoy_visits"
|
26
|
-
end
|
27
|
-
|
28
|
-
def events_queue
|
29
|
-
"ahoy_events"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class FluentdStore < LogStore
|
4
|
-
def log_visit(data)
|
5
|
-
logger.post("visit", data)
|
6
|
-
end
|
7
|
-
|
8
|
-
def log_event(data)
|
9
|
-
logger.post("event", data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def logger
|
13
|
-
@logger ||= Fluent::Logger::FluentLogger.new("ahoy", host: ENV["FLUENTD_HOST"] || "localhost", port: ENV["FLUENTD_PORT"] || 24224)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class KafkaStore < LogStore
|
4
|
-
def log_visit(data)
|
5
|
-
post(visits_topic, data)
|
6
|
-
end
|
7
|
-
|
8
|
-
def log_event(data)
|
9
|
-
post(events_topic, data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def client
|
13
|
-
@client ||= begin
|
14
|
-
client = Kafka.new(
|
15
|
-
seed_brokers: ENV["KAFKA_URL"],
|
16
|
-
logger: Rails.logger
|
17
|
-
)
|
18
|
-
at_exit { client.shutdown }
|
19
|
-
client
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def producer
|
24
|
-
@producer ||= client.async_producer(delivery_interval: 3)
|
25
|
-
end
|
26
|
-
|
27
|
-
def post(topic, data)
|
28
|
-
producer.produce(data.to_json, topic: topic)
|
29
|
-
end
|
30
|
-
|
31
|
-
def visits_topic
|
32
|
-
"ahoy_visits"
|
33
|
-
end
|
34
|
-
|
35
|
-
def events_topic
|
36
|
-
"ahoy_events"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class KinesisFirehoseStore < LogStore
|
4
|
-
def log_visit(data)
|
5
|
-
post(visits_stream, data)
|
6
|
-
end
|
7
|
-
|
8
|
-
def log_event(data)
|
9
|
-
post(events_stream, data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def client
|
13
|
-
@client ||= Aws::Firehose::Client.new(credentials)
|
14
|
-
end
|
15
|
-
|
16
|
-
def post(stream, data)
|
17
|
-
client.put_record(
|
18
|
-
delivery_stream_name: stream,
|
19
|
-
record: {
|
20
|
-
data: "#{data.to_json}\n"
|
21
|
-
}
|
22
|
-
)
|
23
|
-
end
|
24
|
-
|
25
|
-
def credentials
|
26
|
-
{
|
27
|
-
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
|
28
|
-
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
|
29
|
-
region: "us-east-1"
|
30
|
-
}
|
31
|
-
end
|
32
|
-
|
33
|
-
def visits_stream
|
34
|
-
"ahoy_visits"
|
35
|
-
end
|
36
|
-
|
37
|
-
def events_stream
|
38
|
-
"ahoy_events"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class LogStore < BaseStore
|
4
|
-
def track_visit(options, &block)
|
5
|
-
data = {
|
6
|
-
id: ahoy.visit_id,
|
7
|
-
visitor_id: ahoy.visitor_id
|
8
|
-
}.merge(visit_properties.to_hash)
|
9
|
-
data[:user_id] = user.id if user
|
10
|
-
data[:started_at] = options[:started_at]
|
11
|
-
|
12
|
-
yield(data) if block_given?
|
13
|
-
|
14
|
-
log_visit(data)
|
15
|
-
end
|
16
|
-
|
17
|
-
def track_event(name, properties, options, &block)
|
18
|
-
data = {
|
19
|
-
id: options[:id],
|
20
|
-
name: name,
|
21
|
-
properties: properties,
|
22
|
-
visit_id: ahoy.visit_id,
|
23
|
-
visitor_id: ahoy.visitor_id
|
24
|
-
}
|
25
|
-
data[:user_id] = user.id if user
|
26
|
-
data[:time] = options[:time]
|
27
|
-
|
28
|
-
yield(data) if block_given?
|
29
|
-
|
30
|
-
log_event(data)
|
31
|
-
end
|
32
|
-
|
33
|
-
protected
|
34
|
-
|
35
|
-
def log_visit(data)
|
36
|
-
visit_logger.info data.to_json
|
37
|
-
end
|
38
|
-
|
39
|
-
def log_event(data)
|
40
|
-
event_logger.info data.to_json
|
41
|
-
end
|
42
|
-
|
43
|
-
# TODO disable header
|
44
|
-
def visit_logger
|
45
|
-
@visit_logger ||= ActiveSupport::Logger.new(Rails.root.join("log/visits.log"))
|
46
|
-
end
|
47
|
-
|
48
|
-
def event_logger
|
49
|
-
@event_logger ||= ActiveSupport::Logger.new(Rails.root.join("log/events.log"))
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class MongoidStore < BaseStore
|
4
|
-
def track_visit(options, &block)
|
5
|
-
@visit =
|
6
|
-
visit_model.new do |v|
|
7
|
-
v.id = binary(ahoy.visit_id)
|
8
|
-
v.visitor_id = binary(ahoy.visitor_id)
|
9
|
-
v.user = user if v.respond_to?(:user=) && user
|
10
|
-
v.started_at = options[:started_at]
|
11
|
-
end
|
12
|
-
|
13
|
-
set_visit_properties(visit)
|
14
|
-
|
15
|
-
yield(visit) if block_given?
|
16
|
-
|
17
|
-
visit.upsert
|
18
|
-
geocode(visit)
|
19
|
-
end
|
20
|
-
|
21
|
-
def track_event(name, properties, options, &block)
|
22
|
-
event =
|
23
|
-
event_model.new do |e|
|
24
|
-
e.id = binary(options[:id])
|
25
|
-
e.visit_id = binary(ahoy.visit_id)
|
26
|
-
e.user = user if e.respond_to?(:user)
|
27
|
-
e.name = name
|
28
|
-
e.properties = properties
|
29
|
-
e.time = options[:time]
|
30
|
-
end
|
31
|
-
|
32
|
-
yield(event) if block_given?
|
33
|
-
|
34
|
-
event.upsert
|
35
|
-
end
|
36
|
-
|
37
|
-
def visit
|
38
|
-
@visit ||= visit_model.where(_id: binary(ahoy.visit_id)).first if ahoy.visit_id
|
39
|
-
end
|
40
|
-
|
41
|
-
protected
|
42
|
-
|
43
|
-
def visit_model
|
44
|
-
::Visit
|
45
|
-
end
|
46
|
-
|
47
|
-
def event_model
|
48
|
-
::Ahoy::Event
|
49
|
-
end
|
50
|
-
|
51
|
-
def binary(token)
|
52
|
-
token = token.delete("-")
|
53
|
-
if defined?(::BSON)
|
54
|
-
::BSON::Binary.new(token, :uuid)
|
55
|
-
elsif defined?(::Moped::BSON)
|
56
|
-
::Moped::BSON::Binary.new(:uuid, token)
|
57
|
-
else
|
58
|
-
token
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Ahoy
|
2
|
-
module Stores
|
3
|
-
class NatsStore < LogStore
|
4
|
-
def log_visit(data)
|
5
|
-
publish(visits_subject, data)
|
6
|
-
end
|
7
|
-
|
8
|
-
def log_event(data)
|
9
|
-
publish(events_subject, data)
|
10
|
-
end
|
11
|
-
|
12
|
-
def publish(subject, data)
|
13
|
-
client.publish(subject, data.to_json)
|
14
|
-
end
|
15
|
-
|
16
|
-
def client
|
17
|
-
@client ||= begin
|
18
|
-
require "nats/io/client"
|
19
|
-
client = NATS::IO::Client.new
|
20
|
-
client.connect(servers: (ENV["NATS_URL"] || "nats://127.0.0.1:4222").split(","))
|
21
|
-
client
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def visits_subject
|
26
|
-
"ahoy_visits"
|
27
|
-
end
|
28
|
-
|
29
|
-
def events_subject
|
30
|
-
"ahoy_events"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|