ahoy_captain 0.9 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -4
  3. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +19 -3
  4. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +8 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +341 -0
  6. data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
  7. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +58 -16
  8. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +5 -0
  9. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +170 -19
  10. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +0 -1
  11. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +46 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +4 -2
  14. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +9 -0
  15. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  16. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  17. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  18. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  19. data/app/components/ahoy_captain/comparison_link_component.rb +40 -0
  20. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +8 -6
  21. data/app/components/ahoy_captain/filter/modal_component.html.erb +7 -5
  22. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -21
  23. data/app/components/ahoy_captain/filter/select_component.rb +2 -1
  24. data/app/components/ahoy_captain/filter/tag_component.html.erb +1 -1
  25. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  26. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  27. data/app/components/ahoy_captain/stats/container_component.html.erb +13 -6
  28. data/app/components/ahoy_captain/stats/container_component.rb +16 -1
  29. data/app/components/ahoy_captain/sticky_nav_component.html.erb +7 -4
  30. data/app/components/ahoy_captain/sticky_nav_component.rb +3 -0
  31. data/app/components/ahoy_captain/table_component.rb +13 -4
  32. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  33. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  34. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  35. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  36. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  37. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  38. data/app/components/ahoy_captain/tables/headers/header_component.rb +4 -0
  39. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  40. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  41. data/app/components/ahoy_captain/tables/rows/row_component.rb +0 -1
  42. data/app/components/ahoy_captain/tile_component.html.erb +19 -9
  43. data/app/components/ahoy_captain/tile_component.rb +2 -1
  44. data/app/controllers/ahoy_captain/application_controller.rb +7 -16
  45. data/app/controllers/ahoy_captain/exports_controller.rb +1 -2
  46. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  47. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  48. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  49. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +1 -1
  50. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -1
  51. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +4 -4
  52. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  53. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  54. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  55. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -1
  56. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  57. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  58. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  59. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +1 -10
  60. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  61. data/app/helpers/ahoy_captain/application_helper.rb +35 -9
  62. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  63. data/app/models/ahoy_captain/filter_parser.rb +33 -18
  64. data/app/models/ahoy_captain/range_from_params.rb +75 -0
  65. data/app/models/ahoy_captain/rangeable.rb +0 -3
  66. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  67. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  68. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  69. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -54
  70. data/app/queries/ahoy_captain/application_query.rb +74 -10
  71. data/app/queries/ahoy_captain/event_query.rb +7 -2
  72. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  73. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  74. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  75. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  76. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  77. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  78. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  79. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  80. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +3 -3
  81. data/app/queries/ahoy_captain/visit_query.rb +1 -2
  82. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  83. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  84. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  85. data/app/views/ahoy_captain/devices/_table.html.erb +1 -4
  86. data/app/views/ahoy_captain/goals/index.html.erb +1 -4
  87. data/app/views/ahoy_captain/layouts/application.html.erb +0 -1
  88. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  89. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  90. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  91. data/app/views/ahoy_captain/roots/_filters.html.erb +47 -1
  92. data/app/views/ahoy_captain/roots/show.html.erb +60 -31
  93. data/app/views/ahoy_captain/stats/base/index.html.erb +36 -8
  94. data/app/views/ahoy_captain/stats/show.html.erb +8 -10
  95. data/config/routes.rb +2 -0
  96. data/lib/ahoy_captain/ahoy/event_methods.rb +13 -15
  97. data/lib/ahoy_captain/configuration.rb +7 -6
  98. data/lib/ahoy_captain/engine.rb +4 -0
  99. data/lib/ahoy_captain/filters_configuration.rb +5 -1
  100. data/lib/ahoy_captain/version.rb +1 -1
  101. data/lib/ahoy_captain.rb +6 -1
  102. data/lib/generators/ahoy_captain/templates/config.rb.tt +7 -0
  103. metadata +35 -12
  104. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -20
  105. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +0 -65
  106. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +0 -3
  107. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +0 -9
  108. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +0 -6
  109. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +0 -9
  110. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +0 -5
  111. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +0 -12
  112. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +0 -11
  113. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +0 -20
