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
@@ -5,12 +5,20 @@ class AhoyCaptain::TileComponent < ViewComponent::Base
5
5
  renders_one :display_links
6
6
  renders_one :details_cta
7
7
 
8
- def initialize(title: nil, wide: false)
8
+ def initialize(title: nil, wide: false, classes: "p-8 mx-4")
9
+ @classes = classes
9
10
  @title = title
10
11
  @wide = wide
11
12
  end
12
13
 
14
+ def link_to(name, url, **options)
15
+ options[:class] = "inline-block h-5 font-semibold"
16
+ options[:data] ||= {}
17
+ options[:data].merge!(controller: "frame-link")
18
+ view_context.link_to name, url, **options
19
+ end
20
+
13
21
  private
14
22
 
15
23
  attr_reader :title, :wide
16
- end
24
+ end
@@ -1,3 +1,3 @@
1
- <div class="tooltip" data-tip=<%= amount %>>
1
+ <div class="tooltip " data-tip=<%= amount %>>
2
2
  <p><%= abbreviate %></p>
3
- </div>
3
+ </div>
@@ -1,25 +1,16 @@
1
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
-
18
2
  class ApplicationController < ActionController::Base
19
3
  include Pagy::Backend
4
+ include CompareMode
5
+ include RangeOptions
6
+ include Rangeable
20
7
 
21
8
  layout 'ahoy_captain/layouts/application'
22
9
 
10
+ def period
11
+ params[:period] || AhoyCaptain.config.ranges.default
12
+ end
13
+
23
14
  # show the details frame
24
15
  before_action :use_details_frame
25
16
 
@@ -1,7 +1,6 @@
1
1
  module AhoyCaptain
2
2
  class ExportsController < ApplicationController
3
3
  skip_before_action :act_like_an_spa
4
- include Rangeable
5
4
 
6
5
  def show
7
6
  export = Export.new(params, self).build
@@ -9,7 +8,7 @@ module AhoyCaptain
9
8
  send_data file.read,
10
9
  type: 'application/zip',
11
10
  disposition: 'attachment',
12
- filename: "AhoyCaptain export #{request.host} #{range[0].to_date} to #{range[1].to_date}.zip"
11
+ filename: "AhoyCaptain export #{request.host} #{range[0].to_date} to #{(range[1] || Time.current).to_date}.zip"
13
12
  end
14
13
  end
15
14
  end
@@ -1,12 +1,10 @@
1
1
  module AhoyCaptain
2
2
  module Filters
3
3
  class BaseController < ApplicationController
4
- include Rangeable
5
-
6
4
  private
7
5
 
8
6
  def serialize(value)
9
- { text: value }
7
+ { text: (value.presence || AhoyCaptain.none.text), value: (value.presence || AhoyCaptain.none.value) }
10
8
  end
11
9
 
12
10
  def visit_query
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class GoalsController < BaseController
4
+ def index
5
+ render json: AhoyCaptain.configuration.goals.map { |goal| { text: goal.title, value: goal.id } }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -5,7 +5,7 @@ module AhoyCaptain
5
5
  def index
6
6
  query = event_query.all.with_url.distinct_url
7
7
 
8
- render json: query.map { |row| { text: row.url } }
8
+ render json: query.map { |row| serialize(row.url) }
9
9
  end
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module AhoyCaptain
5
5
  def index
6
6
  query = event_query.all.distinct("entry_pages.url").select("entry_pages.url as url")
7
7
 
8
- render json: query.map { |row| { text: row.url } }
8
+ render json: query.map { |row| serialize(row.url) }
9
9
  end
10
10
 
11
11
  end
@@ -5,7 +5,7 @@ module AhoyCaptain
5
5
  def index
6
6
  query = event_query.distinct("exit_pages.url").select("exit_pages.url as url")
7
7
 
8
- render json: query.map { |row| { text: row.url } }
8
+ render json: query.map { |row| serialize(row.url) }
9
9
  end
10
10
 
11
11
  end
@@ -3,11 +3,11 @@ module AhoyCaptain
3
3
  module Properties
4
4
  class ValuesController < BaseController
5
5
  def index
6
- query = ::Ahoy::Event.with(elements: event_query.select("ahoy_events.properties->>'controller' as element"))
7
- .select("distinct elements.element").from("elements")
6
+ param_key = params[:q].to_unsafe_h.detect { |k,v| k.ends_with?("_i_cont") && k.starts_with?("properties.") }[0]
7
+ key = param_key.delete_prefix("properties.").delete_suffix("_i_cont")
8
+ query = event_query.all.distinct.select("properties->>'#{key}'").pluck(Arel.sql "properties->>'#{key}'")
8
9
 
9
-
10
- render json: query.map(&:element).map { |element| serialize(element) }
10
+ render json: query.map { |element| serialize(element) }
11
11
  end
12
12
  end
13
13
  end
@@ -4,7 +4,7 @@ module AhoyCaptain
4
4
  def index
5
5
  query = visit_query.all
6
6
 
7
- render json: query.result.select("distinct referring_domain").where.not(referring_domain: nil).group(:referring_domain).order(Arel.sql "count(*) desc").pluck(:referring_domain).map { |city| serialize(city) }
7
+ render json: query.select("distinct referring_domain").where.not(referring_domain: nil).group(:referring_domain).order(Arel.sql "count(*) desc").pluck(:referring_domain).map { |city| serialize(city) }
8
8
  end
9
9
  end
10
10
  end
@@ -2,7 +2,7 @@ module AhoyCaptain
2
2
  module Filters
3
3
  class UtmsController < BaseController
4
4
  def index
