ahoy_captain 0.91 → 1.0.1

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 +3 -2
  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
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class DevicesTableComponent < DynamicTable
4
+ register do
5
+ progress_bar :display_name, value: :count, max: :total_count, title: "Device"
6
+ number :count, title: "Visitors"
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class DynamicTable
4
+ def self.register(options = {}, &block)
5
+ @table ||= DynamicTableComponent.build(self, options, &block)
6
+ end
7
+
8
+ def self.table
9
+ @table
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,204 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class DynamicTableComponent < ViewComponent::Base
4
+ class TableDefinition
5
+ attr_reader :rows
6
+ class Row
7
+ attr_reader :attribute, :view_context
8
+ attr_reader :item
9
+ def initialize(attribute, options = {}, &block)
10
+ @attribute = attribute
11
+ @options = options
12
+ @block = block
13
+ @view_context = nil
14
+ end
15
+
16
+ def render(item, view_context)
17
+ @view_context = view_context
18
+ @item = item
19
+ if @options[:type] == :progress_bar
20
+ progress_bar(item)
21
+ else
22
+ if @block
23
+ column(@block.call(item))
24
+ else
25
+ column(item.public_send(attribute).presence)
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ def header
32
+ @options[:title] || @attribute.to_s.titleize
33
+ end
34
+
35
+ def link_to(*args)
36
+ @view_context.link_to(*args)
37
+ end
38
+
39
+ def request
40
+ @view_context.request
41
+ end
42
+
43
+ def h
44
+ @view_context.helpers
45
+ end
46
+
47
+ def search_params
48
+ @view_context.search_params
49
+ end
50
+
51
+ # needed for tricky instance_eval with blocks and view_context
52
+ def method_missing(method, *args, &block)
53
+ if klass.respond_to?(method)
54
+ klass.send(method, *args, &block)
55
+ else
56
+ @item.send method, *args, &block
57
+ end
58
+ end
59
+
60
+ def klass
61
+ @options[:klass]
62
+ end
63
+
64
+ def progress_bar(item)
65
+ @item = item
66
+ value = if @options[:value]
67
+ build_option(item, @options[:value]).to_s
68
+ else
69
+ item.public_send(@attribute)
70
+ end
71
+
72
+ max = build_option(item, @options[:max]).to_s
73
+ label = if @block
74
+ instance_eval(&@block)
75
+ else
76
+ item.public_send(@attribute)
77
+ end.to_s
78
+
79
+ items = []
80
+ items << view_context.content_tag(:progress, "", class: "progress-primary bg-base-100 h-8 grow", value: value, max: max)
81
+ items << view_context.content_tag(:span, class: "grow text-elipsis overflow-hidden absolute left-4 bottom-3 h-8 text-primary-content") do
82
+ label
83
+ end
84
+
85
+ items.join.html_safe
86
+ end
87
+
88
+ def build_option(item, value)
89
+ if value.is_a?(Symbol)
90
+ item.public_send(value)
91
+ else
92
+ value
93
+ end
94
+ end
95
+
96
+ def column(value = nil, &block)
97
+ view_context.content_tag(:span, class: "w-8 ml-8 text-right") do
98
+ if value
99
+ if @options[:formatter]
100
+ if respond_to?(@options[:formatter])
101
+ public_send(@options[:formatter], value)
102
+ else
103
+ view_context.public_send(@options[:formatter], value)
104
+ end
105
+ else
106
+ value.to_s
107
+ end
108
+ else
109
+ view_context.capture(&block).to_s
110
+ end
111
+ end
112
+ end
113
+
114
+ def number_to_human(amount)
115
+ tooltip(amount) do
116
+ view_context.number_to_human(amount, format: '%n%u', precision: 2, units: { thousand: 'k', million: 'm', billion: 'b' })
117
+ end
118
+ end
119
+
120
+ def percent_total(item)
121
+ '%.1f' % ((item.unit_amount.to_i * 1.0 / total)*100.0)
122
+ end
123
+
124
+ def tooltip(value)
125
+ AhoyCaptain::TooltipComponent.new(amount: value).render_in(view_context)
126
+ end
127
+ end
128
+
129
+ def initialize(klass)
130
+ @klass = klass
131
+ @rows = []
132
+ end
133
+
134
+ def column(key = nil, options = {}, &block)
135
+ options[:klass] = @klass
136
+ @rows << Row.new(key, options, &block)
137
+
138
+ self
139
+ end
140
+
141
+ def progress_bar(*args, &block)
142
+ options = args.extract_options!
143
+ options.assert_valid_keys(:value, :max, :title)
144
+ options[:klass] = @klass
145
+
146
+ @rows << Row.new(args[0], options.merge(type: :progress_bar), &block)
147
+
148
+ self
149
+ end
150
+
151
+ def number(key = nil, options = {}, &block)
152
+ options[:klass] = @klass
153
+
154
+ @rows << Row.new(key, options.merge(formatter: :number_to_human), &block)
155
+
156
+ self
157
+ end
158
+
159
+ def percent(key = nil, options = {}, &block)
160
+ options[:klass] = @klass
161
+
162
+ @rows << Row.new(key, options.merge(formatter: :number_to_percentage), &block)
163
+
164
+ self
165
+ end
166
+
167
+ def row(item, view_context)
168
+ items = []
169
+ @rows.each do |row|
170
+ items << row.render(item, view_context)
171
+ end
172
+ items.join.html_safe
173
+ end
174
+ end
175
+
176
+ def self.build(klass, options = {}, &block)
177
+ if options.key?(:fixed_height)
178
+ options[:header_options] = { fixed_height: options.delete(:fixed_height) }
179
+ end
180
+ table = TableDefinition.new(klass).instance_exec(&block)
181
+ new(klass, table, options)
182
+ end
183
+
184
+ def initialize(klass, table, options = {})
185
+ @klass = klass
186
+ @table = table
187
+ @options = options
188
+ end
189
+
190
+ def for(item)
191
+ @item = item
192
+ self
193
+ end
194
+
195
+ def row(item, view_context)
196
+ @table.row(item, view_context)
197
+ end
198
+
199
+ def headers
200
+ HeaderComponent.new(@table.rows.map.with_index { |row, index| { label: row.header, grow: index.zero? } }, @options[:header_options] || {})
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,17 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class GoalsTableComponent < DynamicTable
4
+ register fixed_height: false do
5
+ progress_bar value: :cr, max: 100, title: "Name" do |row|
6
+ search_params = view_context.search_params
7
+ query = search_params.dup.merge(q: { goal_in: row.goal_id}).to_query
8
+ url = AhoyCaptain::Engine.app.url_helpers.root_path + "?#{query}"
9
+ link_to row.item.name, url, target: :_top
10
+ end
11
+ number :unique_visits, title: "Uniques"
12
+ number :total_events, title: "Total"
13
+ percent :cr, title: "CR"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ <div class="flex text-sm font-bold mb-4">
2
+ <% @headers.each do |header| %>
3
+ <span class="<%= header[:grow] ? 'grow' : 'w-8 ml-8 text-right' %>"><%= header[:label] %></span>
4
+ <% end %>
5
+ </div>
@@ -0,0 +1,18 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class HeaderComponent < ViewComponent::Base
4
+ def initialize(headers, options = {})
5
+ @headers = headers.flatten
6
+ @options = options
7
+ end
8
+
9
+ def fixed_height?
10
+ if @options.key?(:fixed_height)
11
+ @options[:fixed_height]
12
+ else
13
+ true
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -6,6 +6,10 @@ module AhoyCaptain
6
6
  @category_name = category_name
