ahoy_captain 0.91 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -4
  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_modal_controller.js +45 -0
  7. data/app/assets/javascript/ahoy_captain/controllers/frame_link_controller.js +20 -0
  8. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +58 -16
  9. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +5 -0
  10. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +235 -21
  11. data/app/assets/javascript/ahoy_captain/controllers/map_controller.js +47 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +1 -1
  13. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  14. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +45 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +4 -2
  16. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +33 -0
  17. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  18. data/app/assets/javascript/ahoy_captain/helpers/countries.js +2261 -0
  19. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  20. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  21. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  22. data/app/components/ahoy_captain/comparison_link_component.html.erb +17 -0
  23. data/app/components/ahoy_captain/comparison_link_component.rb +44 -0
  24. data/app/components/ahoy_captain/dropdown_link_component.html.erb +2 -4
  25. data/app/components/ahoy_captain/dropdown_link_component.rb +4 -0
  26. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +8 -6
  27. data/app/components/ahoy_captain/filter/modal_component.html.erb +7 -5
  28. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -21
  29. data/app/components/ahoy_captain/filter/select_component.rb +2 -1
  30. data/app/components/ahoy_captain/filter/tag_component.html.erb +1 -1
  31. data/app/components/ahoy_captain/previous_next_component.html.erb +8 -0
  32. data/app/components/ahoy_captain/previous_next_component.rb +11 -0
  33. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  34. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  35. data/app/components/ahoy_captain/stats/container_component.html.erb +13 -6
  36. data/app/components/ahoy_captain/stats/container_component.rb +15 -1
  37. data/app/components/ahoy_captain/sticky_nav_component.html.erb +27 -20
  38. data/app/components/ahoy_captain/sticky_nav_component.rb +11 -0
  39. data/app/components/ahoy_captain/table_component.rb +13 -4
  40. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  41. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  42. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  43. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  44. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  45. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  46. data/app/components/ahoy_captain/tables/headers/header_component.rb +4 -0
  47. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  48. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  49. data/app/components/ahoy_captain/tables/rows/row_component.rb +0 -1
  50. data/app/components/ahoy_captain/tile_component.html.erb +19 -9
  51. data/app/components/ahoy_captain/tile_component.rb +9 -1
  52. data/app/controllers/ahoy_captain/application_controller.rb +7 -16
  53. data/app/controllers/ahoy_captain/exports_controller.rb +1 -2
  54. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  55. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  56. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  57. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +1 -1
  58. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -1
  59. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +4 -4
  60. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  61. data/app/controllers/ahoy_captain/locations/cities_controller.rb +22 -0
  62. data/app/controllers/ahoy_captain/locations/countries_controller.rb +22 -0
  63. data/app/controllers/ahoy_captain/locations/maps_controller.rb +24 -0
  64. data/app/controllers/ahoy_captain/locations/regions_controller.rb +22 -0
  65. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  66. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  67. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -1
  68. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
  69. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
  70. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  71. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +1 -10
  72. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -1
  73. data/app/helpers/ahoy_captain/application_helper.rb +33 -9
  74. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  75. data/app/models/ahoy_captain/filter_parser.rb +33 -18
  76. data/app/models/ahoy_captain/range_from_params.rb +78 -0
  77. data/app/models/ahoy_captain/rangeable.rb +0 -3
  78. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  79. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  80. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  81. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -54
  82. data/app/queries/ahoy_captain/application_query.rb +74 -10
  83. data/app/queries/ahoy_captain/event_query.rb +7 -2
  84. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  85. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  86. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  87. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  88. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  89. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  90. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  91. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  92. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +2 -2
  93. data/app/queries/ahoy_captain/visit_query.rb +1 -2
  94. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  95. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  96. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  97. data/app/views/ahoy_captain/devices/_table.html.erb +1 -4
  98. data/app/views/ahoy_captain/goals/index.html.erb +1 -4
  99. data/app/views/ahoy_captain/layouts/application.html.erb +0 -1
  100. data/app/views/ahoy_captain/layouts/shared/_tile_loader.html.erb +12 -0
  101. data/app/views/ahoy_captain/locations/maps/show.html.erb +3 -0
  102. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  103. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  104. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  105. data/app/views/ahoy_captain/roots/_filters.html.erb +47 -1
  106. data/app/views/ahoy_captain/roots/show.html.erb +109 -48
  107. data/app/views/ahoy_captain/stats/base/index.html.erb +37 -8
  108. data/app/views/ahoy_captain/stats/show.html.erb +14 -11
  109. data/config/routes.rb +9 -3
  110. data/lib/ahoy_captain/ahoy/event_methods.rb +12 -14
  111. data/lib/ahoy_captain/configuration.rb +2 -1
  112. data/lib/ahoy_captain/engine.rb +4 -0
  113. data/lib/ahoy_captain/filters_configuration.rb +10 -6
  114. data/lib/ahoy_captain/version.rb +1 -1
  115. data/lib/ahoy_captain.rb +7 -1
  116. data/lib/generators/ahoy_captain/templates/config.rb.tt +7 -0
  117. metadata +137 -22
  118. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +0 -18
  119. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -20
  120. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +0 -65
  121. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +0 -3
  122. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +0 -9
  123. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +0 -6
  124. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +0 -9
  125. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +0 -5
  126. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +0 -12
  127. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +0 -11
  128. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +0 -20
  129. data/app/controllers/ahoy_captain/cities_controller.rb +0 -20
  130. data/app/controllers/ahoy_captain/countries_controller.rb +0 -20
  131. data/app/controllers/ahoy_captain/regions_controller.rb +0 -20
  132. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html+details.erb +0 -0
  133. /data/app/views/ahoy_captain/{cities → locations/cities}/index.html.erb +0 -0
  134. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html+details.erb +0 -0
  135. /data/app/views/ahoy_captain/{countries → locations/countries}/index.html.erb +0 -0
  136. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html+details.erb +0 -0
  137. /data/app/views/ahoy_captain/{regions → locations/regions}/index.html.erb +0 -0