@@ -0,0 +1,75 @@
1
+ module AhoyCaptain
2
+ class RangeFromParams
3
+ def self.from_params(params)
4
+
5
+ compare = ComparisonMode.new(params)
6
+ new(period: params[:period], start_date: params[:start_date], end_date: params[:end_date], date: params[:date], comparison: compare.enabled?(false), raw: params).tap { |instance| instance.build }
7
+ end
8
+
9
+ attr_reader :params, :range
10
+ def initialize(period: AhoyCaptain.config.ranges.default, start_date: nil, end_date: nil, date: nil, comparison: false, raw: {})
11
+ @period = period || AhoyCaptain.config.ranges.default
12
+ @start_date = start_date
13
+ @end_date = end_date
14
+ @date = date
15
+ @range = nil
16
+ @comparison = comparison
17
+ @raw = raw
18
+ end
19
+
20
+ def build
21
+ if (@start_date.present? && @end_date.present?) || @date.present?
22
+ if @date
23
+ @start_date ||= @date.to_datetime.beginning_of_day
24
+ @end_date ||= @date.to_datetime.end_of_day
25
+ end
26
+
27
+ custom = [@start_date.to_datetime, @end_date.to_datetime].sort
28
+ duration = (custom[1].to_date - custom[0].to_date)
29
+
30
+ # if AhoyCaptain.config.ranges.max && (duration.days <= AhoyCaptain.config.ranges.max)
31
+ @range = Range.new(custom[0].utc, custom[1].utc)
32
+ return self
33
+ # end
34
+ end
35
+
36
+ @range = Range.new(selected_period[0], selected_period[1])
37
+ self
38
+ end
39
+
40
+ def starts_at
41
+ Time.at(@range.min)
42
+ end
43
+
44
+ def ends_at
45
+ if realtime?
46
+ Time.current
47
+ else
48
+ Time.at(@range.max)
49
+ end
50
+ end
51
+
52
+ def realtime?
53
+ @range.end.nil?
54
+ end
55
+
56
+ def [](value)
57
+ if value == 0
58
+ starts_at
59
+ elsif value == 1
60
+ ends_at
61
+ else
62
+ raise NoMethodError
63
+ end
64
+ end
65
+
66
+ # return an integer-based range which works with step
67
+ def numeric
68
+ @numeric ||= Range.new(starts_at.to_i, ends_at.to_i)
69
+ end
70
+
71
+ def selected_period
72
+ AhoyCaptain.config.ranges.for(@period) || AhoyCaptain.config.ranges.default
73
+ end
74
+ end
75
+ end
@@ -1,10 +1,7 @@
1
1
  module AhoyCaptain
2
2
  module Rangeable
3
- include AhoyCaptain::RangeOptions
4
-
5
3
  def period
6
4
  params[:period] || AhoyCaptain.config.ranges.default
7
5
  end
8
-
9
6
  end
10
7
  end
@@ -0,0 +1,19 @@
1
+ module AhoyCaptain
2
+ module CompareMode
3
+ def self.included(klass)
4
+ if klass < ActionController::Base
5
+ klass.helper_method :compare_mode?
6
+ klass.helper_method :comparison_label
7
+ end
8
+ end
9
+
10
+ # doesn't work for realtime and realtime doesn't need a secondary range
11
+ def compare_mode?
12
+ comparison_mode.enabled?
13
+ end
14
+
15
+ def comparison_mode
16
+ @comparison_mode ||= ComparisonMode.new(params)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module AhoyCaptain
2
+ module Limitable
3
+ private
4
+
5
+ def limit
6
+ if request.variant.include?(:details)
7
+ nil
8
+ else
9
+ if params[:limit]
10
+ params[:limit].to_i
11
+ else
12
+ 10
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,21 +1,8 @@
1
1
  module AhoyCaptain
2
2
  module RangeOptions
3
3
 
4
- def period
5
- raise NotImplementedError
6
- end
7
-
8
4
  private def range
9
- if params[:start_date].present? && params[:end_date].present?
10
- custom = [params[:start_date].to_datetime, params[:end_date].to_datetime].sort
11
- duration = (custom[1].to_date - custom[0].to_date)
12
-
13
- if AhoyCaptain.config.ranges.max && (duration.days <= AhoyCaptain.config.ranges.max)
14
- return custom
15
- end
16
- end
17
-
18
- AhoyCaptain.config.ranges.for(period)
5
+ RangeFromParams.from_params(params)
19
6
  end
