ahoy_captain 0.1.0 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +55 -14
  4. data/Rakefile +1 -10
  5. data/app/assets/images/ahoy_captain/apple-touch-icon.png +0 -0
  6. data/app/assets/images/ahoy_captain/favicon-16x16.png +0 -0
  7. data/app/assets/images/ahoy_captain/favicon-32x32.png +0 -0
  8. data/app/assets/images/ahoy_captain/logo.png +0 -0
  9. data/app/assets/images/ahoy_captain/safari-pinned-tab.svg +199 -0
  10. data/app/assets/javascript/ahoy_captain/application.js +4 -0
  11. data/app/assets/javascript/ahoy_captain/controllers/application.js +9 -0
  12. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +14 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +18 -0
  14. data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +14 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +145 -0
  16. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +17 -0
  17. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +117 -0
  18. data/app/assets/javascript/ahoy_captain/controllers/index.js +3 -0
  19. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +10 -0
  20. data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +43 -0
  21. data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +25 -0
  22. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +27 -0
  23. data/app/assets/manifest/ahoy_captain/manifest.js +2 -0
  24. data/app/components/ahoy_captain/dropdown_button_component.html.erb +16 -0
  25. data/app/components/ahoy_captain/dropdown_button_component.rb +14 -0
  26. data/app/components/ahoy_captain/dropdown_link_component.html.erb +19 -0
  27. data/app/components/ahoy_captain/dropdown_link_component.rb +15 -0
  28. data/app/components/ahoy_captain/filter/modal_component.html.erb +13 -0
  29. data/app/components/ahoy_captain/filter/modal_component.rb +13 -0
  30. data/app/components/ahoy_captain/filter/select_component.html.erb +21 -0
  31. data/app/components/ahoy_captain/filter/select_component.rb +32 -0
  32. data/app/components/ahoy_captain/filter/tag_component.html.erb +9 -0
  33. data/app/components/ahoy_captain/filter/tag_component.rb +38 -0
  34. data/app/components/ahoy_captain/filter/tag_container_component.html.erb +5 -0
  35. data/app/components/ahoy_captain/filter/tag_container_component.rb +13 -0
  36. data/app/components/ahoy_captain/sticky_nav_component.html.erb +37 -0
  37. data/app/components/ahoy_captain/sticky_nav_component.rb +5 -0
  38. data/app/components/ahoy_captain/table_component.html.erb +49 -0
  39. data/app/components/ahoy_captain/table_component.rb +28 -0
  40. data/app/components/ahoy_captain/tile_component.html.erb +12 -0
  41. data/app/components/ahoy_captain/tile_component.rb +16 -0
  42. data/app/components/ahoy_captain/tooltip_component.html.erb +3 -0
  43. data/app/components/ahoy_captain/tooltip_component.rb +18 -0
  44. data/app/controllers/ahoy_captain/application_controller.rb +86 -0
  45. data/app/controllers/ahoy_captain/campaigns_controller.rb +27 -0
  46. data/app/controllers/ahoy_captain/cities_controller.rb +24 -0
  47. data/app/controllers/ahoy_captain/countries_controller.rb +24 -0
  48. data/app/controllers/ahoy_captain/devices_controller.rb +23 -0
  49. data/app/controllers/ahoy_captain/entry_pages_controller.rb +21 -0
  50. data/app/controllers/ahoy_captain/exit_pages_controller.rb +20 -0
  51. data/app/controllers/ahoy_captain/filters/base_controller.rb +17 -0
  52. data/app/controllers/ahoy_captain/filters/locations_controller.rb +11 -0
  53. data/app/controllers/ahoy_captain/filters/operating_systems/names_controller.rb +13 -0
  54. data/app/controllers/ahoy_captain/filters/operating_systems/versions_controller.rb +13 -0
  55. data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +13 -0
  56. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +14 -0
  57. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +16 -0
  58. data/app/controllers/ahoy_captain/filters/screens_controller.rb +11 -0
  59. data/app/controllers/ahoy_captain/filters/sources_controller.rb +11 -0
  60. data/app/controllers/ahoy_captain/filters/utms_controller.rb +10 -0
  61. data/app/controllers/ahoy_captain/funnels_controller.rb +8 -0
  62. data/app/controllers/ahoy_captain/goals_controller.rb +7 -0
  63. data/app/controllers/ahoy_captain/realtimes_controller.rb +7 -0
  64. data/app/controllers/ahoy_captain/regions_controller.rb +24 -0
  65. data/app/controllers/ahoy_captain/roots_controller.rb +6 -0
  66. data/app/controllers/ahoy_captain/sources_controller.rb +24 -0
  67. data/app/controllers/ahoy_captain/stats/base_controller.rb +67 -0
  68. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +11 -0
  69. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +9 -0
  70. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +9 -0
  71. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +9 -0
  72. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +19 -0
  73. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +9 -0
  74. data/app/controllers/ahoy_captain/stats_controller.rb +7 -0
  75. data/app/controllers/ahoy_captain/top_pages_controller.rb +26 -0
  76. data/app/decorators/ahoy_captain/application_decorator.rb +34 -0
  77. data/app/decorators/ahoy_captain/campaign_decorator.rb +19 -0
  78. data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
  79. data/app/decorators/ahoy_captain/country_decorator.rb +28 -0
  80. data/app/decorators/ahoy_captain/device_decorator.rb +16 -0
  81. data/app/decorators/ahoy_captain/entry_page_decorator.rb +7 -0
  82. data/app/decorators/ahoy_captain/exit_page_decorator.rb +7 -0
  83. data/app/decorators/ahoy_captain/page_decorator.rb +16 -0
  84. data/app/decorators/ahoy_captain/region_decorator.rb +12 -0
  85. data/app/decorators/ahoy_captain/source_decorator.rb +20 -0
  86. data/app/decorators/ahoy_captain/top_page_decorator.rb +7 -0
  87. data/app/helpers/ahoy_captain/application_helper.rb +32 -0
  88. data/app/models/ahoy_captain/current.rb +9 -0
  89. data/app/models/ahoy_captain/rangeable.rb +10 -0
  90. data/app/models/ahoy_captain/url_helpers.rb +6 -0
  91. data/app/models/ahoy_captain/widget.rb +15 -0
  92. data/app/models/concerns/ahoy_captain/range_options.rb +21 -0
  93. data/app/presenters/ahoy_captain/dashboard_presenter.rb +84 -0
  94. data/app/presenters/ahoy_captain/funnel_presenter.rb +68 -0
  95. data/app/presenters/ahoy_captain/goals_presenter.rb +69 -0
  96. data/app/queries/ahoy_captain/application_query.rb +121 -0
  97. data/app/queries/ahoy_captain/entry_pages_query.rb +17 -0
  98. data/app/queries/ahoy_captain/event_query.rb +35 -0
  99. data/app/queries/ahoy_captain/exit_pages_query.rb +17 -0
  100. data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +13 -0
  101. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +11 -0
  102. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +17 -0
  103. data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +9 -0
  104. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +9 -0
  105. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +9 -0
  106. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +17 -0
  107. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +16 -0
  108. data/app/queries/ahoy_captain/visit_query.rb +32 -0
  109. data/app/views/ahoy_captain/campaigns/index.html+details.erb +4 -0
  110. data/app/views/ahoy_captain/campaigns/index.html.erb +3 -0
  111. data/app/views/ahoy_captain/cities/index.html+details.erb +4 -0
  112. data/app/views/ahoy_captain/cities/index.html.erb +3 -0
  113. data/app/views/ahoy_captain/countries/index.html+details.erb +5 -0
  114. data/app/views/ahoy_captain/countries/index.html.erb +3 -0
  115. data/app/views/ahoy_captain/devices/index.html+details.erb +4 -0
  116. data/app/views/ahoy_captain/devices/index.html.erb +3 -0
  117. data/app/views/ahoy_captain/entry_pages/index.html+details.erb +4 -0
  118. data/app/views/ahoy_captain/entry_pages/index.html.erb +3 -0
  119. data/app/views/ahoy_captain/exit_pages/index.html+details.erb +4 -0
  120. data/app/views/ahoy_captain/exit_pages/index.html.erb +3 -0
  121. data/app/views/ahoy_captain/funnels/index.html.erb +7 -0
  122. data/app/views/ahoy_captain/funnels/show.html.erb +6 -0
  123. data/app/views/ahoy_captain/goals/index.html.erb +39 -0
  124. data/app/views/ahoy_captain/layouts/application.html.erb +127 -0
  125. data/app/views/ahoy_captain/realtimes/show.html.erb +9 -0
  126. data/app/views/ahoy_captain/regions/index.html+details.erb +4 -0
  127. data/app/views/ahoy_captain/regions/index.html.erb +3 -0
  128. data/app/views/ahoy_captain/roots/show.html.erb +179 -0
  129. data/app/views/ahoy_captain/sources/index.html+details.erb +4 -0
  130. data/app/views/ahoy_captain/sources/index.html.erb +3 -0
  131. data/app/views/ahoy_captain/stats/base/index.html.erb +14 -0
  132. data/app/views/ahoy_captain/stats/show.html.erb +57 -0
  133. data/app/views/ahoy_captain/top_pages/index.html+details.erb +4 -0
  134. data/app/views/ahoy_captain/top_pages/index.html.erb +3 -0
  135. data/config/routes.rb +56 -0
  136. data/lib/ahoy_captain/active_record.rb +108 -0
  137. data/lib/ahoy_captain/ahoy/event_methods.rb +114 -0
  138. data/lib/ahoy_captain/ahoy/visit_methods.rb +23 -0
  139. data/lib/ahoy_captain/configuration.rb +43 -0
  140. data/lib/ahoy_captain/engine.rb +24 -0
  141. data/lib/ahoy_captain/funnels.rb +44 -0
  142. data/lib/ahoy_captain/goals.rb +43 -0
  143. data/lib/ahoy_captain/period_collection.rb +115 -0
  144. data/lib/ahoy_captain/railtie.rb +7 -0
  145. data/lib/ahoy_captain/version.rb +1 -3
  146. data/lib/ahoy_captain.rb +50 -4
  147. data/lib/generators/ahoy_captain/install_generator.rb +18 -0
  148. data/lib/generators/ahoy_captain/migration_generator.rb +21 -0
  149. data/lib/generators/ahoy_captain/templates/config.rb.tt +138 -0
  150. data/lib/generators/ahoy_captain/templates/migration.rb.tt +7 -0
  151. metadata +410 -17
  152. data/.rspec +0 -3
  153. data/.rubocop.yml +0 -13
  154. data/CHANGELOG.md +0 -5
  155. data/Gemfile +0 -12
  156. data/ahoy_captain.gemspec +0 -37
  157. data/sig/ahoy_captain.rbs +0 -4