@@ -1,7 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class BaseController < ApplicationController
4
- include Rangeable
5
4
 
6
5
  INTERVAL_PERIOD = {
7
6
  "realtime" => ["minute"],
@@ -12,8 +11,18 @@ module AhoyCaptain
12
11
  }
13
12
 
14
13
  INTERVALS = ["minute", "hour", "day", "week", "month"]
14
+
15
15
  private
16
16
 
17
+ helper_method :metric_type
18
+ def metric_type(stats)
19
+ if compare_mode?
20
+ stats.current.values.first.try(:class) || stats.compared_to.values.first.try(:class)
21
+ else
22
+ stats.values.first.class
23
+ end
24
+ end
25
+
17
26
  helper_method :selected_interval
18
27
  def selected_interval
19
28
  if params[:interval].in?(INTERVALS)
@@ -32,11 +41,13 @@ module AhoyCaptain
32
41
  # assume we're in a realtime
33
42
  return INTERVAL_PERIOD["realtime"][0]
34
43
  end
35
- diff = (range[1] - range[0]).seconds.in_days.to_i
36
- if diff > 30
44
+ diff = (range[1] - range[0]).seconds.in_days
45
+ if diff >= 31
37
46
  "month"
38
- elsif diff > 0
47
+ elsif diff > 1
39
48
  "day"
49
+ elsif diff == 1
50
+ "hour"
40
51
  else
41
52
  "hour"
42
53
  end
@@ -49,7 +60,7 @@ module AhoyCaptain
49
60
 
50
61
  diff = (range[1] - range[0]).seconds.in_days
51
62
 
52
- if diff == 0
63
+ if diff < 1
53
64
  INTERVAL_PERIOD["day"]