7
7
  @unit_name = unit_name
8
8
  end
9
+
10
+ def fixed_height?
11
+ true
12
+ end
9
13
  end
10
14
  end
11
15
  end
@@ -0,0 +1,27 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ class PropertiesTableComponent < DynamicTable
4
+ register do
5
+ progress_bar :label, value: :percentage, max: 100, title: "Name" do |item|
6
+ link_to item.label, url(item), target: :_top
7
+ end
8
+ number :unique_visitors_count, title: "Visitors"
9
+ number :events_count, title: "Events"
10
+ percent :percentage, title: "%"
11
+ end
12
+
13
+ def self.url(item)
14
+ name = Base64.decode64(item.request.params[:id])
15
+ params = item.search_params.dup
16
+ if params["q"] && params["q"].key?("properties_json_cont")
17
+ json = JSON.parse(params["q"]["properties_json_cont"])
18
+ json[name] = item.item.label
19
+ params["q"]["properties_json_cont"] = json.to_json
20
+ else
21
+ params[:q] = { "properties_json_cont" => { name => item.item.label }.to_json }
22
+ end
23
+ params
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ <% @rows.each do |row| %>
2
+ <%= row.render(@item, view_context) %>
3
+
4
+ <% end %>
@@ -27,7 +27,6 @@ module AhoyCaptain
27
27
  end
28
28
  end
29
29
 
30
-
31
30
  def percent_total(item)
32
31
  '%.1f' % ((item.unit_amount.to_i * 1.0 / total)*100.0)
33
32
  end
@@ -1,13 +1,23 @@
1
- <div class="card card-compact <%= 'lg:col-span-2' if wide %> col-span-1 p-4 shadow-xl rounded-md p-8 mx-4 lg:mx-0 bg-base-100 ">
2
- <div class="flex justify-between">
3
- <h2 class="card-title"><%= title %></h2>
4
- <div class="flex self-center gap-3">
5
- <%= display_links %>
1
+ <div
2
+ data-controller="tile"
3
+ class="card card-compact <%= 'lg:col-span-2' if wide %> col-span-1 shadow-xl rounded-md lg:mx-0 bg-base-200 <%= @classes %>">
4
+ <% if title.present? || display_links.present? %>
5
+ <div class="flex justify-between">
6
+ <% if title.present? %>
7
+ <h2 class="card-title" data-tile-target="title"><%= title %></h2>
8
+ <% end %>
9
+ <% if display_links.present? %>
10
+ <div class="flex self-center lg:gap-3">
11
+ <%= display_links %>
12
+ </div>
13
+ <% end %>
6
14
  </div>
7
- </div>
15
+ <% end %>
8
16
 
9
17
  <%= statistic_display %>
10
- <div class="flex justify-center">
11
- <%= details_cta %>
12
- </div>
18
+ <% if details_cta.present? %>
19
+ <div class="flex justify-center">
20
+ <%= details_cta %>
21
+ </div>
22
+ <% end %>
13
23
  </div>
@@ -5,11 +5,19 @@ 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
@@ -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
@@ -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