@@ -0,0 +1,86 @@
1
+
2
+
3
+ module AhoyCaptain
4
+ module Limitable
5
+ private
6
+
7
+ def limit
8
+ if request.variant.include?(:details)
9
+ nil
10
+ else
11
+ if params[:limit]
12
+ params[:limit].to_i
13
+ else
14
+ 10
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ class ApplicationController < ActionController::Base
21
+ include Pagy::Backend
22
+
23
+ layout 'ahoy_captain/layouts/application'
24
+
25
+ before_action do
26
+ Current.request = self
27
+ end
28
+
29
+ # show the details frame
30
+ before_action do
31
+ if request.headers['Turbo-Frame'] == 'details'
32
+ request.variant = :details
33
+ end
34
+ end
35
+
36
+ # act like an spa without being an spa
37
+ before_action do
38
+ if request.format.html? && request.headers['Turbo-Frame'].blank?
39
+ if request.path != root_path
40
+ requested_params = Rails.application.routes.recognize_path(request.path).except(:controller, :action)
41
+ params.merge!(requested_params)
42
+ unless params[:debug]
43
+ render template: 'ahoy_captain/roots/show'
44
+ end
45
+ end
46
+ end
47
+ end
48
+
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
+ def visit_query
56
+ VisitQuery.call(params)
57
+ end
58
+
59
+ def event_query
60
+ EventQuery.call(params)
61
+ end
62
+
63
+ def paginate(collection)
64
+ if paginate?
65
+ pagy, results = pagy(collection, page: params[:page])
66
+ @pagination = pagy
67
+ return results
68
+ end
69
+
70
+ collection
71
+ end
72
+
73
+ def paginate?
74
+ request.variant.include?(:details)
75
+ end
76
+
77
+ def cached(*names)
78
+ if AhoyCaptain.cache.class == ActiveSupport::Cache::NullStore
79
+ return yield
80
+ end
81
+ AhoyCaptain.cache.fetch("ahoy_captain:#{names.join(":")}:#{request.query_parameters.sort.map { |k,v| "#{k}-#{v}" }.join(":")}", expire_in: AhoyCaptain.config.cache[:ttl]) do
82
+ yield
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,27 @@
1
+ module AhoyCaptain
2
+ class CampaignsController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:campaigns, params[:campaigns_type])
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :sources)
8
+ end
9
+ end
10
+
11
+ def index
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)
22
+ end
23
+ @campaigns = paginate(results).map { |campaign| CampaignDecorator.new(campaign) }
24
+ @campaign_type = params[:campaigns_type]&.titleize&.gsub("Utm", "UTM")
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module AhoyCaptain
2
+ class CitiesController < ApplicationController
3
+ include AhoyCaptain::Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:locations, :cities)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
8
+ end
9
+ end
10
+
11
+ def index
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")
18
+ .limit(limit)
19
+ end
20
+
21
+ @cities = paginate(results).map { |city| CityDecorator.new(city) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module AhoyCaptain
2
+ class CountriesController < ApplicationController
3
+ include Limitable
4
+ include Rangeable
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
+ 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")
18
+ .limit(limit)
19
+ end
20
+
21
+ @countries = paginate(results).map { |country| CountryDecorator.new(country) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module AhoyCaptain
2
+ class DevicesController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:devices, params[:devices_type])
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :devices)
8
+ end
9
+ end
10
+
11
+ def index
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)
18
+ end
19
+
20
+ @devices = results.map { |device| DeviceDecorator.new(device) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module AhoyCaptain
2
+ class EntryPagesController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:entry_pages)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :pages)
8
+ end
9
+ end
10
+
11
+ def index
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)
16
+ end
17
+
18
+ @pages = paginate(results).map { |page| EntryPageDecorator.new(page) }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module AhoyCaptain
2
+ class ExitPagesController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:exit_pages)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :pages)
8
+ end
9
+ end
10
+
11
+ def index
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)
16
+ end
17
+ @pages = paginate(results).map { |page| ExitPageDecorator.new(page) }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class BaseController < ApplicationController
4
+ include Rangeable
5
+
6
+ private
7
+
8
+ def serialize(value)
9
+ { text: value }
10
+ end
11
+
12
+ def visit_query
13
+ VisitQuery.call(params)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class LocationsController < BaseController
4
+ def index
5
+ query = visit_query.all
6
+
7
+ render json: query.select("distinct #{params[:type]}").where.not(params[:type] => nil).group(params[:type]).order(Arel.sql "count(*) desc").limit(50).pluck(params[:type]).map { |city| serialize(city) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module OperatingSystems
4
+ class NamesController < BaseController
5
+ def index
6
+ query = visit_query.all
7
+
8
+ render json: query.select("distinct os").where.not(os: nil).group(:os).order(Arel.sql "count(*) desc").pluck(:os).map { |city| serialize(city) }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module OperatingSystems
4
+ class VersionsController < BaseController
5
+ def index
6
+ query = visit_query.all
7
+
8
+ render json: query.select("distinct os_version").where.not(os_version: nil).group(:os_version).order(Arel.sql "count(*) desc").pluck(:os_version).map { |city| serialize(city) }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module Pages
4
+ class ActionsController < BaseController
5
+ def index
6
+ query = event_query.all.with_url.distinct_url
7
+
8
+ render json: query.map { |row| { text: row.url } }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module Pages
4
+ # TODO: ACCOMODATE EXIT_PAGES
5
+ class EntryPagesController < BaseController
6
+ def index
7
+ query = event_query.all.with_url.distinct_url
8
+ render json: query.map { |row| { text: row.url } }
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ module Pages
4
+ # TODO: ACCOMODATE ENTRY_PAGES
5
+ class ExitPagesController < BaseController
6
+ def index
7
+ query = event_query.with_url.distinct_url
8
+
9
+ render json: query.map { |row| { text: row.url } }
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class ScreensController < BaseController
4
+ def index
5
+ query = visit_query.all
6
+
7
+ render json: query.select("distinct device_type").where.not(device_type: nil).group(:device_type).order(Arel.sql "count(*) desc").pluck(:device_type).map { |city| serialize(city) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class SourcesController < BaseController
4
+ def index
5
+ query = visit_query.all
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) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module AhoyCaptain
2
+ module Filters
3
+ class UtmsController < BaseController
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") }
6
+ render json: query
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module AhoyCaptain
2
+ class FunnelsController < ApplicationController
3
+ def show
4
+ funnel = AhoyCaptain.configuration.funnels[params[:id]]
5
+ @funnel = FunnelPresenter.new(funnel, event_query).build
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class GoalsController < ApplicationController
3
+ def index
4
+ @presenter = GoalsPresenter.new(event_query).build
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class RealtimesController < ApplicationController
3
+ def show
4
+ @total = event_query.where("#{AhoyCaptain.event.table_name}.time > ?", 1.minute.ago).distinct(:visit_id).count(:visit_id)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ module AhoyCaptain
2
+ class RegionsController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:locations, :regions)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :geography)
8
+ end
9
+ end
10
+
11
+ def index
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)
19
+ end
20
+
21
+ @regions = paginate(results).map { |region| RegionDecorator.new(region) }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module AhoyCaptain
2
+ class RootsController < ApplicationController
3
+ def show
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ module AhoyCaptain
2
+ class SourcesController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:sources)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :sources)
8
+ end
9
+ end
10
+
11
+ def index
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")
17
+ .limit(limit)
18
+ end
19
+
20
+ @sources = paginate(results).map { |source| AhoyCaptain::SourceDecorator.new(source) }
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,67 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class BaseController < ApplicationController
4
+ include Rangeable
5
+
6
+ INTERVAL_PERIOD = {
7
+ "realtime" => ["minute"],
8
+ "day" => ["minute", "hour"],
9
+ "7d" => ["hour", "day"],
10
+ "month" => ["day", "week"],
11
+ "all" => ["day", "week", "month"]
12
+ }
13
+
14
+ INTERVALS = ["minute", "hour", "day", "week", "month"]
15
+ private
16
+
17
+ helper_method :selected_interval
18
+ def selected_interval
19
+ if params[:interval].in?(INTERVALS)
20
+ params[:interval]
21
+ else
22
+ default_interval_for_period
23
+ end
24
+ end
25
+
26
+ def default_interval_for_period
27
+ default_interval_for_date_range(range)
28
+ end
29
+
30
+ def default_interval_for_date_range(range)
31
+ if range[1].nil?
32
+ # assume we're in a realtime
33
+ return INTERVAL_PERIOD["realtime"][0]
34
+ end
35
+ diff = (range[1] - range[0]).seconds
36
+ if diff.in_months > 1
37
+ "month"
38
+ elsif diff.in_days > 0
39
+ "day"
40
+ else
41
+ "hour"
42
+ end
43
+ end
44
+
45
+ helper_method :available_intervals
46
+ def available_intervals
47
+ if range
48
+ return INTERVAL_PERIOD["realtime"] if range[1].nil?
49
+
50
+ diff = (range[1] - range[0]).seconds.in_days
51
+
52
+ if diff == 0
53
+ INTERVAL_PERIOD["day"]
54
+ elsif diff <= 7
55
+ INTERVAL_PERIOD["7d"]
56
+ elsif diff <= 31
57
+ INTERVAL_PERIOD["month"]
58
+ else
59
+ INTERVAL_PERIOD["all"]
60
+ end
61
+ else
62
+ INTERVAL_PERIOD["month"]
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class BounceRatesController < BaseController
4
+ # @todo: this is lazy
5
+ def index
6
+ @stats = AhoyCaptain::Stats::BounceRatesQuery.call(params)
7
+ @stats = @stats.group_by_period(selected_interval, "daily_bounce_rate.date").average("bounce_rate")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class TotalPageviewsController < BaseController
4
+ def index
5
+ @stats = AhoyCaptain::Stats::TotalPageviewsQuery.call(params).group_by_period(selected_interval, :time).count
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class TotalVisitsController < BaseController
4
+ def index
5
+ @stats = AhoyCaptain::Stats::TotalVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class UniqueVisitorsController < BaseController
4
+ def index
5
+ @stats = AhoyCaptain::Stats::UniqueVisitorsQuery.call(params).group_by_period(selected_interval, :started_at).count
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class ViewsPerVisitsController < BaseController
4
+ include Rangeable
5
+
6
+ # @todo: make me a window func
7
+ 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
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ module Stats
3
+ class VisitDurationsController < BaseController
4
+ def index
5
+ @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).group_by_period(selected_interval, 'started_at').average(:duration)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class StatsController < ApplicationController
3
+ def show
4
+ @presenter = DashboardPresenter.new(params)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ module AhoyCaptain
2
+ class TopPagesController < ApplicationController
3
+ include Limitable
4
+
5
+ before_action do
6
+ if Widget.disabled?(:top_pages)
7
+ raise Widget::WidgetDisabled.new("Widget disabled", :pages)
8
+ end
9
+ end
10
+
11
+ def index
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"))
20
+ .limit(limit)
21
+ end
22
+
23
+ @pages = paginate(results).map { |page| TopPageDecorator.new(page) }
24
+ end
25
+ end
26
+ end