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,34 @@
1
+ module AhoyCaptain
2
+ class ApplicationDecorator
3
+ attr_reader :object
4
+
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ private
10
+
11
+ def h
12
+ @h ||= Current.request.view_context
13
+ end
14
+
15
+ def params
16
+ h.params
17
+ end
18
+
19
+ def search_query(args = {})
20
+ query = h.search_params.dup
21
+ query[:q] ||= {}
22
+ args.each { |k,v| query[:q]["#{k}"] = v }
23
+ query.to_query
24
+ end
25
+
26
+ def frame_link(label, search)
27
+ h.link_to(label, "#{AhoyCaptain::Engine.routes.url_helpers.root_path}?#{search}", data: { turbo_frame: "_top" }).html_safe
28
+ end
29
+
30
+ def request
31
+ Current.request.request
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ module AhoyCaptain
2
+ class CampaignDecorator < ApplicationDecorator
3
+ def display_name
4
+ if object.label == "Direct/None"
5
+ value = ""
6
+ else
7
+ value = object.label
8
+ end
9
+
10
+ search = search_query("#{params[:campaigns_type]}_eq" => value)
11
+
12
+ frame_link(object.label, search)
13
+ end
14
+
15
+ def unit_amount
16
+ object.count
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module AhoyCaptain
2
+ class CityDecorator < CountryDecorator
3
+ def display_name
4
+ search = search_query(country_eq: object.country, city_eq: object.city)
5
+ frame_link("#{country_emoji(object.country)} #{object.city}", search)
6
+ end
7
+
8
+ def unit_amount
9
+ object.count
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ module AhoyCaptain
2
+ class CountryDecorator < ApplicationDecorator
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
+
5
+ def display_name
6
+ search = search_query(country_eq: object.label)
7
+ frame_link("#{country_emoji(country)} #{country}", search)
8
+ end
9
+
10
+ def unit_amount
11
+ object.count
12
+ end
13
+
14
+ private
15
+
16
+ def country_emoji(iso)
17
+ if iso
18
+ EMOJI_MAP[iso.to_sym]
19
+ else
20
+ "🏴‍️"
21
+ end
22
+ end
23
+
24
+ def country
25
+ object.label
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module AhoyCaptain
2
+ class DeviceDecorator < ApplicationDecorator
3
+ def display_name
4
+ search = search_query("#{params[:devices_type]}_eq" => object.label)
5
+ frame_link(object.label, search)
6
+ end
7
+
8
+ def unit_amount
9
+ object.count
10
+ end
11
+
12
+ def total_count
13
+ object.total_count
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class EntryPageDecorator < PageDecorator
3
+ def type
4
+ :entry_page_in
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class ExitPageDecorator < PageDecorator
3
+ def type
4
+ :exit_page_in
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module AhoyCaptain
2
+ class PageDecorator < ApplicationDecorator
3
+ def display_name
4
+ search = search_query(type => object.url)
5
+ frame_link(object.url, search)
6
+ end
7
+
8
+ def unit_amount
9
+ object.count
10
+ end
11
+
12
+ def type
13
+ raise NotImplementedError
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ module AhoyCaptain
2
+ class RegionDecorator < CountryDecorator
3
+ def display_name
4
+ search = search_query(region_eq: object.region, country_eq: object.country)
5
+ frame_link("#{country_emoji(object.country)} #{object.region}", search)
6
+ end
7
+
8
+ def unit_amount
9
+ object.count
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module AhoyCaptain
2
+ class SourceDecorator < ApplicationDecorator
3
+ def display_name
4
+ display = %Q(
5
+ <div class='flex justify-start space-x-8 col-span-1 items-center'>
6
+ <img class='transparent w-5 h-5'
7
+ src='https://www.google.com/s2/favicons?domain=#{object.referring_domain}&sz=32'
8
+ />
9
+ <span>#{object.referring_domain}</span>
10
+ </div>
11
+ ).html_safe
12
+ path = search_query(ref_domain_eq: object.referring_domain)
13
+ frame_link(display, path)
14
+ end
15
+
16
+ def unit_amount
17
+ object.count
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module AhoyCaptain
2
+ class TopPageDecorator < PageDecorator
3
+ def type
4
+ :route_eq
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ module AhoyCaptain
2
+ module ApplicationHelper
3
+ include Pagy::Frontend
4
+
5
+ SPECIAL_PARAMS = [:campaigns_type, :devices_type]
6
+ def ahoy_captain_importmap_tags(entry_point = "application", shim: true)
7
+ safe_join [
8
+ (javascript_importmap_shim_tag if shim),
9
+ (javascript_importmap_shim_nonce_configuration_tag if shim),
10
+ javascript_import_module_tag(entry_point),
11
+ javascript_importmap_module_preload_tags(AhoyCaptain.importmap),
12
+ javascript_inline_importmap_tag(AhoyCaptain.importmap.to_json(resolver: self)),
13
+ ].compact, "\n"
14
+ end
15
+
16
+ def search_params
17
+ request.query_parameters
18
+ end
19
+
20
+ def special_params
21
+ params.to_unsafe_h.slice(*SPECIAL_PARAMS)
22
+ end
23
+
24
+ def render_pagination
25
+ if @pagination
26
+ pagy_nav(@pagination).html_safe
27
+ else
28
+ ""
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module AhoyCaptain
2
+ class Current < ActiveSupport::CurrentAttributes
3
+ attribute :request
4
+
5
+ after_reset do
6
+ self.request = nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module AhoyCaptain
2
+ module Rangeable
3
+ include AhoyCaptain::RangeOptions
4
+
5
+ def period
6
+ params[:period] || AhoyCaptain.config.ranges.default
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ module AhoyCaptain
2
+ class UrlHelpers
3
+ include AhoyCaptain::Engine.routes.url_helpers
4
+ include Singleton
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module AhoyCaptain
2
+ class Widget
3
+ class WidgetDisabled < StandardError
4
+ attr_reader :frame
5
+ def initialize(msg, frame = nil)
6
+ @frame = frame
7
+ super(msg)
8
+ end
9
+ end
10
+
11
+ def self.disabled?(*names)
12
+ AhoyCaptain.config.disabled_widgets.include?(names.join("."))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module AhoyCaptain
2
+ module RangeOptions
3
+
4
+ def period
5
+ raise NotImplementedError
6
+ end
7
+
8
+ private def range
9
+ if params[:start_date].present? && params[:end_date].present?
10
+ custom = [params[:start_date].to_datetime, params[:end_date].to_datetime].sort
11
+ duration = (custom[1].to_date - custom[0].to_date)
12
+
13
+ if AhoyCaptain.config.ranges.max && (duration.days <= AhoyCaptain.config.ranges.max)
14
+ return custom
15
+ end
16
+ end
17
+
18
+ AhoyCaptain.config.ranges.for(period)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,84 @@
1
+ module AhoyCaptain
2
+ class DashboardPresenter
3
+ include RangeOptions
4
+
5
+ attr_reader :params
6
+
7
+ def initialize(params)
8
+ @params = params
9
+ end
10
+
11
+ def unique_visitors
12
+ cached(:unique_visitors) do
13
+ Stats::UniqueVisitorsQuery.call(params).count(:visitor_token)
14
+ end
15
+ end
16
+
17
+ def total_visits
18
+ cached(:total_visits) do
19
+ Stats::TotalVisitorsQuery.call(params).count(:id)
20
+ end
21
+ end
22
+
23
+ def total_pageviews
24
+ cached(:total_pageviews) do
25
+ Stats::TotalPageviewsQuery.call(params).count(:id)
26
+ end
27
+ end
28
+
29
+ def views_per_visit
30
+ cached(:views_per_visit) do
31
+ begin
32
+ result = Stats::AverageViewsPerVisitQuery.call(params).count(:id)
33
+ (result.values.sum.to_f / result.size).round(2)
34
+ rescue ::ActiveRecord::StatementInvalid => e
35
+ if e.message.include?("PG::DivisionByZero")
36
+ return "0"
37
+ else
38
+ raise e
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def bounce_rate
45
+ cached(:bounce_rate) do
46
+ begin
47
+ result = Stats::BounceRatesQuery.call(params)
48
+ average = result.average("bounce_rate")
49
+ if average
50
+ average.round(2)
51
+ else
52
+ "0"
53
+ end
54
+ rescue ::ActiveRecord::StatementInvalid => e
55
+ if e.message.include?("PG::DivisionByZero")
56
+ return "0"
57
+ else
58
+ raise e
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def visit_duration
65
+ cached(:visit_duration) do
66
+ result = Stats::AverageVisitDurationQuery.call(params)
67
+ duration = result[0].average_visit_duration
68
+ if duration
69
+ "#{duration.in_minutes.to_i}M #{duration.parts[:seconds].to_i}S"
70
+ else
71
+ "0M 0S"
72
+ end
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def cached(*names)
79
+ AhoyCaptain.cache.fetch("ahoy_captain:#{names.join(":")}:#{params.permit!.except("controller", "action").to_unsafe_h.map { |k,v| "#{k}-#{v}" }.join(":")}", expire_in: AhoyCaptain.config.cache[:ttl]) do
80
+ yield
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,68 @@
1
+ module AhoyCaptain
2
+ # this is incredibly naive and needs some tlc
3
+ class FunnelPresenter
4
+
5
+ attr_reader :steps
6
+ def initialize(funnel, event_query)
7
+ @funnel = funnel
8
+ @event_query = event_query.joins(:visit)
9
+ end
10
+
11
+ def build
12
+ if AhoyCaptain.config.goals.none?
13
+ @goals = []
14
+ return self
15
+ end
16
+
17
+ queries = {
18
+ totals: @event_query.select("count(distinct(#{AhoyCaptain.event.table_name}.visit_id)) as unique_visits, '_internal_total_visits_' as name, count(distinct #{AhoyCaptain.event.table_name}.id) as total_events, 0 as sort_order")
19
+ }
20
+ selects = ["SELECT unique_visits, name, total_events, sort_order from totals"]
21
+ last_goal = nil
22
+ map = {}.with_indifferent_access
23
+
24
+ AhoyCaptain.config.goals.each_with_index do |goal, index|
25
+ queries[goal.id] = @event_query.select("count(distinct(#{AhoyCaptain.event.table_name}.visit_id)) as unique_visits, '#{goal.id}' as name, count(distinct #{AhoyCaptain.event.table_name}.id) as total_events, #{index + 1} as sort_order").merge(goal.event_query.call).group("#{AhoyCaptain.event.table_name}.name")
26
+ selects << ["SELECT unique_visits, name, total_events, sort_order from #{goal.id}"]
27
+ map[goal.id] = goal
28
+ last_goal = goal
29
+ end
30
+
31
+ # activerecord quirk / with bug
32
+ select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
33
+ select = select.delete_prefix("SELECT ")
34
+ steps = ::Ahoy::Event.with(
35
+ queries,
36
+ ).select(select).from("#{last_goal.id}").order("sort_order asc")
37
+
38
+ items = ::Ahoy::Event.with(steps: steps).select("total_events, unique_visits, name, round((total_events::numeric/lag(total_events, 1) over ()),2) as drop_off").from("steps").order("sort_order asc").index_by(&:name)
39
+ items.delete("_internal_total_visits_")
40
+ @steps = []
41
+
42
+ items.values.each do |item|
43
+ if map[item.name]
44
+ item.name = map[item.name].title
45
+ end
46
+ end
47
+
48
+ @steps = items.values
49
+ self
50
+ end
51
+
52
+
53
+ def total
54
+ @event_query.distinct(:visitor_token).count
55
+ end
56
+
57
+ def as_json
58
+ {
59
+ steps: @steps.as_json,
60
+ total: total
61
+ }
62
+ end
63
+
64
+ def to_json
65
+ as_json.to_json
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,69 @@
1
+ module AhoyCaptain
2
+ class GoalsPresenter
3
+ attr_reader :goals
4
+ def initialize(event_query)
5
+ @event_query = event_query
6
+ @goals = nil
7
+ end
8
+
9
+ # this is a dumpster fire
10
+ def build
11
+ if AhoyCaptain.config.goals.none?
12
+ @goals = []
13
+ return self
14
+ end
15
+
16
+ queries = {
17
+ totals: @event_query.select("count(distinct(#{AhoyCaptain.event.table_name}.visit_id)) as unique_visits, '_internal_total_visits_' as name, count(distinct #{AhoyCaptain.event.table_name}.id) as total_events, 0 as sort_order")
18
+ }
19
+ selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr from totals"]
20
+ last_goal = nil
21
+ map = {}.with_indifferent_access
22
+
23
+ AhoyCaptain.config.goals.each_with_index do |goal, index|
24
+ queries[goal.id] = @event_query.select(
25
+ [
26
+ "count(distinct(#{AhoyCaptain.event.table_name}.visit_id)) as unique_visits" ,
27
+ "'#{goal.id}' as name",
28
+ "count(distinct #{AhoyCaptain.event.table_name}.id) as total_events",
29
+ "#{index + 1} as sort_order",
30
+ ]
31
+ ).merge(goal.event_query.call).group("#{AhoyCaptain.event.table_name}.name")
32
+ selects << ["SELECT unique_visits, name, total_events, sort_order, 0::decimal as cr from #{goal.id}"]
33
+ map[goal.id] = goal
34
+ last_goal = goal
35
+ end
36
+
37
+ # activerecord quirk / with bug
38
+ select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
39
+ select = select.delete_prefix("SELECT ")
40
+ steps = ::Ahoy::Event.with(
41
+ queries,
42
+ ).select(select).from("#{last_goal.id}").order("sort_order asc").index_by(&:name)
43
+ totals = steps.delete("_internal_total_visits_")
44
+
45
+ @goals = steps.keys.collect do |name|
46
+ step = steps[name]
47
+ step.name = map[name].title
48
+ step.cr = ((step.total_events.to_d / totals.total_events.to_d) * 100).round(2)
49
+ step
50
+ end
51
+ self
52
+ end
53
+
54
+ def total_visitors
55
+ @total_visitors ||= @event_query.select(:visit_id).distinct.count
56
+ end
57
+
58
+ def as_json
59
+ {
60
+ steps: @steps.as_json,
61
+ total: total
62
+ }
63
+ end
64
+
65
+ def to_json
66
+ as_json.to_json
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,121 @@
1
+ module AhoyCaptain
2
+ class ApplicationQuery
3
+ # if you want to enforce returning a relation
4
+ class_attribute :strict, default: true
5
+ include Rangeable
6
+
7
+ delegate_missing_to :@query
8
+
9
+ def self.inherited(klass)
10
+ klass.protected_methods :build
11
+ klass.private_methods :call
12
+ end
13
+
14
+ def self.call(params, query = nil)
15
+ new(params, query).send(:call)
16
+ end
17
+
18
+ attr_reader :params
19
+ def initialize(params, query)
20
+ @params = params
21
+ @query = query
22
+ end
23
+
24
+ def inspect
25
+ "<#{self.class.name}>"
26
+ end
27
+ protected
28
+
29
+ def build
30
+ raise NotImplementedError
31
+ end
32
+
33
+ private
34
+
35
+ def call
36
+ @query = build
37
+
38
+ if self.class.strict?
39
+ if @query.is_a?(ActiveRecord::Relation) || @query.class != self.class
40
+ else
41
+ raise ArgumentError, "#{self.class.name} has strict enabled, and should return a relation. Returned a #{@query.class}."
42
+ end
43
+ end
44
+
45
+ self
46
+ end
47
+
48
+ def visit_query
49
+ VisitQuery.call(params)
50
+ end
51
+
52
+ def event_query
53
+ EventQuery.call(params)
54
+ end
55
+
56
+ def ransack_params_for(type)
57
+ ransackable_params = {}
58
+
59
+ if params[:q]
60
+ ransackable_attributes = {
61
+ visit: (AhoyCaptain.visit.ransackable_attributes + AhoyCaptain.visit.ransackable_scopes).map(&:to_s),
62
+ event: (AhoyCaptain.event.ransackable_attributes + AhoyCaptain.event.ransackable_scopes).map(&:to_s),
63
+ }
64
+ pattern = /(?:_not_eq|_eq|_in|_not_in|_cont|_not_cont|_i_cont)$/
65
+ params[:q].each do |key, value|
66
+ attribute_name = key.gsub(pattern, '')
67
+ if type == :event && ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key)
68
+ ransackable_params["visit_#{key}"] = value
69
+ elsif type == :visit && ransackable_attributes[:event].include?(attribute_name) || ransackable_attributes[:event].include?(key)
70
+ ransackable_params["events_#{key}"] = value
71
+ else
72
+ ransackable_params[key] = value
73
+ end
74
+ end
75
+ end
76
+
77
+ # send the right format
78
+ # ::Ahoy::Visit.ransack(events_time_lt: Time.now).result.to_sql
79
+ # is not
80
+ # ::Ahoy::Visit.ransack(events_time_lt: Time.now.to_i).result.to_sql
81
+ if range
82
+ if type == :event
83
+ if range.size == 2
84
+ ransackable_params['time_gt'] = range[0]
85
+ ransackable_params['time_lt'] = range[1]
86
+ ransackable_params["visit_started_at_gt"] = range[0]
87
+ ransackable_params["visit_started_at_lt"] = range[1]
88
+ else
89
+ ransackable_params['time_gt'] = range[0]
90
+ ransackable_params["visit_started_at_gt"] = range[0]
91
+ end
92
+ elsif type == :visit
93
+ if range.size == 2
94
+ ransackable_params["started_at_gt"] = range[0]
95
+ ransackable_params["started_at_lt"] = range[1]
96
+ ransackable_params["events_time_gteq"] = range[0]
97
+ ransackable_params["events_time_lteq"] = range[1]
98
+ else
99
+ ransackable_params["started_at_gt"] = range[0]
100
+ ransackable_params["events_time_gt"] = range[0]
101
+ end
102
+ end
103
+ end
104
+
105
+
106
+ ransackable_params
107
+ end
108
+
109
+ # merge both sets of ransackable params and ensure that they're being set on the correct association
110
+ # if we have params that reach across associations
111
+ def ransack_params
112
+ if self.class.name == "AhoyCaptain::EventQuery"
113
+ ransack_params_for(:event)
114
+ elsif self.class.name == "AhoyCaptain::VisitQuery"
115
+ ransack_params_for(:visit)
116
+ else
117
+ raise ArgumentError, "use ransack_params_for(type)"
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,17 @@
1
+ module AhoyCaptain
2
+ class EntryPagesQuery < ApplicationQuery
3
+
4
+ def build
5
+ max_id_query = @query.with_routes.select("min(#{AhoyCaptain.event.table_name}.id) as id").group("visit_id")
6
+ @query = @query.with_routes.select(
7
+ "#{AhoyCaptain.config.event[:url_column]} as url",
8
+ "count(#{AhoyCaptain.config.event[:url_column]}) as count",
9
+ "sum(count(#{AhoyCaptain.config.event[:url_column]})) over() as total_count"
10
+ )
11
+ .where(id: max_id_query)
12
+ .group(AhoyCaptain.config.event[:url_column])
13
+ end
14
+
15
+
16
+ end
17
+ end