ahoy_captain 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -13
  3. data/app/assets/javascript/ahoy_captain/application.js +4 -4
  4. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +14 -0
  5. data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
  6. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +9 -10
  7. data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +5 -5
  8. data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +2 -2
  9. data/app/assets/javascript/ahoy_captain/controllers/filter/item_controller.js +12 -0
  10. data/app/assets/javascript/ahoy_captain/controllers/filter_form_controller.js +13 -0
  11. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +11 -8
  12. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +77 -107
  13. data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
  14. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +3 -3
  15. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +37 -0
  16. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +10 -0
  17. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +9 -8
  18. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +65 -0
  19. data/app/assets/javascript/ahoy_captain/controllers/toggle_controller.js +17 -0
  20. data/app/components/ahoy_captain/dropdown_button_component.html.erb +5 -5
  21. data/app/components/ahoy_captain/dropdown_link_component.html.erb +5 -5
  22. data/app/components/ahoy_captain/filter/dropdown_component.html.erb +48 -0
  23. data/app/components/ahoy_captain/filter/dropdown_component.rb +51 -0
  24. data/app/components/ahoy_captain/filter/modal_component.html.erb +8 -7
  25. data/app/components/ahoy_captain/filter/select_component.html.erb +14 -12
  26. data/app/components/ahoy_captain/filter/select_component.rb +40 -9
  27. data/app/components/ahoy_captain/filter/tag_component.html.erb +8 -4
  28. data/app/components/ahoy_captain/filter/tag_component.rb +6 -30
  29. data/app/components/ahoy_captain/filter/tag_container_component.html.erb +2 -3
  30. data/app/components/ahoy_captain/filter/tag_container_component.rb +1 -8
  31. data/app/components/ahoy_captain/stats/container_component.html.erb +8 -0
  32. data/app/components/ahoy_captain/stats/container_component.rb +11 -0
  33. data/app/components/ahoy_captain/sticky_nav_component.html.erb +7 -19
  34. data/app/components/ahoy_captain/sticky_nav_component.rb +8 -0
  35. data/app/components/ahoy_captain/table_component.html.erb +4 -37
  36. data/app/components/ahoy_captain/table_component.rb +15 -4
  37. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +3 -0
  38. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +9 -0
  39. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +6 -0
  40. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +9 -0
  41. data/app/components/ahoy_captain/tables/headers/header_component.html.erb +5 -0
  42. data/app/components/ahoy_captain/tables/headers/header_component.rb +12 -0
  43. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +5 -0
  44. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +12 -0
  45. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +11 -0
  46. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +20 -0
  47. data/app/components/ahoy_captain/tables/rows/row_component.html.erb +6 -0
  48. data/app/components/ahoy_captain/tables/rows/row_component.rb +41 -0
  49. data/app/components/ahoy_captain/tile_component.html.erb +4 -3
  50. data/app/components/ahoy_captain/tile_component.rb +1 -1
  51. data/app/components/ahoy_captain/tooltip_component.html.erb +2 -2
  52. data/app/controllers/ahoy_captain/application_controller.rb +13 -14
  53. data/app/controllers/ahoy_captain/campaigns_controller.rb +2 -10
  54. data/app/controllers/ahoy_captain/cities_controller.rb +2 -6
  55. data/app/controllers/ahoy_captain/countries_controller.rb +2 -6
  56. data/app/controllers/ahoy_captain/devices_controller.rb +3 -6
  57. data/app/controllers/ahoy_captain/entry_pages_controller.rb +2 -4
  58. data/app/controllers/ahoy_captain/exit_pages_controller.rb +3 -4
  59. data/app/controllers/ahoy_captain/exports_controller.rb +15 -0
  60. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +2 -2
  61. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -2
  62. data/app/controllers/ahoy_captain/filters/properties/names_controller.rb +11 -0
  63. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +15 -0
  64. data/app/controllers/ahoy_captain/filters/sources_controller.rb +1 -1
  65. data/app/controllers/ahoy_captain/regions_controller.rb +3 -7
  66. data/app/controllers/ahoy_captain/sources_controller.rb +2 -5
  67. data/app/controllers/ahoy_captain/stats/base_controller.rb +3 -3
  68. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +1 -0
  69. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -0
  70. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -0
  71. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +1 -0
  72. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +3 -2
  73. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -0
  74. data/app/controllers/ahoy_captain/top_pages_controller.rb +2 -8
  75. data/app/decorators/ahoy_captain/application_decorator.rb +27 -3
  76. data/app/decorators/ahoy_captain/campaign_decorator.rb +8 -0
  77. data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
  78. data/app/decorators/ahoy_captain/country_decorator.rb +10 -0
  79. data/app/decorators/ahoy_captain/device_decorator.rb +13 -2
  80. data/app/decorators/ahoy_captain/page_decorator.rb +11 -0
  81. data/app/decorators/ahoy_captain/region_decorator.rb +16 -0
  82. data/app/decorators/ahoy_captain/source_decorator.rb +7 -0
  83. data/app/helpers/ahoy_captain/application_helper.rb +33 -0
  84. data/app/models/ahoy_captain/export.rb +48 -0
  85. data/app/models/ahoy_captain/filter_parser.rb +67 -0
  86. data/app/presenters/ahoy_captain/dashboard_presenter.rb +6 -1
  87. data/app/presenters/ahoy_captain/goals_presenter.rb +3 -2
  88. data/app/queries/ahoy_captain/application_query.rb +4 -3
  89. data/app/queries/ahoy_captain/campaign_query.rb +14 -0
  90. data/app/queries/ahoy_captain/city_query.rb +11 -0
  91. data/app/queries/ahoy_captain/country_query.rb +10 -0
  92. data/app/queries/ahoy_captain/device_query.rb +10 -0
  93. data/app/queries/ahoy_captain/entry_pages_query.rb +3 -2
  94. data/app/queries/ahoy_captain/event_query.rb +17 -15
  95. data/app/queries/ahoy_captain/exit_pages_query.rb +6 -4
  96. data/app/queries/ahoy_captain/region_query.rb +11 -0
  97. data/app/queries/ahoy_captain/source_query.rb +10 -0
  98. data/app/queries/ahoy_captain/top_page_query.rb +13 -0
  99. data/app/queries/ahoy_captain/visit_query.rb +1 -1
  100. data/app/views/ahoy_captain/devices/_table.html.erb +5 -0
  101. data/app/views/ahoy_captain/devices/index.html+details.erb +1 -1
  102. data/app/views/ahoy_captain/devices/index.html.erb +2 -2
  103. data/app/views/ahoy_captain/funnels/show.html.erb +5 -2
  104. data/app/views/ahoy_captain/goals/index.html.erb +3 -35
  105. data/app/views/ahoy_captain/layouts/application.html.erb +3 -3
  106. data/app/views/ahoy_captain/realtimes/show.html.erb +1 -1
  107. data/app/views/ahoy_captain/roots/_filters.html.erb +34 -0
  108. data/app/views/ahoy_captain/roots/show.html.erb +33 -95
  109. data/app/views/ahoy_captain/stats/base/index.html.erb +4 -7
  110. data/app/views/ahoy_captain/stats/show.html.erb +6 -51
  111. data/config/routes.rb +7 -0
  112. data/lib/ahoy_captain/ahoy/event_methods.rb +36 -73
  113. data/lib/ahoy_captain/ahoy/visit_methods.rb +1 -1
  114. data/lib/ahoy_captain/configuration.rb +13 -3
  115. data/lib/ahoy_captain/engine.rb +18 -0
  116. data/lib/ahoy_captain/filter_configuration/filter.rb +16 -0
  117. data/lib/ahoy_captain/filter_configuration/filter_collection.rb +48 -0
  118. data/lib/ahoy_captain/filters_configuration.rb +73 -0
  119. data/lib/ahoy_captain/goals.rb +10 -2
  120. data/lib/ahoy_captain/period_collection.rb +1 -1
  121. data/lib/ahoy_captain/predicate_label.rb +7 -0
  122. data/lib/ahoy_captain/version.rb +1 -1
  123. data/lib/ahoy_captain.rb +1 -0
  124. data/lib/generators/ahoy_captain/templates/config.rb.tt +25 -0
  125. metadata +56 -20
  126. data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +0 -145
  127. data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +0 -43
  128. data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +0 -25
  129. data/app/models/ahoy_captain/current.rb +0 -9
  130. data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -0,0 +1,41 @@