5
- query = visit_query.select("#{params[:type]}", "count(#{params[:type]}) as total").group(params[:type]).order(Arel.sql "count(#{params[:type]}) desc").pluck(params[:type]).map { |city| serialize(city || "Direct/none") }
5
+ query = visit_query.select("#{params[:type]}", "count(#{params[:type]}) as total").group(params[:type]).order(Arel.sql "count(#{params[:type]}) desc").pluck(params[:type]).map { |city| serialize(city) }
6
6
  render json: query
7
7
  end
8
8
  end
@@ -0,0 +1,22 @@
1
+ module AhoyCaptain
2
+ module Locations
3
+ class CitiesController < ApplicationController
4
+ include AhoyCaptain::Limitable
5
+
6
+ before_action do
7
+ if Widget.disabled?(:locations, :cities)
8
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
9
+ end
10
+ end
11
+
12
+ def index
13
+ results = cached(:cities) do
14
+ CityQuery.call(params)
15
+ .limit(limit)
16
+ end
17
+
18
+ @cities = paginate(results).map { |city| CityDecorator.new(city, self) }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module AhoyCaptain
2
+ module Locations
3
+ class CountriesController < ApplicationController
4
+ include Limitable
5
+
6
+ before_action do
7
+ if Widget.disabled?(:locations, :countries)
8
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
9
+ end
10
+ end
11
+
12
+ def index
13
+ results = cached(:countries) do
14
+ CountryQuery.call(params)
15
+ .limit(limit)
16
+ end
17
+
18
+ @countries = paginate(results).map { |country| CountryDecorator.new(country, self) }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module AhoyCaptain
2
+ module Locations
3
+ class MapsController < ApplicationController
4
+ include Limitable
5
+
6
+ before_action do
7
+ if Widget.disabled?(:locations, :map)
8
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
9
+ end
10
+ end
11
+
12
+ def show
13
+ if request.variant.include?(:details)
14
+ results = CountryQuery.call(params)
15
+ results = results.limit(limit)
16
+ @countries = paginate(results).map { |country| CountryDecorator.new(country, self) }
17
+ render template: 'ahoy_captain/locations/countries/index'
18
+ else
19
+ @countries = visit_query.group("country").count
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ module AhoyCaptain
2
+ module Locations
3
+ class RegionsController < ApplicationController
4
+ include Limitable
5
+
6
+ before_action do
7
+ if Widget.disabled?(:locations, :regions)
8
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
9
+ end
10
+ end
11
+
12
+ def index
13
+ results = cached(:regions) do
14
+ RegionQuery.call(params)
15
+ .limit(limit)
16
+ end
17
+
18
+ @regions = paginate(results).map { |region| RegionDecorator.new(region, self) }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ module AhoyCaptain
2
+ class PropertiesController < ApplicationController
3
+ before_action do
4
+ @options = ::Ahoy::Event.select("jsonb_object_keys(properties) as keys").distinct("jsonb_object_keys(properties)").map(&:keys).map { |key| [Base64.urlsafe_encode64(key), key]}.to_h
5
+ end
6
+
7
+ def index
8
+ end
9
+
10
+ def show
11
+ value = Base64.urlsafe_decode64(params[:id])
12
+
13
+ @properties = event_query
14
+ .select(
15
+ "COALESCE(properties->>'#{value}', '(none)') AS label",
16
+ "COUNT(*) AS events_count",
17
+ "COUNT(DISTINCT visit_id) AS unique_visitors_count",
18
+ "(COUNT(DISTINCT visit_id)/COUNT(*)::numeric) * 100 as percentage"
19
+ )
20
+ .group("COALESCE(properties->>'#{value}', '(none)')")
21
+ .order(Arel.sql "COUNT(*) desc")
22
+ end
23
+
24
+ private
25
+
26
+ helper_method :has_property?
27
+ def has_property?(value)
28
+ searching_properties[value]
29
+ end
30
+
31
+ helper_method :selected_property?
32
+ def selected_property?(value)
33
+ encoded = Base64.urlsafe_encode64(value, padding: false)
34
+ encoded == params[:id]
35
+ end
36
+
37
+ def searching_properties
38
+ JSON.parse(params.dig("q", "properties_json_cont") || '{}')
39
+ end
40
+ end
41
+ end
@@ -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
36
- if diff.in_months > 1
44
+ diff = (range[1] - range[0]).seconds.in_days
45
+ if diff >= 31
37
46
  "month"
38
- elsif diff.in_days > 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,7 +2,36 @@ module AhoyCaptain
2
2
  module ApplicationHelper
3
3
  include Pagy::Frontend
4
4
 
5
- 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
34
+
6
35
  def ahoy_captain_importmap_tags(entry_point = "application", shim: true)
7
36
  safe_join [
8
37
  (javascript_importmap_shim_tag if shim),
@@ -17,8 +46,36 @@ module AhoyCaptain
17
46
  request.query_parameters
18
47
  end
19
48
 
20
- def special_params
21
- params.to_unsafe_h.slice(*SPECIAL_PARAMS)
49
+ # gets put into the form as a hidden field
50
+ #
51
+ def non_filter_ransack_params
52
+ other_params = {}
53
+ map = [
54
+ :start_date,
55
+ :end_date,
56
+ :period,
57
+ :interval,
58
+ :comparison,
59
+ ]
60
+
61
+ # properties stuff falls into current_property_filter
62
+ ransack = [:goal]
63
+
64
+ map.each do |key|
65
+ if params[key]
66
+ other_params[key] = params[key]
67
+ end
68
+ end
69
+
70
+ ransack.each do |key|
71
+ Ransack.predicates.keys.each do |predicate|
72
+ if value = params.dig(:q, "#{key}_#{predicate}")
73
+ other_params["q[#{key}_#{predicate}]"] = value
74
+ end
75
+ end
76
+ end
77
+
78
+ other_params
22
79
  end
23
80
 
24
81
  def render_pagination
@@ -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