20
7
  end
21
8
  end
@@ -1,6 +1,8 @@
1
1
  module AhoyCaptain
2
2
  class DashboardPresenter
3
+ include Rangeable
3
4
  include RangeOptions
5
+ include CompareMode
4
6
 
5
7
  attr_reader :params
6
8
 
@@ -9,81 +11,43 @@ module AhoyCaptain
9
11
  end
10
12
 
11
13
  def unique_visitors
12
- cached(:unique_visitors) do
13
- Stats::UniqueVisitorsQuery.call(params).count(:visitor_token)
14
- end
14
+ Stats::UniqueVisitorsQuery.call(params).with_comparison(compare_mode?).count
15
15
  end
16
16
 
17
17
  def total_visits
18
- cached(:total_visits) do
19
- Stats::TotalVisitorsQuery.call(params).count(:id)
20
- end
18
+ Stats::TotalVisitorsQuery.call(params).with_comparison(compare_mode?).count
21
19
  end
22
20
 
23
21
  def total_pageviews
24
- cached(:total_pageviews) do
25
- Stats::TotalPageviewsQuery.call(params).count(:id)
26
- end
22
+ Stats::TotalPageviewsQuery.call(params).with_comparison(compare_mode?).count
27
23
  end
28
24
 
29
25
  def views_per_visit
30
- cached(:views_per_visit) do
31
- begin
32
- result = Stats::AverageViewsPerVisitQuery.call(params).count(:id)
33
- count = (result.values.sum.to_f / result.size).round(2)
34
- if count.nan?
35
- return "0"
36
- else
37
- return count
38
- end
39
- rescue ::ActiveRecord::StatementInvalid => e
40
- if e.message.include?("PG::DivisionByZero")
41
- return "0"
42
- else
43
- raise e
44
- end
45
- end
46
- end
26
+ Stats::AverageViewsPerVisitQuery.call(params).with_comparison(compare_mode?).average("count")
47
27
  end
48
28
 
49
29
  def bounce_rate
50
- cached(:bounce_rate) do
51
- begin
52
- result = Stats::BounceRatesQuery.call(params)
53
- average = result.average("bounce_rate")
54
- if average
55
- average.round(2)
56
- else
57
- "0"
58
- end
59
- rescue ::ActiveRecord::StatementInvalid => e
60
- if e.message.include?("PG::DivisionByZero")
61
- return "0"
62
- else
63
- raise e
64
- end
65
- end
30
+ query = Stats::BounceRatesQuery.call(params)
31
+ if compare_mode?
32
+ query.with_comparison(true)
33
+ else
34
+ query.average("bounce_rate").try(:round, 2) || 0
66
35
  end
67
36
  end
68
37
 
69
38
  def visit_duration
70
- cached(:visit_duration) do
71
- result = Stats::AverageVisitDurationQuery.call(params)
72
- duration = result[0].average_visit_duration
73
- if duration
74
- "#{duration.in_minutes.to_i}M #{duration.parts[:seconds].to_i}S"
75
- else
76
- "0M 0S"
77
- end
39
+ query = Stats::AverageVisitDurationQuery.call(params)
40
+ if compare_mode?
41
+ query.with_comparison(true)
42
+ else
43
+ query[0].average_visit_duration
78
44
  end
79
45
  end
80
46
 
81
47
  private
82
48
 
83
- def cached(*names)
84
- AhoyCaptain.cache.fetch("ahoy_captain:#{names.join(":")}:#{params.permit!.except("controller", "action").to_unsafe_h.map { |k,v| "#{k}-#{v}" }.join(":")}", expire_in: AhoyCaptain.config.cache[:ttl]) do
85
- yield
86
- end
49
+ def compare_mode?
50
+ params[:comparison] != 'false'
87
51
  end
88
52
  end
89
53
  end
@@ -1,8 +1,9 @@
1
+ require_relative '../concerns/ahoy_captain/comparable_queries'
2
+
1
3
  module AhoyCaptain
2
4
  class ApplicationQuery
3
5
  # if you want to enforce returning a relation
4
6
  class_attribute :strict, default: true
5
- include Rangeable
6
7
 
7
8
  delegate_missing_to :@query
8
9
 
@@ -63,7 +64,7 @@ module AhoyCaptain
63
64
  event: (AhoyCaptain.event.ransackable_attributes + AhoyCaptain.event.ransackable_scopes).map(&:to_s),
64
65
  }