1
+ module AhoyCaptain
2
+ module Tables
3
+ module Rows
4
+ class RowComponent < ViewComponent::Base
5
+ def initialize(table:, item:)
6
+ @table = table
7
+ @item = item
8
+ end
9
+
10
+ def progress_bar(value, max, label)
11
+ items = []
12
+ items << view_context.content_tag(:progress, "", class: "progress-primary bg-base-100 h-8 grow", value: value, max: max)
13
+ items << view_context.content_tag(:span, class: "grow text-elipsis overflow-hidden absolute left-4 bottom-3 h-8 text-primary-content") do
14
+ label
15
+ end
16
+
17
+ items.join.html_safe
18
+ end
19
+
20
+ def item(value = nil, &block)
21
+ view_context.content_tag(:span, class: "w-8 ml-8 text-right ") do
22
+ if value
23
+ value
24
+ else
25
+ capture(&block)
26
+ end
27
+ end
28
+ end
29
+
30
+
31
+ def percent_total(item)
32
+ '%.1f' % ((item.unit_amount.to_i * 1.0 / total)*100.0)
33
+ end
34
+
35
+ def tooltip(value)
36
+ AhoyCaptain::TooltipComponent.new(amount: value).render_in(self)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,12 +1,13 @@
1
- <div class='<%= 'lg:col-span-2' if wide %> col-span-1 p-4 bg-base-200 rounded-md p-8 mx-4 lg:mx-0'>
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
2
  <div class="flex justify-between">
