ahoy_captain 0.8 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -13
  3. data/app/assets/javascript/ahoy_captain/application.js +4 -4
  4. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +30 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
  6. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +16 -9
  7. data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +341 -0
  8. data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +5 -5
  9. data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +2 -2
  10. data/app/assets/javascript/ahoy_captain/controllers/filter/item_controller.js +12 -0
  11. data/app/assets/javascript/ahoy_captain/controllers/filter_form_controller.js +13 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +116 -104
  14. data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
  15. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +8 -3
  16. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +188 -0
  17. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +9 -0
  18. data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
  19. data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +46 -0
  20. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +12 -9
  21. data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +9 -0
  22. data/app/assets/javascript/ahoy_captain/controllers/toggle_controller.js +17 -0
  23. data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
  24. data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
  25. data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
  26. data/app/components/ahoy_captain/combobox_component.rb +13 -0
  27. data/app/components/ahoy_captain/comparison_link_component.rb +40 -0
  28. data/app/components/ahoy_captain/dropdown_button_component.html.erb +5 -5
  29. data/app/components/ahoy_captain/dropdown_link_component.html.erb +5 -5
  30. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +50 -0
  31. data/app/components/ahoy_captain/filter/dropdown_component.rb +51 -0
  32. data/app/components/ahoy_captain/filter/modal_component.html.erb +12 -9
  33. data/app/components/ahoy_captain/filter/select_component.html.erb +23 -19
  34. data/app/components/ahoy_captain/filter/select_component.rb +41 -9
  35. data/app/components/ahoy_captain/filter/tag_component.html.erb +8 -4
  36. data/app/components/ahoy_captain/filter/tag_component.rb +6 -30
  37. data/app/components/ahoy_captain/filter/tag_container_component.html.erb +2 -3
  38. data/app/components/ahoy_captain/filter/tag_container_component.rb +1 -8
  39. data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
  40. data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
  41. data/app/components/ahoy_captain/stats/container_component.html.erb +15 -0
  42. data/app/components/ahoy_captain/stats/container_component.rb +26 -0
  43. data/app/components/ahoy_captain/sticky_nav_component.html.erb +13 -22
  44. data/app/components/ahoy_captain/sticky_nav_component.rb +11 -0
  45. data/app/components/ahoy_captain/table_component.html.erb +4 -37
  46. data/app/components/ahoy_captain/table_component.rb +25 -5
  47. data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
  48. data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
  49. data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
  50. data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
  51. data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
  52. data/app/components/ahoy_captain/tables/header_component.rb +18 -0
  53. data/app/components/ahoy_captain/tables/headers/header_component.html.erb +5 -0
  54. data/app/components/ahoy_captain/tables/headers/header_component.rb +16 -0
  55. data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
  56. data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
  57. data/app/components/ahoy_captain/tables/rows/row_component.html.erb +6 -0
  58. data/app/components/ahoy_captain/tables/rows/row_component.rb +40 -0
  59. data/app/components/ahoy_captain/tile_component.html.erb +21 -10
  60. data/app/components/ahoy_captain/tile_component.rb +3 -2
  61. data/app/components/ahoy_captain/tooltip_component.html.erb +2 -2
  62. data/app/controllers/ahoy_captain/application_controller.rb +19 -29
  63. data/app/controllers/ahoy_captain/campaigns_controller.rb +2 -10
  64. data/app/controllers/ahoy_captain/cities_controller.rb +2 -6
  65. data/app/controllers/ahoy_captain/countries_controller.rb +2 -6
  66. data/app/controllers/ahoy_captain/devices_controller.rb +3 -6
  67. data/app/controllers/ahoy_captain/entry_pages_controller.rb +2 -4
  68. data/app/controllers/ahoy_captain/exit_pages_controller.rb +3 -4
  69. data/app/controllers/ahoy_captain/exports_controller.rb +14 -0
  70. data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
  71. data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
  72. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
  73. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +3 -3
  74. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +2 -3
  75. data/app/controllers/ahoy_captain/filters/properties/names_controller.rb +11 -0
  76. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +15 -0
  77. data/app/controllers/ahoy_captain/filters/sources_controller.rb +1 -1
  78. data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
  79. data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
  80. data/app/controllers/ahoy_captain/regions_controller.rb +3 -7
  81. data/app/controllers/ahoy_captain/sources_controller.rb +2 -5
  82. data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
  83. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +2 -1
  84. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +2 -1
  85. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +2 -1
  86. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +3 -1
  87. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +3 -11
  88. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +2 -1
  89. data/app/controllers/ahoy_captain/top_pages_controller.rb +2 -8
  90. data/app/decorators/ahoy_captain/application_decorator.rb +27 -3
  91. data/app/decorators/ahoy_captain/campaign_decorator.rb +8 -0
  92. data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
  93. data/app/decorators/ahoy_captain/country_decorator.rb +10 -0
  94. data/app/decorators/ahoy_captain/device_decorator.rb +13 -2
  95. data/app/decorators/ahoy_captain/page_decorator.rb +11 -0
  96. data/app/decorators/ahoy_captain/region_decorator.rb +16 -0
  97. data/app/decorators/ahoy_captain/source_decorator.rb +7 -0
  98. data/app/helpers/ahoy_captain/application_helper.rb +62 -3
  99. data/app/models/ahoy_captain/comparison_mode.rb +72 -0
  100. data/app/models/ahoy_captain/export.rb +48 -0
  101. data/app/models/ahoy_captain/filter_parser.rb +82 -0
  102. data/app/models/ahoy_captain/range_from_params.rb +75 -0
  103. data/app/models/ahoy_captain/rangeable.rb +0 -3
  104. data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
  105. data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
  106. data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
  107. data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -49
  108. data/app/presenters/ahoy_captain/goals_presenter.rb +3 -2
  109. data/app/queries/ahoy_captain/application_query.rb +78 -13
  110. data/app/queries/ahoy_captain/campaign_query.rb +14 -0
  111. data/app/queries/ahoy_captain/city_query.rb +11 -0
  112. data/app/queries/ahoy_captain/country_query.rb +10 -0
  113. data/app/queries/ahoy_captain/device_query.rb +10 -0
  114. data/app/queries/ahoy_captain/entry_pages_query.rb +3 -2
  115. data/app/queries/ahoy_captain/event_query.rb +20 -13
  116. data/app/queries/ahoy_captain/exit_pages_query.rb +6 -4
  117. data/app/queries/ahoy_captain/region_query.rb +11 -0
  118. data/app/queries/ahoy_captain/source_query.rb +10 -0
  119. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
  120. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
  121. data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
  122. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
  123. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
  124. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  125. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  126. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
  127. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +3 -3
  128. data/app/queries/ahoy_captain/top_page_query.rb +13 -0
  129. data/app/queries/ahoy_captain/visit_query.rb +2 -3
  130. data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
  131. data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
  132. data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
  133. data/app/views/ahoy_captain/devices/_table.html.erb +2 -0
  134. data/app/views/ahoy_captain/devices/index.html+details.erb +1 -1
  135. data/app/views/ahoy_captain/devices/index.html.erb +2 -2
  136. data/app/views/ahoy_captain/funnels/show.html.erb +5 -2
  137. data/app/views/ahoy_captain/goals/index.html.erb +2 -37
  138. data/app/views/ahoy_captain/layouts/application.html.erb +3 -4
  139. data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
  140. data/app/views/ahoy_captain/properties/index.html.erb +3 -0
  141. data/app/views/ahoy_captain/properties/show.html.erb +6 -0
  142. data/app/views/ahoy_captain/realtimes/show.html.erb +1 -1
  143. data/app/views/ahoy_captain/roots/_filters.html.erb +80 -0
  144. data/app/views/ahoy_captain/roots/show.html.erb +76 -109
  145. data/app/views/ahoy_captain/stats/base/index.html.erb +34 -9
  146. data/app/views/ahoy_captain/stats/show.html.erb +8 -55
  147. data/config/routes.rb +9 -0
  148. data/lib/ahoy_captain/ahoy/event_methods.rb +35 -74
  149. data/lib/ahoy_captain/ahoy/visit_methods.rb +1 -1
  150. data/lib/ahoy_captain/configuration.rb +18 -7
  151. data/lib/ahoy_captain/engine.rb +22 -0
  152. data/lib/ahoy_captain/filter_configuration/filter.rb +16 -0
  153. data/lib/ahoy_captain/filter_configuration/filter_collection.rb +48 -0
  154. data/lib/ahoy_captain/filters_configuration.rb +77 -0
  155. data/lib/ahoy_captain/goals.rb +10 -2
  156. data/lib/ahoy_captain/period_collection.rb +1 -1
  157. data/lib/ahoy_captain/predicate_label.rb +7 -0
  158. data/lib/ahoy_captain/version.rb +1 -1
  159. data/lib/ahoy_captain.rb +7 -1
  160. data/lib/generators/ahoy_captain/templates/config.rb.tt +32 -0
  161. metadata +80 -21
  162. data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +0 -145
  163. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -17
  164. data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +0 -43
  165. data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +0 -25
  166. data/app/models/ahoy_captain/current.rb +0 -9
  167. data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -2,7 +2,8 @@ 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