65
66
 
66
- pattern = /(?:_not_eq|_eq|_in|_not_in|_cont|_not_cont|_i_cont)$/
67
+ pattern = /(?:#{Ransack.predicates.sorted_names_with_underscores.to_h.values.join("|")})$/
67
68
  params[:q].each do |key, value|
68
69
  attribute_name = key.gsub(pattern, '')
69
70
  if type == :event && (ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key))
@@ -76,35 +77,57 @@ module AhoyCaptain
76
77
  end
77
78
  end
78
79
 
80
+ merge_params = {}
81
+ ransackable_params.each do |k,v|
82
+ transform = false
83
+ if v == AhoyCaptain.none.value
84
+ transform = true
85
+ elsif v.is_a?(Array) && v[0] == AhoyCaptain.none.value
86
+ transform = true
87
+ end
88
+
89
+ if transform
90
+ key = k.dup
91
+ ransackable_params.delete(key)
92
+ predicate = Ransack::Predicate.detect_and_strip_from_string!(key)
93
+ if predicate.include?("not")
94
+ merge_params["#{key}_not_null"] = '1'
95
+ else
96
+ merge_params["#{key}_null"] = '1'
97
+ end
98
+ end
99
+ end
100
+
101
+ ransackable_params.merge!(merge_params)
79
102
  # send the right format
80
103
  # ::Ahoy::Visit.ransack(events_time_lt: Time.now).result.to_sql
81
104
  # is not
82
105
  # ::Ahoy::Visit.ransack(events_time_lt: Time.now.to_i).result.to_sql
83
106
  if range
84
107
  if type == :event
85
- if range.size == 2
108
+ if range.realtime?
86
109
  ransackable_params['time_gt'] = range[0]
87
- ransackable_params['time_lt'] = range[1]
88
110
  ransackable_params["visit_started_at_gt"] = range[0]
89
- ransackable_params["visit_started_at_lt"] = range[1]
90
111
  else
91
112
  ransackable_params['time_gt'] = range[0]
113
+ ransackable_params['time_lt'] = range[1]
92
114
  ransackable_params["visit_started_at_gt"] = range[0]
115
+ ransackable_params["visit_started_at_lt"] = range[1]
93
116
  end
94
117
  elsif type == :visit
95
- if range.size == 2
118
+ if range.realtime?
119
+ ransackable_params["started_at_gt"] = range[0]
120
+ ransackable_params["events_time_gt"] = range[0]
121
+ else
96
122
  ransackable_params["started_at_gt"] = range[0]
97
123
  ransackable_params["started_at_lt"] = range[1]
98
124
  ransackable_params["events_time_gteq"] = range[0]
99
125
  ransackable_params["events_time_lteq"] = range[1]
100
- else
101
- ransackable_params["started_at_gt"] = range[0]
102
- ransackable_params["events_time_gt"] = range[0]
103
126
  end
104
127
  end
105
128
  end
106
129
 
107
- ransackable_params
130
+ ransackify(ransackable_params, type)
108
131
  end
109
132
 
110
133
  # merge both sets of ransackable params and ensure that they're being set on the correct association
@@ -118,5 +141,46 @@ module AhoyCaptain
118
141
  raise ArgumentError, "use ransack_params_for(type)"
119
142
  end
120
143
  end
144
+
145
+ def ransackify(query, type)
146
+ return unless query
147
+
148
+ query = query.try(:permit!).try(:to_h) unless query.is_a?(Hash)
149
+ obj = query.each_with_object({}) do |(k, v), obj|
150
+ if k.starts_with?('properties.')
151
+ field = k.split('properties.').last
152
+ operation = Ransack::Predicate.detect_and_strip_from_string!(field)
153
+
154
+ raise ArgumentError, "No valid predicate for #{field}" unless operation
155
+
156
+ prefix = type == :event ? "" : "events_"
157
+ obj[:c] ||= []
158
+
159
+ obj[:c] << {
160
+ a: {
161
+ '0' => {
162
+ name: "#{prefix}properties",
163
+ ransacker_args: field
164
+ }
165
+ },
166
+ p: operation,
167
+ v: [v]
168
+ }
169
+
170
+ else
171
+ obj[k] = v
172
+ end
173
+ end
174
+
175
+ if type == :event
176
+ return obj
177
+ else
178
+ return obj
179
+ end
180
+ end
181
+
182
+ def range
183
+ RangeFromParams.from_params(params)
184
+ end
121
185
  end
