ahoy_captain 0.83 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -13
  3. data/Rakefile +23 -2
  4. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +20 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +371 -0
  6. data/app/assets/javascript/ahoy_captain/controllers/filter/item_controller.js +12 -0
  7. data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
  8. data/app/assets/javascript/ahoy_captain/controllers/frame_link_controller.js +20 -0
  9. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +58 -16
  10. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +5 -0
  11. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +236 -22
  12. data/app/assets/javascript/ahoy_captain/controllers/map_controller.js +47 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +1 -1
  14. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +45 -0
  16. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +4 -2
  17. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +33 -0
  18. data/app/assets/javascript/ahoy_captain/controllers/toggle_controller.js +17 -0
  19. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  20. data/app/assets/javascript/ahoy_captain/helpers/countries.js +2261 -0
  21. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  22. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  23. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  24. data/app/components/ahoy_captain/comparison_link_component.html.erb +17 -0
  25. data/app/components/ahoy_captain/comparison_link_component.rb +44 -0
  26. data/app/components/ahoy_captain/dropdown_button_component.html.erb +5 -5
  27. data/app/components/ahoy_captain/dropdown_link_component.html.erb +5 -7
  28. data/app/components/ahoy_captain/dropdown_link_component.rb +4 -0
  29. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +50 -0
  30. data/app/components/ahoy_captain/filter/dropdown_component.rb +51 -0
  31. data/app/components/ahoy_captain/filter/modal_component.html.erb +7 -5
  32. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -21
  33. data/app/components/ahoy_captain/filter/select_component.rb +24 -9
  34. data/app/components/ahoy_captain/filter/tag_component.html.erb +8 -4
  35. data/app/components/ahoy_captain/filter/tag_component.rb +6 -30
  36. data/app/components/ahoy_captain/filter/tag_container_component.html.erb +2 -3
  37. data/app/components/ahoy_captain/filter/tag_container_component.rb +1 -8
  38. data/app/components/ahoy_captain/previous_next_component.html.erb +8 -0
  39. data/app/components/ahoy_captain/previous_next_component.rb +11 -0
  40. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  41. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  42. data/app/components/ahoy_captain/stats/container_component.html.erb +15 -0
  43. data/app/components/ahoy_captain/stats/container_component.rb +26 -0
  44. data/app/components/ahoy_captain/sticky_nav_component.html.erb +28 -33
  45. data/app/components/ahoy_captain/sticky_nav_component.rb +19 -0
  46. data/app/components/ahoy_captain/table_component.html.erb +2 -2
  47. data/app/components/ahoy_captain/table_component.rb +15 -3
  48. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  49. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  50. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  51. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  52. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  53. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  54. data/app/components/ahoy_captain/tables/headers/header_component.html.erb +1 -1
  55. data/app/components/ahoy_captain/tables/headers/header_component.rb +4 -0
  56. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  57. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  58. data/app/components/ahoy_captain/tables/rows/row_component.rb +2 -3
  59. data/app/components/ahoy_captain/tile_component.html.erb +21 -10
  60. data/app/components/ahoy_captain/tile_component.rb +10 -2
  61. data/app/components/ahoy_captain/tooltip_component.html.erb +2 -2
  62. data/app/controllers/ahoy_captain/application_controller.rb +7 -16
  63. data/app/controllers/ahoy_captain/exports_controller.rb +1 -2
  64. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  65. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  66. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  67. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +1 -1
  68. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -1
  69. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +4 -4
  70. data/app/controllers/ahoy_captain/filters/sources_controller.rb +1 -1
  71. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  72. data/app/controllers/ahoy_captain/locations/cities_controller.rb +22 -0
  73. data/app/controllers/ahoy_captain/locations/countries_controller.rb +22 -0
  74. data/app/controllers/ahoy_captain/locations/maps_controller.rb +24 -0
  75. data/app/controllers/ahoy_captain/locations/regions_controller.rb +22 -0
  76. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  77. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  78. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -1
  79. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  80. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  81. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  82. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +1 -10
  83. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  84. data/app/helpers/ahoy_captain/application_helper.rb +60 -3
  85. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  86. data/app/models/ahoy_captain/filter_parser.rb +82 -0
  87. data/app/models/ahoy_captain/range_from_params.rb +78 -0
  88. data/app/models/ahoy_captain/rangeable.rb +0 -3
  89. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  90. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  91. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  92. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -54
  93. data/app/presenters/ahoy_captain/goals_presenter.rb +3 -2
  94. data/app/queries/ahoy_captain/application_query.rb +74 -10
  95. data/app/queries/ahoy_captain/event_query.rb +7 -2
  96. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  97. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  98. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  99. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  100. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  101. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  102. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  103. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  104. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +3 -3
  105. data/app/queries/ahoy_captain/visit_query.rb +1 -2
  106. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  107. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  108. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  109. data/app/views/ahoy_captain/devices/_table.html.erb +1 -4
  110. data/app/views/ahoy_captain/funnels/show.html.erb +5 -2
  111. data/app/views/ahoy_captain/goals/index.html.erb +1 -4
  112. data/app/views/ahoy_captain/layouts/application.html.erb +3 -4
  113. data/app/views/ahoy_captain/layouts/shared/_tile_loader.html.erb +12 -0
  114. data/app/views/ahoy_captain/locations/maps/show.html.erb +3 -0
  115. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  116. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  117. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  118. data/app/views/ahoy_captain/realtimes/show.html.erb +1 -1
  119. data/app/views/ahoy_captain/roots/_filters.html.erb +80 -0
  120. data/app/views/ahoy_captain/roots/show.html.erb +113 -122
  121. data/app/views/ahoy_captain/stats/base/index.html.erb +37 -8
  122. data/app/views/ahoy_captain/stats/show.html.erb +14 -56
  123. data/config/routes.rb +9 -3
  124. data/lib/ahoy_captain/ahoy/event_methods.rb +21 -14
  125. data/lib/ahoy_captain/ahoy/visit_methods.rb +1 -1
  126. data/lib/ahoy_captain/configuration.rb +18 -7
  127. data/lib/ahoy_captain/engine.rb +21 -0
  128. data/lib/ahoy_captain/filter_configuration/filter.rb +16 -0
  129. data/lib/ahoy_captain/filter_configuration/filter_collection.rb +48 -0
  130. data/lib/ahoy_captain/filters_configuration.rb +77 -0
  131. data/lib/ahoy_captain/goals.rb +1 -1
  132. data/lib/ahoy_captain/predicate_label.rb +7 -0
  133. data/lib/ahoy_captain/version.rb +1 -1
  134. data/lib/ahoy_captain.rb +8 -1
  135. data/lib/generators/ahoy_captain/templates/config.rb.tt +32 -0
  136. metadata +149 -22
  137. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +0 -15
  138. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -20
  139. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +0 -65
  140. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +0 -3
  141. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +0 -9
  142. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +0 -6
  143. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +0 -9
  144. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +0 -5
  145. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +0 -12
  146. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +0 -11
  147. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +0 -12
  148. data/app/controllers/ahoy_captain/cities_controller.rb +0 -20
  149. data/app/controllers/ahoy_captain/countries_controller.rb +0 -20
  150. data/app/controllers/ahoy_captain/regions_controller.rb +0 -20
  151. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html+details.erb +0 -0
  152. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html.erb +0 -0
  153. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html+details.erb +0 -0
  154. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html.erb +0 -0
  155. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html+details.erb +0 -0
  156. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html.erb +0 -0