+ @label = "Visitors"
6
7
  end
7
8
  end
8
9
  end
@@ -2,7 +2,9 @@ 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)
7
+ @label = "Visitors"
6
8
  end
7
9
  end
8
10
  end
@@ -1,18 +1,10 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class ViewsPerVisitsController < BaseController
4
- include Rangeable
5
-
6
- # @todo: make me a window func
7
4
  def index
8
- @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit)
9
- if range[1]
10
- (range[0].to_date..range[1].to_date).to_a.each do |date|
11
- unless @stats.key?(date.to_date)
12
- @stats[date.to_date] = 2
13
- end
14
- end
15
- 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))
6
+
7
+ @label = "Views"
16
8
  end
17
9
  end
18
10
  end
@@ -2,7 +2,8 @@ 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
+ @label = "Duration"
6
7
  end
7
8
  end
8
9
  end
@@ -10,17 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:top_pages) do
13
- event_query.with_routes.select(
14
- "#{AhoyCaptain.config.event[:url_column]} as url",
15
- "count(*) as count",
16
- "sum(count(*)) over() as total_count"
17
- )
18
- .group(Arel.sql ("(#{AhoyCaptain.config.event[:url_column]})"))
19
- .order(Arel.sql("count(#{AhoyCaptain.config.event[:url_column]}) desc"))
13
+ TopPageQuery.call(params)
20
14
  .limit(limit)