3
- <h5 class='text-lg text-accent-content'><%= title %></h5>
3
+ <h2 class="card-title"><%= title %></h2>
4
4
  <div class="flex self-center gap-3">
5
5
  <%= display_links %>
6
6
  </div>
7
7
  </div>
8
+
8
9
  <%= statistic_display %>
9
10
  <div class="flex justify-center">
10
11
  <%= details_cta %>
11
12
  </div>
12
- </div>
13
+ </div>
@@ -13,4 +13,4 @@ class AhoyCaptain::TileComponent < ViewComponent::Base
13
13
  private
14
14
 
15
15
  attr_reader :title, :wide
16
- end
16
+ 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,5 +1,3 @@
1
-
2
-
3
1
  module AhoyCaptain
4
2
  module Limitable
5
3
  private
@@ -22,19 +20,25 @@ module AhoyCaptain
22
20
 
23
21
  layout 'ahoy_captain/layouts/application'
24
22
 
25
- before_action do
26
- Current.request = self
23
+ # show the details frame
24
+ before_action :use_details_frame
25
+
26
+ # act like an spa without being an spa
27
+ before_action :act_like_an_spa
28
+
29
+ rescue_from Widget::WidgetDisabled do |e|
30
+ render template: 'ahoy_captain/shared/widget_disabled', locals: { frame: e.frame }
27
31
  end
28
32
 
29
- # show the details frame
30
- before_action do
33
+ private
34
+
35
+ def use_details_frame
31
36
  if request.headers['Turbo-Frame'] == 'details'
32
37
  request.variant = :details
33
38
  end
34
39
  end
35
40
 
36
- # act like an spa without being an spa
37
- before_action do
41
+ def act_like_an_spa
38
42
  if request.format.html? && request.headers['Turbo-Frame'].blank?
39
43
  if request.path != root_path
40
44
  requested_params = Rails.application.routes.recognize_path(request.path).except(:controller, :action)
@@ -46,12 +50,6 @@ module AhoyCaptain
46
50
  end
47
51
  end
48
52
 
49
- rescue_from Widget::WidgetDisabled do |e|
50
- render template: 'ahoy_captain/shared/widget_disabled', locals: { frame: e.frame }
51
- end
52
-
53
- private
54
-
55
53
  def visit_query
56
54
  VisitQuery.call(params)
57
55
  end
@@ -60,6 +58,7 @@ module AhoyCaptain
60
58
  EventQuery.call(params)
61
59
  end
62
60
 
61
+ # Only paginate details requests requests
63
62
  def paginate(collection)
64
63
  if paginate?
65
64
  pagy, results = pagy(collection, page: params[:page])
@@ -10,17 +10,9 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:campaigns, params[:campaigns_type]) do
13
- visit_query
14
- .select(
15
- "COALESCE(#{params[:campaigns_type]}, 'Direct/None') as label",
16
- "count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) as count",
17
- "sum(count(COALESCE(#{params[:campaigns_type]}, 'Direct/None'))) OVER() as total_count"
18
- )
19
- .group("COALESCE(#{params[:campaigns_type]}, 'Direct/None')")
20
- .order(Arel.sql("count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) desc"))
21
- .limit(limit)
13
+ CampaignQuery.call(params).limit(limit)
22
14
  end
23
- @campaigns = paginate(results).map { |campaign| CampaignDecorator.new(campaign) }
15
+ @campaigns = paginate(results).map { |campaign| CampaignDecorator.new(campaign, self) }
24
16
  @campaign_type = params[:campaigns_type]&.titleize&.gsub("Utm", "UTM")