54
65
  elsif diff <= 7
55
66
  INTERVAL_PERIOD["7d"]
@@ -62,6 +73,76 @@ module AhoyCaptain
62
73
  INTERVAL_PERIOD["month"]
63
74
  end
64
75
  end
76
+
77
+ def lazy_window(result, value = 0, base = nil)
78
+ if result.is_a?(AhoyCaptain::LazyComparableQuery::LazyComparison)
79
+ result.result.current = lazy_window(result.result.current, value, range)
80
+ result.result.compared_to = lazy_window(result.result.compared_to, value, result.compare_range)
81
+ return result.result
82
+ end
83
+
84
+ base ||= range
85
+ window = window_for(selected_interval, result.keys[0].class, base.numeric)
86
+
87
+ window.each do |item|
88
+ if result.key?(item)
89
+ next
90
+ end
91
+
92
+ result[item] ||= value
93
+ end
94
+
95
+ transform = interval_label_transformation(selected_interval)
96
+
97
+ if transform
98
+ result.transform_keys! { |key| key.strftime(transform) }
99
+ end
100
+
101
+ result
102
+ end
103
+
104
+ def interval_label_transformation(interval)
105
+ return nil
106
+ if interval == 'hour'
107
+ return '%H:%M %p'
108
+ end
109
+
110
+ nil
111
+ end
112
+
113
+ # base should be a range
114
+ def window_for(interval, type, base = nil)
115
+ function = case type.to_s
116
+ when 'Date', 'NilClass'
117
+ ->(value) {
118
+ date = Time.at(value).utc
119
+ if interval == 'month'
120
+ date.change(day: 1)
121
+ elsif interval == 'week'
122
+ date.beginning_of_week
123
+ elsif interval == 'day'
124
+ date.beginning_of_day
125
+ elsif interval == 'hour'
126
+ date.beginning_of_hour
127
+ elsif interval == 'minute'
128
+ date.beginning_of_minute
129
+ else
130
+ abort
131
+ end.to_date
132
+ }
133
+ when 'DateTime'
134
+ ->(value) { Time.at(value).utc.change(sec: 0) }
135
+ when 'ActiveSupport::TimeWithZone'
136
+ ->(value) { Time.at(value).utc }
137
+ else
138
+ raise ArgumentError
139
+ end
140
+
141
+ base
142
+ .step(1.send(interval))
143
+ .to_a
144
+ .map { |value| function.call(value) }
145
+ end
65
146
  end
66
147
  end
67
148
  end
@@ -4,7 +4,7 @@ module AhoyCaptain
4
4
  # @todo: this is lazy
5
5
  def index
6
6
  @stats = AhoyCaptain::Stats::BounceRatesQuery.call(params)
7
- @stats = @stats.group_by_period(selected_interval, "daily_bounce_rate.date").average("bounce_rate")
7
+ @stats = lazy_window(@stats.with_lazy_comparison(compare_mode?).group_by_period(selected_interval, "daily_bounce_rate.date").average("bounce_rate"))
8
8
  @label = "Bounce Rate"
9
9
  end
10
10
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class TotalPageviewsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::TotalPageviewsQuery.call(params).group_by_period(selected_interval, :time).count
5
+ @stats = lazy_window(AhoyCaptain::Stats::TotalPageviewsQuery.call(params).with_lazy_comparison(compare_mode?).group_by_period(selected_interval, :time).count, 0)
6
6
  @label = "Visitors"
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class TotalVisitsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::TotalVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
5
+ @stats = lazy_window(AhoyCaptain::Stats::TotalVisitorsQuery.call(params).with_lazy_comparison(compare_mode?).group_by_period(selected_interval, :started_at).count)
6
6
  @label = "Visitors"
7
7
  end
8
8
  end