21
15
  end
22
16
 
23
- @pages = paginate(results).map { |page| TopPageDecorator.new(page) }
17
+ @pages = paginate(results).map { |page| TopPageDecorator.new(page, self) }
24
18
  end
25
19
  end
26
20
  end
@@ -1,15 +1,39 @@
1
+ require 'csv'
2
+
1
3
  module AhoyCaptain
2
4
  class ApplicationDecorator
3
5
  attr_reader :object
4
6
 
5
- def initialize(object)
7
+ def self.to_csv(collection, context)
8
+ rows = collection.map { |row| new(row, context) }
9
+ CSV.generate do |csv|
10
+ csv << csv_map(context.params).keys
11
+
12
+ rows.each do |row|
13
+ items = []
14
+ csv_map.values.each do |attr|
15
+ items << row.send(attr)
16
+ end
17
+
18
+ csv << items
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.csv_map(params = {})
24
+ raise NotImplementedError
25
+ end
26
+
27
+ delegate_missing_to :object
28
+ def initialize(object, context)
6
29
  @object = object
30
+ @context = context
7
31
  end
8
32
 
9
33
  private
10
34
 
11
35
  def h
12
- @h ||= Current.request.view_context
36
+ @h ||= @context.view_context
13
37
  end
14
38
 
15
39
  def params
@@ -28,7 +52,7 @@ module AhoyCaptain
28
52
  end
29
53
 
30
54
  def request
31
- Current.request.request
55
+ @context.request
32
56
  end
33
57
  end
34
58
  end
@@ -1,5 +1,13 @@
1
1
  module AhoyCaptain
2
2
  class CampaignDecorator < ApplicationDecorator