25
17
  end
26
18
  end
@@ -10,15 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:cities) do
13
- visit_query
14
- .select("city, country, count(concat(city, region, country)) as count, sum(count(concat(city, region, country))) over() as total_count")
15
- .where.not(city: nil)
16
- .group("city, region, country")
17
- .order(Arel.sql "count(concat(city, region, country)) desc")
13
+ CityQuery.call(params)
18
14
  .limit(limit)
19
15
  end
20
16
 
21
- @cities = paginate(results).map { |city| CityDecorator.new(city) }
17
+ @cities = paginate(results).map { |city| CityDecorator.new(city, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -1,7 +1,6 @@
1
1
  module AhoyCaptain
2
2
  class CountriesController < ApplicationController
3
3
  include Limitable
4
- include Rangeable
5
4
 
6
5
  before_action do
7
6
  if Widget.disabled?(:locations, :countries)
@@ -11,14 +10,11 @@ module AhoyCaptain
11
10
 
12
11
  def index
13
12
  results = cached(:countries) do
14
- visit_query
15
- .reselect("country as label, count(country) as count, sum(count(country)) OVER() as total_count")
16
- .group("country")
17
- .order("count(country) desc")
13
+ CountryQuery.call(params)
18
14
  .limit(limit)
19
15
  end
20
16
 
21
- @countries = paginate(results).map { |country| CountryDecorator.new(country) }
17
+ @countries = paginate(results).map { |country| CountryDecorator.new(country, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -10,14 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:devices, params[:devices_type]) do
13
- visit_query
14
- .select("#{params[:devices_type]} as label", "count(#{params[:devices_type]}) as count", "sum(count(#{params[:devices_type]})) over() as total_count")
15
- .group(params[:devices_type])
16
- .order("count(#{params[:devices_type]}) desc")
17
- .limit(limit)
13
+ DeviceQuery.call(params)
14
+ .limit(limit)
18
15
  end
19
16
 
20
- @devices = results.map { |device| DeviceDecorator.new(device) }
17
+ @devices = results.map { |device| DeviceDecorator.new(device, self) }
21
18
  end
22
19
  end
23
20
  end
@@ -10,12 +10,10 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:entry_pages) do
13
- EntryPagesQuery.call(params, event_query)
14
- .order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
15
- .limit(limit)
13
+ EntryPagesQuery.call(params).limit(limit)
16
14
  end
17
15
 
18
- @pages = paginate(results).map { |page| EntryPageDecorator.new(page) }
16
+ @pages = paginate(results).map { |page| EntryPageDecorator.new(page, self) }
19
17
  end
20
18
  end
21
19
  end
@@ -10,11 +10,10 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:exit_pages) do
13
- ExitPagesQuery.call(params, event_query)
14
- .order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
15
- .limit(limit)
13
+ ExitPagesQuery.call(params)
14
+ .limit(limit)
16
15
  end
17
- @pages = paginate(results).map { |page| ExitPageDecorator.new(page) }
16
+ @pages = paginate(results).map { |page| ExitPageDecorator.new(page, self) }
18
17
  end
19
18
  end
20
19
  end
@@ -0,0 +1,15 @@
1
+ module AhoyCaptain
2
+ class ExportsController < ApplicationController
3
+ skip_before_action :act_like_an_spa
4
+ include Rangeable
5
+
6
+ def show
7
+ export = Export.new(params, self).build
8
+ file = export.to_zip
9
+ send_data file.read,
10
+ type: 'application/zip',
11
+ disposition: 'attachment',
12
+ filename: "AhoyCaptain export #{request.host} #{range[0].to_date} to #{range[1].to_date}.zip"
13
+ end
14
+ end
15
+ end
@@ -1,10 +1,10 @@
1
1
  module AhoyCaptain
2
2
  module Filters
3
3
  module Pages
4
- # TODO: ACCOMODATE EXIT_PAGES
5
4
  class EntryPagesController < BaseController
6
5
  def index
7
- query = event_query.all.with_url.distinct_url
6
+ query = event_query.all.distinct("entry_pages.url").select("entry_pages.url as url")
7
+
8
8
  render json: query.map { |row| { text: row.url } }
9
9
  end
10
10
 
@@ -1,10 +1,9 @@
1
1
  module AhoyCaptain
2
2
  module Filters
3
3
  module Pages
4
- # TODO: ACCOMODATE ENTRY_PAGES
5
4
  class ExitPagesController < BaseController
6
5
  def index
7
- query = event_query.with_url.distinct_url
6
+ query = event_query.distinct("exit_pages.url").select("exit_pages.url as url")
8
7
 
9
8
  render json: query.map { |row| { text: row.url } }
10
9
  end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module Properties
4
+ class NamesController < BaseController
5
+ def index
6
+ render json: ::Ahoy::Event.select("jsonb_object_keys(properties) as keys").distinct("jsonb_object_keys(properties)").map(&:keys).map { |key| serialize(key) }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module Properties
4
+ class ValuesController < BaseController
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")
8
+
9
+
10
+ render json: query.map(&:element).map { |element| serialize(element) }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ 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
@@ -10,15 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:regions) do
13
- visit_query
14
- .reselect("region, country, count(concat(region, country)) as count, sum(count(region)) over() as total_count")
15
- .where.not(region: nil)
16
- .group("region, country")
17
- .order(Arel.sql "count(concat(region, country)) desc")
18
- .limit(limit)
13
+ RegionQuery.call(params)
14
+ .limit(limit)
19
15
  end