@@ -0,0 +1,82 @@
1
+ module AhoyCaptain
2
+ class FilterParser
3
+ FILTER_MENU_MAX_SIZE = 2
4
+ class Item
5
+ attr_accessor :name, :column, :description, :values, :predicate, :url, :modal, :label
6
+
7
+ def title
8
+ column.titleize
9
+ end
10
+ end
11
+
12
+ def self.parse(request)
13
+ new(request).tap do |instance|
14
+ instance.parse
15
+ end
16
+ end
17
+
18
+ delegate_missing_to :@items
19
+
20
+ def initialize(request)
21
+ @request = request
22
+ @params = @request.params
23
+ @filter_params = @request.params[:q] || {}
24
+ @items = {}
25
+ end
26
+
27
+ def parse
28
+ @filter_params.each do |key, values|
29
+ next if ::AhoyCaptain.event.ransackable_scopes.include?(key.to_sym)
30
+
31
+ item = build_item(key, values)
32
+ @items[key] = item
33
+ end
34
+
35
+ @items
36
+ end
37
+
38
+ private
39
+
40
+ def build_item(key, values)
41
+ item = Item.new
42
+ item.values = Array(values)
43
+
44
+ item.predicate = Ransack::Predicate.detect_and_strip_from_string!(key.dup)
45
+ item.column = key.delete_suffix("_#{item.predicate}")
46
+ modal_name = AhoyCaptain.config.filters.detect { |_, filters| filters.include?(item.column) }
47
+ if modal_name
48
+ item.modal = modal_name[1].modal_name
49
+ end
50
+
51
+ label = if item.column == "goal"
52
+ item.values.map { |value| AhoyCaptain.config.goals[value].title }
53
+ else
54
+ item.values
55
+ end.to_sentence(last_word_connector: " or ")
56
+
57
+ item.label = if key.start_with?("properties.")
58
+ item.modal = "customPropertyFilterModal"
59
+ item.column = item.column.dup.delete_prefix("properties.")
60
+ label
61
+ else
62
+ label
63
+ end
64
+
65
+ item.description = "#{item.label} #{::AhoyCaptain::PredicateLabel[item.predicate]} #{label}"
66
+ item.url = build_url(key, values)
67
+ item
68
+ end
69
+
70
+ def build_url(name, values)
71
+ search_params = @request.query_parameters.deep_dup
72
+ if search_params["q"][name].is_a?(Array)
73
+ search_params["q"][name] = search_params["q"][name] - Array(values)
74
+ else
75
+ search_params["q"].delete(name)
76
+ end
77
+
78
+ @request.path + "?" + search_params.to_query
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,78 @@
1
+ module AhoyCaptain
2
+ class RangeFromParams
3
+ def self.from_params(params)
4
+ compare = ComparisonMode.new(params)
5
+ 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 }
6
+ end
7
+
8
+ attr_reader :params, :range
9
+ def initialize(period: AhoyCaptain.config.ranges.default, start_date: nil, end_date: nil, date: nil, comparison: false, raw: {})
10
+ @period = period || AhoyCaptain.config.ranges.default
11
+ @start_date = start_date
12
+ @end_date = end_date
13
+ @date = date
14
+ @range = nil
15
+ @comparison = comparison
16
+ @raw = raw
17
+ end
18
+
19
+ def build
20
+ if (@start_date.present? && @end_date.present?) || @date.present?
21
+ if @date
22
+ @start_date ||= @date.to_datetime.beginning_of_day
23
+ @end_date ||= @date.to_datetime.end_of_day
24
+ end
25
+
26
+ custom = [@start_date.to_datetime, @end_date.to_datetime].sort
27
+ duration = (custom[1].to_date - custom[0].to_date)
28
+
29
+ # if AhoyCaptain.config.ranges.max && (duration.days <= AhoyCaptain.config.ranges.max)
30
+ @range = Range.new(custom[0].utc, custom[1].utc)
31
+ return self
32
+ # end
33
+ end
34
+
35
+ @range = Range.new(selected_period[0], selected_period[1])
36
+ self
37
+ end
38
+
39
+ def starts_at
40
+ Time.at(@range.min)
41
+ end
42
+
43
+ def ends_at
44
+ if realtime?
45
+ Time.current
46
+ else
47
+ Time.at(@range.max)
48
+ end
49
+ end
50
+
51
+ def realtime?
52
+ @range.end.nil?
53
+ end
54
+
55
+ def custom?
56
+ @raw[:start_date].present? && @raw[:end_date].present?
57
+ end
58
+
59
+ def [](value)
60
+ if value == 0
61
+ starts_at
62
+ elsif value == 1
63
+ ends_at
64
+ else
65
+ raise NoMethodError
66
+ end
67
+ end
68
+
69
+ # return an integer-based range which works with step
70
+ def numeric
71
+ @numeric ||= Range.new(starts_at.to_i, ends_at.to_i)
72
+ end
73
+
74
+ def selected_period
75
+ AhoyCaptain.config.ranges.for(@period) || AhoyCaptain.config.ranges.default
76
+ end
77
+ end
78
+ 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
@@ -16,7 +16,7 @@ module AhoyCaptain
16
16
  queries = {
17
17
  totals: @event_query.select("count(distinct(#{AhoyCaptain.event.table_name}.visit_id)) as unique_visits, '_internal_total_visits_' as name, count(distinct #{AhoyCaptain.event.table_name}.id) as total_events, 0 as sort_order")
18
18
  }
19
- selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr from totals"]
19
+ selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr, '' as goal_id from totals"]
20
20
  last_goal = nil
21
21
  map = {}.with_indifferent_access
22
22
 
@@ -27,9 +27,10 @@ module AhoyCaptain
27
27
  "'#{goal.id}' as name",
28
28
  "count(distinct #{AhoyCaptain.event.table_name}.id) as total_events",
29
29
  "#{index + 1} as sort_order",
30
+ "'#{goal.id}' as goal_id"
30
31
  ]
31
32
  ).merge(goal.event_query.call).group("#{AhoyCaptain.event.table_name}.name")
32
- selects << ["SELECT unique_visits, name, total_events, sort_order, 0::decimal as cr from #{goal.id}"]
33
+ selects << ["SELECT unique_visits, name, total_events, sort_order, 0::decimal as cr, '#{goal.id}' as goal_id from #{goal.id}"]
33
34
  map[goal.id] = goal
34
35
  last_goal = goal
35
36
  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