3
+
4
+ def self.csv_map(params = {})
5
+ {
6
+ params[:campaigns_type] => :label,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  if object.label == "Direct/None"
5
13
  value = ""
@@ -1,10 +1,22 @@
1
1
  module AhoyCaptain
2
2
  class CityDecorator < CountryDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Country" => :country,
6
+ "City" => :city,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  search = search_query(country_eq: object.country, city_eq: object.city)
5
13
  frame_link("#{country_emoji(object.country)} #{object.city}", search)
6
14
  end
7
15
 
16
+ def country
17
+ "#{country_emoji(object.country)} #{object.country}"
18
+ end
19
+
8
20
  def unit_amount
9
21
  object.count
10
22
  end
@@ -1,6 +1,16 @@
1
1
  module AhoyCaptain
2
2
  class CountryDecorator < ApplicationDecorator
3
3
  EMOJI_MAP = { "AD": "🇦🇩", "AE": "🇦🇪", "AF": "🇦🇫", "AG": "🇦🇬", "AI": "🇦🇮", "AL": "🇦🇱", "AM": "🇦🇲", "AO": "🇦🇴", "AQ": "🇦🇶", "AR": "🇦🇷", "AS": "🇦🇸", "AT": "🇦🇹", "AU": "🇦🇺", "AW": "🇦🇼", "AX": "🇦🇽", "AZ": "🇦🇿", "BA": "🇧🇦", "BB": "🇧🇧", "BD": "🇧🇩", "BE": "🇧🇪", "BF": "🇧🇫", "BG": "🇧🇬", "BH": "🇧🇭", "BI": "🇧🇮", "BJ": "🇧🇯", "BL": "🇧🇱", "BM": "🇧🇲", "BN": "🇧🇳", "BO": "🇧🇴", "BQ": "🇧🇶", "BR": "🇧🇷", "BS": "🇧🇸", "BT": "🇧🇹", "BV": "🇧🇻", "BW": "🇧🇼", "BY": "🇧🇾", "BZ": "🇧🇿", "CA": "🇨🇦", "CC": "🇨🇨", "CD": "🇨🇩", "CF": "🇨🇫", "CG": "🇨🇬", "CH": "🇨🇭", "CI": "🇨🇮", "CK": "🇨🇰", "CL": "🇨🇱", "CM": "🇨🇲", "CN": "🇨🇳", "CO": "🇨🇴", "CR": "🇨🇷", "CU": "🇨🇺", "CV": "🇨🇻", "CW": "🇨🇼", "CX": "🇨🇽", "CY": "🇨🇾", "CZ": "🇨🇿", "DE": "🇩🇪", "DJ": "🇩🇯", "DK": "🇩🇰", "DM": "🇩🇲", "DO": "🇩🇴", "DZ": "🇩🇿", "EC": "🇪🇨", "EE": "🇪🇪", "EG": "🇪🇬", "EH": "🇪🇭", "ER": "🇪🇷", "ES": "🇪🇸", "ET": "🇪🇹", "FI": "🇫🇮", "FJ": "🇫🇯", "FK": "🇫🇰", "FM": "🇫🇲", "FO": "🇫🇴", "FR": "🇫🇷", "GA": "🇬🇦", "GB": "🇬🇧", "GD": "🇬🇩", "GE": "🇬🇪", "GF": "🇬🇫", "GG": "🇬🇬", "GH": "🇬🇭", "GI": "🇬🇮", "GL": "🇬🇱", "GM": "🇬🇲", "GN": "🇬🇳", "GP": "🇬🇵", "GQ": "🇬🇶", "GR": "🇬🇷", "GS": "🇬🇸", "GT": "🇬🇹", "GU": "🇬🇺", "GW": "🇬🇼", "GY": "🇬🇾", "HK": "🇭🇰", "HM": "🇭🇲", "HN": "🇭🇳", "HR": "🇭🇷", "HT": "🇭🇹", "HU": "🇭🇺", "ID": "🇮🇩", "IE": "🇮🇪", "IL": "🇮🇱", "IM": "🇮🇲", "IN": "🇮🇳", "IO": "🇮🇴", "IQ": "🇮🇶", "IR": "🇮🇷", "IS": "🇮🇸", "IT": "🇮🇹", "JE": "🇯🇪", "JM": "🇯🇲", "JO": "🇯🇴", "JP": "🇯🇵", "KE": "🇰🇪", "KG": "🇰🇬", "KH": "🇰🇭", "KI": "🇰🇮", "KM": "🇰🇲", "KN": "🇰🇳", "KP": "🇰🇵", "KR": "🇰🇷", "KW": "🇰🇼", "KY": "🇰🇾", "KZ": "🇰🇿", "LA": "🇱🇦", "LB": "🇱🇧", "LC": "🇱🇨", "LI": "🇱🇮", "LK": "🇱🇰", "LR": "🇱🇷", "LS": "🇱🇸", "LT": "🇱🇹", "LU": "🇱🇺", "LV": "🇱🇻", "LY": "🇱🇾", "MA": "🇲🇦", "MC": "🇲🇨", "MD": "🇲🇩", "ME": "🇲🇪", "MF": "🇲🇫", "MG": "🇲🇬", "MH": "🇲🇭", "MK": "🇲🇰", "ML": "🇲🇱", "MM": "🇲🇲", "MN": "🇲🇳", "MO": "🇲🇴", "MP": "🇲🇵", "MQ": "🇲🇶", "MR": "🇲🇷", "MS": "🇲🇸", "MT": "🇲🇹", "MU": "🇲🇺", "MV": "🇲🇻", "MW": "🇲🇼", "MX": "🇲🇽", "MY": "🇲🇾", "MZ": "🇲🇿", "NA": "🇳🇦", "NC": "🇳🇨", "NE": "🇳🇪", "NF": "🇳🇫", "NG": "🇳🇬", "NI": "🇳🇮", "NL": "🇳🇱", "NO": "🇳🇴", "NP": "🇳🇵", "NR": "🇳🇷", "NU": "🇳🇺", "NZ": "🇳🇿", "OM": "🇴🇲", "PA": "🇵🇦", "PE": "🇵🇪", "PF": "🇵🇫", "PG": "🇵🇬", "PH": "🇵🇭", "PK": "🇵🇰", "PL": "🇵🇱", "PM": "🇵🇲", "PN": "🇵🇳", "PR": "🇵🇷", "PS": "🇵🇸", "PT": "🇵🇹", "PW": "🇵🇼", "PY": "🇵🇾", "QA": "🇶🇦", "RE": "🇷🇪", "RO": "🇷🇴", "RS": "🇷🇸", "RU": "🇷🇺", "RW": "🇷🇼", "SA": "🇸🇦", "SB": "🇸🇧", "SC": "🇸🇨", "SD": "🇸🇩", "SE": "🇸🇪", "SG": "🇸🇬", "SH": "🇸🇭", "SI": "🇸🇮", "SJ": "🇸🇯", "SK": "🇸🇰", "SL": "🇸🇱", "SM": "🇸🇲", "SN": "🇸🇳", "SO": "🇸🇴", "SR": "🇸🇷", "SS": "🇸🇸", "ST": "🇸🇹", "SV": "🇸🇻", "SX": "🇸🇽", "SY": "🇸🇾", "SZ": "🇸🇿", "TC": "🇹🇨", "TD": "🇹🇩", "TF": "🇹🇫", "TG": "🇹🇬", "TH": "🇹🇭", "TJ": "🇹🇯", "TK": "🇹🇰", "TL": "🇹🇱", "TM": "🇹🇲", "TN": "🇹🇳", "TO": "🇹🇴", "TR": "🇹🇷", "TT": "🇹🇹", "TV": "🇹🇻", "TW": "🇹🇼", "TZ": "🇹🇿", "UA": "🇺🇦", "UG": "🇺🇬", "UM": "🇺🇲", "US": "🇺🇸", "UY": "🇺🇾", "UZ": "🇺🇿", "VA": "🇻🇦", "VC": "🇻🇨", "VE": "🇻🇪", "VG": "🇻🇬", "VI": "🇻🇮", "VN": "🇻🇳", "VU": "🇻🇺", "WF": "🇼🇫", "WS": "🇼🇸", "YE": "🇾🇪", "YT": "🇾🇹", "ZA": "🇿🇦", "ZM": "🇿🇲", "ZW": "🇿🇼" }
4
+ def self.csv_map(params = {})
5
+ {
6
+ "Country" => :label,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
11
+ def label
12
+ "#{country_emoji(country)} #{country}"
13
+ end
4
14
 
5
15
  def display_name
6
16
  search = search_query(country_eq: object.label)
@@ -1,8 +1,19 @@
1
1
  module AhoyCaptain
2
2
  class DeviceDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "#{params[:devices_type]}" => :label,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
3
10
  def display_name
4
- search = search_query("#{params[:devices_type]}_eq" => object.label)
5
- frame_link(object.label, search)
11
+ search = search_query("#{params[:devices_type]}_eq" => label)
12
+ frame_link(label, search)
13
+ end
14
+
15
+ def label
16
+ object.label
6
17
  end
7
18
 
8
19
  def unit_amount
@@ -1,5 +1,16 @@
1
1
  module AhoyCaptain
2
2
  class PageDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "URL" => :label,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
10
+ def label
11
+ object.url
12
+ end
13
+
3
14
  def display_name
4
15
  search = search_query(type => object.url)
5
16
  frame_link(object.url, search)
@@ -1,10 +1,26 @@
1
1
  module AhoyCaptain
2
2
  class RegionDecorator < CountryDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Country" => :country,
6
+ "Region" => :region,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  search = search_query(region_eq: object.region, country_eq: object.country)
5
13
  frame_link("#{country_emoji(object.country)} #{object.region}", search)
6
14
  end
7
15
 
16
+ def country
17
+ "#{country_emoji(object.country)} #{object.country}"
18
+ end
19
+
20
+ def region
21
+ object.region
22
+ end
23
+
8
24
  def unit_amount
9
25
  object.count
10
26
  end
@@ -1,5 +1,12 @@
1
1
  module AhoyCaptain
2
2
  class SourceDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Domain" => :referring_domain,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
3
10
  def display_name
4
11
  display = %Q(
5
12
  <div class='flex justify-start space-x-8 col-span-1 items-center'>
@@ -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,38 @@ 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
+ :compare_to_start_date,
60
+ :compare_to_end_date
61
+ ]
62
+
63
+ # properties stuff falls into current_property_filter
64
+ ransack = [:goal]
65
+
66
+ map.each do |key|
67
+ if params[key]
68
+ other_params[key] = params[key]
69
+ end
70
+ end
71
+
72
+ ransack.each do |key|
73
+ Ransack.predicates.keys.each do |predicate|
74
+ if value = params.dig(:q, "#{key}_#{predicate}")
75
+ other_params["q[#{key}_#{predicate}]"] = value
76
+ end
77
+ end
78
+ end
79
+
80
+ other_params
22
81
  end
23
82
 
24
83
  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
@@ -0,0 +1,48 @@
1
+ module AhoyCaptain
2
+ class Export
3
+ def initialize(params, context)
4
+ @params = params
5
+ @context = context
6
+ @files = {}
7
+ end
8
+
9
+ def build
10
+ @files["browsers.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: "browser")), DeviceDecorator)
11
+ @files["cities.csv"] = to_csv(CityQuery.call(merged_params), CityDecorator)
12
+ @files["countries.csv"] = to_csv(CountryQuery.call(merged_params), CountryDecorator)
13
+ @files["devices.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: :device_type)), DeviceDecorator)
14
+ @files["entry_pages.csv"] = to_csv(EntryPagesQuery.call(merged_params), EntryPageDecorator)
15
+ @files["exit_pages.csv"] = to_csv(ExitPagesQuery.call(merged_params), ExitPageDecorator)
16
+ @files["operating_systems.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: "os")), DeviceDecorator)
17
+ @files["top_pages.csv"] = to_csv(TopPageQuery.call(merged_params), TopPageDecorator)
18
+ @files["regions.csv"] = to_csv(RegionQuery.call(merged_params), RegionDecorator)
19
+ @files["sources.csv"] = to_csv(SourceQuery.call(merged_params), SourceDecorator)
20
+ ["campaign", "content", "medium", "source", "term"].each do |utm|
21
+ @files["utm_#{utm.pluralize}.csv"] = to_csv(CampaignQuery.call(merged_params(campaigns_type: "utm_#{utm}")), CampaignDecorator)
22
+ end
23
+ self
24
+ end
25
+
26
+ def to_zip
27
+ zip_stream = Zip::OutputStream.write_buffer do |zip|
28
+ @files.each do |filename, csv|
29
+ zip.put_next_entry(filename)
30
+ zip.write(csv)
31
+ end
32
+ end
33
+
34
+ zip_stream.rewind
35
+ zip_stream
36
+ end
37
+
38
+ private
39
+
40
+ def to_csv(query, decorator)
41
+ decorator.to_csv(query, @context)
42
+ end
43
+
44
+ def merged_params(params_to_merge = {})
45
+ @params.dup.merge(params_to_merge)
46
+ end
47
+ end
48
+ end
@@ -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,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