122
186
  end
@@ -1,6 +1,5 @@
1
1
  module AhoyCaptain
2
2
  class EventQuery < ApplicationQuery
3
- include Rangeable
4
3
 
5
4
  def build
6
5
  entry_pages = ransack_params_for(:event).select { |k,v| k.start_with?("entry_page") }
@@ -13,7 +12,7 @@ module AhoyCaptain
13
12
  ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }, context: shared_context
14
13
  )
15
14
 
16
- visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") }.transform_keys { |key| "visit_#{key}" }
15
+ visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") || k == :c }.transform_keys { |key| "visit_#{key}" }
17
16
  search_children = AhoyCaptain.visit.ransack(
18
17
  visit_params, context: shared_context
19
18
  )
@@ -33,5 +32,11 @@ module AhoyCaptain
33
32
 
34
33
  joined.where(shared_conditions.reduce(&:and))
35
34
  end
35
+
36
+ def page_view
37
+ @query = @query.page_view
38
+
39
+ self
40
+ end
36
41
  end
37
42
  end
@@ -1,12 +1,19 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  # pls fix
4
- class AverageViewsPerVisitQuery < ApplicationQuery
4
+ class AverageViewsPerVisitQuery < BaseQuery
5
5
  def build
6
- event_query.joins(:visit)
7
- .where(name: "$view")
8
- .group("ahoy_visits.id")
6
+ subquery = event_query.select("count(ahoy_events.visit_id) as count").where(name: "$view").group(:visit_id)
9
7
 
8
+ AhoyCaptain.event.select("count").from("(#{subquery.to_sql}) as events")
9
+ end
10
+
11
+ def self.cast_type(_column)
12
+ nil
13
+ end
14
+
15
+ def self.cast_value(_, value)
16
+ value.to_i
10
17
  end
11
18
  end
12
19
  end
@@ -1,11 +1,23 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class AverageVisitDurationQuery < ApplicationQuery
3
+ class AverageVisitDurationQuery < BaseQuery
4
4
  def build
5
5
  max_events = event_query.select("#{AhoyCaptain.event.table_name}.visit_id, max(#{AhoyCaptain.event.table_name}.time) as created_at").group("visit_id")
6
- visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at)) as average_visit_duration")
6
+ visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at))::interval as average_visit_duration")
7
7
  .joins("LEFT JOIN (#{max_events.to_sql}) as max_events ON #{AhoyCaptain.visit.table_name}.id = max_events.visit_id")
8
8
  end
9
+
10
+ def self.cast_type(value)
11
+ ActiveRecord::Type.lookup(:string)
12
+ end
13
+
14
+ def self.cast_value(_type, value)
15
+ if value.present?
16
+ ActiveSupport::Duration.parse(value)
17
+ else
18
+ ActiveSupport::Duration.parse("P0MT0S")
19
+ end
20
+ end
9
21
  end
10
22
  end
11
23
  end
@@ -0,0 +1,18 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class BaseQuery < ApplicationQuery
4
+ include ComparableQuery
5
+ include LazyComparableQuery
6
+
7
+ protected
8
+
9
+ def self.cast_type(column)
10
+ ActiveRecord::Type.lookup(::Ahoy::Visit.columns_hash[column.to_s].type)
11
+ end
12
+
13
+ def self.cast_value(type, value)
14
+ type.cast(value)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class BounceRatesQuery < ApplicationQuery
3
+ class BounceRatesQuery < BaseQuery
4
4
  def build
5
5
  total_visits = visit_query.select("date(#{AhoyCaptain.visit.table_name}.started_at) as date, count(*) as count").group("date(#{AhoyCaptain.visit.table_name}.started_at)")
6
6
  subquery = visit_query.select(:id, :started_at).joins(:events).group("#{AhoyCaptain.visit.table_name}.id, #{AhoyCaptain.visit.table_name}.started_at").having("count(#{AhoyCaptain.event.table_name}.id) = 1")