@@ -2,7 +2,8 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class UniqueVisitorsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
5
+ @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).with_lazy_comparison(compare_mode?).group_by_period(selected_interval, :started_at).count
6
+ @stats = lazy_window(@stats)
6
7
  @label = "Visitors"
7
8
  end
8
9
  end
@@ -1,19 +1,10 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class ViewsPerVisitsController < BaseController
4
- # @todo: make me a window func
5
4
  def index
6
- @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit)
7
- if range[1]
8
- (range[0].to_date..range[1].to_date).to_a.each do |date|
9
- unless @stats.key?(date.to_date)
10
- @stats[date.to_date] = 2
11
- end
12
- end
13
- end
5
+ @stats = lazy_window(AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).with_lazy_comparison(compare_mode?).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit))
14
6
 
15
7
  @label = "Views"
16
-
17
8
  end
18
9
  end
19
10
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class VisitDurationsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).group_by_period(selected_interval, 'started_at').average(:duration)
5
+ @stats = lazy_window(AhoyCaptain::Stats::VisitDurationQuery.call(params).with_lazy_comparison(compare_mode?).group_by_period(selected_interval, 'started_at').average(:duration))
6
6
  @label = "Duration"
7
7
  end
8
8
  end
@@ -2,8 +2,35 @@ module AhoyCaptain
2
2
  module ApplicationHelper
3
3
  include Pagy::Frontend
4
4
 
5
- # params that are coerced from the ApplicationController#act_like_an_spa
6
- SPECIAL_PARAMS = [:campaigns_type, :devices_type]
5
+ def current_property_filter
6
+ return nil unless params[:q]
7
+
8
+ prop = params[:q].to_unsafe_h.detect { |key, _| key.starts_with?("properties.") }
9
+ if prop
10
+ key = prop[0].dup
11
+ Ransack::Predicate.detect_and_strip_from_string!(key)
12
+ { key: key, value: prop[1] }
13
+
14
+ else
15
+ nil
16
+ end
17
+ end
18
+
19
+ def stats_container(value, url, label, formatter, selected = false)
20
+ if value.is_a?(AhoyCaptain::ComparableQuery::Comparison)
21
+ ::AhoyCaptain::Stats::ComparableContainerComponent.new(url, label, value, formatter, selected, compare_mode?)
22
+ else
23
+ ::AhoyCaptain::Stats::ContainerComponent.new(url, label, value, formatter, selected)
24
+ end
25
+ end
26
+
27
+ def number_to_duration(duration)
28
+ if duration
29
+ "#{duration.in_minutes.to_i}M #{duration.parts[:seconds].to_i}S"
30
+ else
31
+ "0M 0S"
32
+ end
33
+ end
7
34
 
8
35
  def ahoy_captain_importmap_tags(entry_point = "application", shim: true)
9
36
  safe_join [
@@ -19,10 +46,6 @@ module AhoyCaptain
19
46
  request.query_parameters
20
47
  end
21
48
 
22
- def special_params
23
- params.to_unsafe_h.slice(*SPECIAL_PARAMS)
24
- end
25
-
26
49
  # gets put into the form as a hidden field
27
50
  #
28
51
  def non_filter_ransack_params
@@ -31,9 +54,11 @@ module AhoyCaptain
31
54
  :start_date,
32
55
  :end_date,
33
56
  :period,
34
- :interval
57
+ :interval,
58
+ :comparison,
35
59
  ]
36
60
 
61
+ # properties stuff falls into current_property_filter
37
62
  ransack = [:goal]
38
63
 
39
64
  map.each do |key|
@@ -45,8 +70,7 @@ module AhoyCaptain
45
70
  ransack.each do |key|
46
71
  Ransack.predicates.keys.each do |predicate|
47
72
  if value = params.dig(:q, "#{key}_#{predicate}")
48
- other_params[:q] ||= {}
49
- other_params[:q]["#{key}_#{predicate}"] = value
73
+ other_params["q[#{key}_#{predicate}]"] = value
50
74
  end