20
16
 
21
- @regions = paginate(results).map { |region| RegionDecorator.new(region) }
17
+ @regions = paginate(results).map { |region| RegionDecorator.new(region, self) }
22
18
  end
23
19
  end
24
20
  end
@@ -10,14 +10,11 @@ module AhoyCaptain
10
10
 
11
11
  def index
12
12
  results = cached(:sources) do
13
- visit_query.select("substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)') as referring_domain, count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')) as count, sum(count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)'))) OVER() as total_count")
14
- .where.not(referring_domain: nil)
15
- .group("substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')")
16
- .order(Arel.sql "count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')) desc")
13
+ SourceQuery.call(params)
17
14
  .limit(limit)
18
15
  end
19
16
 
20
- @sources = paginate(results).map { |source| AhoyCaptain::SourceDecorator.new(source) }
17
+ @sources = paginate(results).map { |source| AhoyCaptain::SourceDecorator.new(source, self) }
21
18
  end
22
19
  end
23
20
 
@@ -32,10 +32,10 @@ module AhoyCaptain
32
32
  # assume we're in a realtime
33
33
  return INTERVAL_PERIOD["realtime"][0]
34
34
  end
35
- diff = (range[1] - range[0]).seconds
36
- if diff.in_months > 1
35
+ diff = (range[1] - range[0]).seconds.in_days.to_i
36
+ if diff > 30
37
37
  "month"
38
- elsif diff.in_days > 0
38
+ elsif diff > 0
39
39
  "day"
40
40
  else
41
41
  "hour"
@@ -5,6 +5,7 @@ module AhoyCaptain
5
5
  def index
6
6
  @stats = AhoyCaptain::Stats::BounceRatesQuery.call(params)
7
7
  @stats = @stats.group_by_period(selected_interval, "daily_bounce_rate.date").average("bounce_rate")
8
+ @label = "Bounce Rate"
8
9
  end
9
10
  end
10
11
  end
@@ -3,6 +3,7 @@ module AhoyCaptain
3
3
  class TotalPageviewsController < BaseController
4
4
  def index
5
5
  @stats = AhoyCaptain::Stats::TotalPageviewsQuery.call(params).group_by_period(selected_interval, :time).count
6
+ @label = "Visitors"
6
7
  end
7
8
  end
8
9
  end
@@ -3,6 +3,7 @@ module AhoyCaptain
3
3
  class TotalVisitsController < BaseController
4
4
  def index
5
5
  @stats = AhoyCaptain::Stats::TotalVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
+ @label = "Visitors"
6
7
  end
7
8
  end
8
9
  end
@@ -3,6 +3,7 @@ module AhoyCaptain
3
3
  class UniqueVisitorsController < BaseController
4
4
  def index
5
5
  @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
+ @label = "Visitors"
6
7
  end
7
8
  end
8
9
  end
@@ -1,8 +1,6 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class ViewsPerVisitsController < BaseController
4
- include Rangeable
5
-
6
4
  # @todo: make me a window func
7
5
  def index
8
6
  @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit)
@@ -13,6 +11,9 @@ module AhoyCaptain
13
11
  end
14
12
  end
15
13
  end
14
+
15
+ @label = "Views"
16
+
16
17
  end
17
18
  end
18
19
  end
@@ -3,6 +3,7 @@ module AhoyCaptain
3
3
  class VisitDurationsController < BaseController
4
4
  def index
5
5
  @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).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)