@@ -12,6 +12,20 @@ module AhoyCaptain
12
12
  ::Ahoy::Visit.with(total_visits: total_visits, single_page_visits: single_page_visits, daily_bounce_rate: daily_bounce_rate).select("bounce_rate, date").from("daily_bounce_rate")
13
13
  end
14
14
 
15
+ protected
16
+
17
+ def self.cast_type(column)
18
+ ActiveRecord::Type.lookup(:decimal)
19
+ end
20
+
21
+ def self.cast_value(type, value)
22
+ if value.blank?
23
+ return 0.to_d
24
+ end
25
+
26
+ super.round(2)
27
+ end
28
+
15
29
  end
16
30
  end
17
31
  end
@@ -1,8 +1,8 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class TotalPageviewsQuery < ApplicationQuery
3
+ class TotalPageviewsQuery < BaseQuery
4
4
  def build
5
- event_query.where(name: AhoyCaptain.config.event[:view_name])
5
+ event_query.page_view
6
6
  end
7
7
  end
8
8
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class TotalVisitorsQuery < ApplicationQuery
3
+ class TotalVisitorsQuery < BaseQuery
4
4
  def build
5
5
  visit_query.distinct.select(:id)
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class UniqueVisitorsQuery < ApplicationQuery
3
+ class UniqueVisitorsQuery < BaseQuery
4
4
  def build
5
5
  visit_query.distinct.select(:visitor_token)
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class ViewsPerVisitQuery < ApplicationQuery
3
+ class ViewsPerVisitQuery < BaseQuery
4
4
  def build
5
5
  events = event_query
6
6
  .joins(:visit)
@@ -1,15 +1,15 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
- class VisitDurationQuery < ApplicationQuery
3
+ class VisitDurationQuery < BaseQuery
4
4
  def build
5
5
  events = event_query
6
6
  .reselect("max(#{AhoyCaptain.event.table_name}.time) - min(#{AhoyCaptain.event.table_name}.time) as duration, #{AhoyCaptain.event.table_name}.visit_id")
7
7
  .group("#{AhoyCaptain.event.table_name}.visit_id")
8
8
 
9
9
  ::Ahoy::Visit
10
- .select("duration as duration, started_at")
10
+ .select("duration::duration as duration, started_at")
11
11
  .from(events, :views_per_visit_table)
12
- .joins("inner join #{AhoyCaptain.visit.table_name} on ahoy_visits.id = views_per_visit_table.visit_id")
12
+ .joins("inner join #{AhoyCaptain.visit.table_name} on #{AhoyCaptain.visit.table_name}.id = views_per_visit_table.visit_id")
13
13
  end
14
14
  end
15
15
  end
@@ -1,6 +1,5 @@
1
1
  module AhoyCaptain
2
2
  class VisitQuery < ApplicationQuery
3
- include Rangeable
4
3
 
5
4
  def build
6
5
  shared_context = Ransack::Context.for(AhoyCaptain.visit)
@@ -9,7 +8,7 @@ module AhoyCaptain
9
8
  ransack_params_for(:visit).reject { |k,v| k.start_with?("events_") }, context: shared_context
10
9
  )
11
10
  search_children = AhoyCaptain.event.ransack(
12
- ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }.transform_keys { |key| "events_#{key}" }, context: shared_context
11
+ ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") || k == :c }.transform_keys { |key| "events_#{key}" }, context: shared_context
13
12
  )
14
13
 
15
14
  shared_conditions = [search_parents, search_children].map { |search|
@@ -0,0 +1,25 @@
1
+ module AhoyCaptain
2
+ module ComparableQueries
3
+ def compare_range
4
+ @compare_range ||= begin
5
+ ComparisonMode.new(@params).compared_to_range
6
+ end
7
+ end
8
+
9
+ def range
10
+ @range ||= @query.send(:range)
11
+ end
12
+
13
+ def comparison_params
14
+ params = @params.deep_dup
15
+ params.delete("period")
16
+
17
+ params[:start_date] = compare_range[0]
18
+ params[:end_date] = compare_range[1]
19
+ params
20
+ end
21
+ end
22
+ end
23
+
24
+ require_relative './lazy_comparable_query'
25
+ require_relative './comparable_query'