51
75
  end
52
76
  end
@@ -0,0 +1,72 @@
1
+ module AhoyCaptain
2
+ class ComparisonMode
3
+ VALID_COMPARISONS = %w{previous year true}
4
+
5
+ include RangeOptions
6
+ include Rangeable
7
+
8
+ attr_reader :params
9
+ def initialize(params)
10
+ @params = params
11
+ end
12
+
13
+ # can't compare realtime
14
+ def enabled?(strict = true)
15
+ comparing = (params[:comparison].in?(VALID_COMPARISONS) || custom_compare?)
16
+ if strict
17
+ comparing && !range.realtime?
18
+ else
19
+ comparing
20
+ end
21
+ end
22
+
23
+ def label
24
+ if custom_compare?
25
+ return "Custom period"
26
+ end
27
+
28
+ if type == :true || type == :previous
29
+ "Previous period"
30
+ elsif type == :year
31
+ "Year-over-year"
32
+ else
33
+ raise ArgumentError
34
+ end
35
+ end
36
+
37
+ def compared_to_range
38
+ return custom_compare if custom_compare?
39
+
40
+ if type == :true || type == :previous
41
+ RangeFromParams.new(period: nil, start_date: (range[0] - (range[1] - range[0])).utc.to_s, end_date: range[0].utc.to_s).build
42
+ elsif type == :year
43
+ RangeFromParams.new(period: nil, start_date: range[0].change(year: range[0].year - 1).utc.to_s, end_date: range[1].change(year: range[1].year - 1).utc.to_s).build
44
+ else
45
+ raise ArgumentError
46
+ end
47
+ end
48
+
49
+ def type
50
+ return params[:comparison].to_sym if params[:comparison].in?(VALID_COMPARISONS)
51
+ return :custom if custom_compare?
52
+
53
+ nil
54
+ end
55
+
56
+ def match_to
57
+ return params[:compare_to].to_sym if params[:compare_to].in?(%w{dow date})
58
+
59
+ nil
60
+ end
61
+
62
+ def custom_compare
63
+ return nil unless (params[:compare_to_start_date].present? && params[:compare_to_end_date].present?)
64
+
65
+ RangeFromParams.new(period: nil, start_date: params[:compare_to_start_date], end_date: params[:compare_to_end_date]).build
66
+ end
67
+
68
+ def custom_compare?
69
+ custom_compare
70
+ end
71
+ end
72
+ end
@@ -26,24 +26,9 @@ module AhoyCaptain
26
26
 
27
27
  def parse
28
28
  @filter_params.each do |key, values|
29
- item = Item.new
30
-
31
- item.values = Array(values)
32
- item.predicate = Ransack::Predicate.detect_and_strip_from_string!(key.dup)
33
- item.column = key.delete_suffix("_#{item.predicate}")
34
- modal_name = AhoyCaptain.config.filters.detect { |_, filters| filters.include?(item.column) }[1].modal_name
35
- if modal_name
36
- item.modal = modal_name
37
- end
38
-
39
- label = if item.column == "goal"
40
- AhoyCaptain.config.goals[values].title
41
- else
42
- item.values.to_sentence(last_word_connector: " or ")
43
- end
44
- item.label = label
45
- item.description = "#{item.column.titleize} #{::AhoyCaptain::PredicateLabel[item.predicate]} #{label}"
46
- item.url = build_url(key, values)
29
+ next if ::AhoyCaptain.event.ransackable_scopes.include?(key.to_sym)
30
+
31
+ item = build_item(key, values)
47
32
  @items[key] = item
48
33
  end
49
34
 
@@ -52,6 +37,36 @@ module AhoyCaptain
52
37
 
53
38
  private
54
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
+
55
70
  def build_url(name, values)
56
71
  search_params = @request.query_parameters.deep_dup
57
72
  if search_params["q"][name].is_a?(Array)
@@ -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