ahoy_captain 0.77 → 0.82

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascript/ahoy_captain/application.js +4 -4
  3. data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +15 -0
  4. data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
  5. data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +9 -10
  6. data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +5 -5
  7. data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +2 -2
  8. data/app/assets/javascript/ahoy_captain/controllers/filter_form_controller.js +13 -0
  9. data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +11 -8
  10. data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +77 -133
  11. data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
  12. data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +10 -0
  13. data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +37 -0
  14. data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +10 -0
  15. data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +9 -8
  16. data/app/assets/javascript/ahoy_captain/controllers/search_select_controller.js +65 -0
  17. data/app/components/ahoy_captain/filter/modal_component.html.erb +6 -5
  18. data/app/components/ahoy_captain/filter/select_component.html.erb +13 -11
  19. data/app/components/ahoy_captain/filter/select_component.rb +21 -4
  20. data/app/components/ahoy_captain/table_component.html.erb +2 -35
  21. data/app/components/ahoy_captain/table_component.rb +13 -5
  22. data/app/components/ahoy_captain/tables/headers/devices_header_component.html.erb +3 -0
  23. data/app/components/ahoy_captain/tables/headers/devices_header_component.rb +9 -0
  24. data/app/components/ahoy_captain/tables/headers/goals_header_component.html.erb +6 -0
  25. data/app/components/ahoy_captain/tables/headers/goals_header_component.rb +9 -0
  26. data/app/components/ahoy_captain/tables/headers/header_component.html.erb +5 -0
  27. data/app/components/ahoy_captain/tables/headers/header_component.rb +12 -0
  28. data/app/components/ahoy_captain/tables/rows/devices_row_component.html.erb +5 -0
  29. data/app/components/ahoy_captain/tables/rows/devices_row_component.rb +12 -0
  30. data/app/components/ahoy_captain/tables/rows/goals_row_component.html.erb +11 -0
  31. data/app/components/ahoy_captain/tables/rows/goals_row_component.rb +12 -0
  32. data/app/components/ahoy_captain/tables/rows/row_component.html.erb +6 -0
  33. data/app/components/ahoy_captain/tables/rows/row_component.rb +41 -0
  34. data/app/controllers/ahoy_captain/application_controller.rb +17 -15
  35. data/app/controllers/ahoy_captain/campaigns_controller.rb +2 -10
  36. data/app/controllers/ahoy_captain/cities_controller.rb +2 -6
  37. data/app/controllers/ahoy_captain/countries_controller.rb +2 -6
  38. data/app/controllers/ahoy_captain/devices_controller.rb +3 -6
  39. data/app/controllers/ahoy_captain/entry_pages_controller.rb +2 -4
  40. data/app/controllers/ahoy_captain/exit_pages_controller.rb +3 -4
  41. data/app/controllers/ahoy_captain/exports_controller.rb +15 -0
  42. data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +2 -2
  43. data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +1 -2
  44. data/app/controllers/ahoy_captain/filters/properties/names_controller.rb +11 -0
  45. data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +15 -0
  46. data/app/controllers/ahoy_captain/realtimes_controller.rb +1 -1
  47. data/app/controllers/ahoy_captain/regions_controller.rb +3 -7
  48. data/app/controllers/ahoy_captain/sources_controller.rb +2 -5
  49. data/app/controllers/ahoy_captain/stats/base_controller.rb +61 -0
  50. data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +4 -2
  51. data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +2 -1
  52. data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +2 -1
  53. data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +2 -1
  54. data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +10 -6
  55. data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +2 -1
  56. data/app/controllers/ahoy_captain/top_pages_controller.rb +2 -8
  57. data/app/decorators/ahoy_captain/application_decorator.rb +27 -3
  58. data/app/decorators/ahoy_captain/campaign_decorator.rb +8 -0
  59. data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
  60. data/app/decorators/ahoy_captain/country_decorator.rb +10 -0
  61. data/app/decorators/ahoy_captain/device_decorator.rb +13 -2
  62. data/app/decorators/ahoy_captain/page_decorator.rb +11 -0
  63. data/app/decorators/ahoy_captain/region_decorator.rb +16 -0
  64. data/app/decorators/ahoy_captain/source_decorator.rb +7 -0
  65. data/app/models/ahoy_captain/export.rb +48 -0
  66. data/app/presenters/ahoy_captain/dashboard_presenter.rb +24 -16
  67. data/app/presenters/ahoy_captain/funnel_presenter.rb +32 -29
  68. data/app/presenters/ahoy_captain/goals_presenter.rb +32 -23
  69. data/app/queries/ahoy_captain/application_query.rb +9 -5
  70. data/app/queries/ahoy_captain/campaign_query.rb +14 -0
  71. data/app/queries/ahoy_captain/city_query.rb +11 -0
  72. data/app/queries/ahoy_captain/country_query.rb +10 -0
  73. data/app/queries/ahoy_captain/device_query.rb +10 -0
  74. data/app/queries/ahoy_captain/entry_pages_query.rb +3 -2
  75. data/app/queries/ahoy_captain/event_query.rb +26 -9
  76. data/app/queries/ahoy_captain/exit_pages_query.rb +6 -4
  77. data/app/queries/ahoy_captain/region_query.rb +11 -0
  78. data/app/queries/ahoy_captain/source_query.rb +10 -0
  79. data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +3 -7
  80. data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +9 -6
  81. data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
  82. data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
  83. data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +2 -2
  84. data/app/queries/ahoy_captain/stats/visit_duration_query.rb +4 -4
  85. data/app/queries/ahoy_captain/top_page_query.rb +13 -0
  86. data/app/queries/ahoy_captain/visit_query.rb +12 -12
  87. data/app/views/ahoy_captain/devices/_table.html.erb +5 -0
  88. data/app/views/ahoy_captain/devices/index.html+details.erb +1 -1
  89. data/app/views/ahoy_captain/devices/index.html.erb +2 -2
  90. data/app/views/ahoy_captain/goals/index.html.erb +3 -35
  91. data/app/views/ahoy_captain/layouts/application.html.erb +1 -1
  92. data/app/views/ahoy_captain/roots/show.html.erb +81 -73
  93. data/app/views/ahoy_captain/stats/base/index.html.erb +8 -1
  94. data/app/views/ahoy_captain/stats/show.html.erb +1 -1
  95. data/config/routes.rb +7 -0
  96. data/lib/ahoy_captain/ahoy/event_methods.rb +29 -75
  97. data/lib/ahoy_captain/configuration.rb +2 -2
  98. data/lib/ahoy_captain/engine.rb +1 -0
  99. data/lib/ahoy_captain/goals.rb +15 -3
  100. data/lib/ahoy_captain/period_collection.rb +1 -1
  101. data/lib/ahoy_captain/version.rb +1 -1
  102. data/lib/generators/ahoy_captain/migration_generator.rb +21 -0
  103. data/lib/generators/ahoy_captain/templates/config.rb.tt +18 -3
  104. data/lib/generators/ahoy_captain/templates/migration.rb.tt +7 -0
  105. metadata +48 -7
  106. data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +0 -145
  107. data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +0 -43
  108. data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +0 -25
  109. data/app/models/ahoy_captain/current.rb +0 -9
  110. data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -1,15 +1,19 @@
1
1
  module AhoyCaptain
2
2
  module Stats
3
3
  class ViewsPerVisitsController < BaseController
4
- include Rangeable
5
-
4
+ # @todo: make me a window func
6
5
  def index
7
- @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_day('views_per_visit_table.started_at').average(:views_per_visit)
8
- (range[0]..range[1]).to_a.each do |date|
9
- unless @stats.key?(date.to_date)
10
- @stats[date.to_date] = 2
6
+ @stats = AhoyCaptain::Stats::ViewsPerVisitQuery.call(params).group_by_period(selected_interval, 'views_per_visit_table.started_at').average(:views_per_visit)
7
+ if range[1]
8
+ (range[0].to_date..range[1].to_date).to_a.each do |date|
9
+ unless @stats.key?(date.to_date)
10
+ @stats[date.to_date] = 2
11
+ end
11
12
  end
12
13
  end
14
+
15
+ @label = "Views"
16
+
13
17
  end
14
18
  end
15
19
  end
@@ -2,7 +2,8 @@ module AhoyCaptain
2
2
  module Stats
3
3
  class VisitDurationsController < BaseController
4
4
  def index
5
- @stats = AhoyCaptain::Stats::VisitDurationQuery.call(params).group('started_at').average(:duration)
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)
@@ -1,10 +1,26 @@
1
1
  module AhoyCaptain
2
2
  class RegionDecorator < CountryDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Country" => :country,
6
+ "Region" => :region,
7
+ "Total" => :unit_amount
8
+ }
9
+ end
10
+
3
11
  def display_name
4
12
  search = search_query(region_eq: object.region, country_eq: object.country)
5
13
  frame_link("#{country_emoji(object.country)} #{object.region}", search)
6
14
  end
7
15
 
16
+ def country
17
+ "#{country_emoji(object.country)} #{object.country}"
18
+ end
19
+
20
+ def region
21
+ object.region
22
+ end
23
+
8
24
  def unit_amount
9
25
  object.count
10
26
  end
@@ -1,5 +1,12 @@
1
1
  module AhoyCaptain
2
2
  class SourceDecorator < ApplicationDecorator
3
+ def self.csv_map(params = {})
4
+ {
5
+ "Domain" => :referring_domain,
6
+ "Total" => :unit_amount
7
+ }
8
+ end
9
+
3
10
  def display_name
4
11
  display = %Q(
5
12
  <div class='flex justify-start space-x-8 col-span-1 items-center'>
@@ -0,0 +1,48 @@
1
+ module AhoyCaptain
2
+ class Export
3
+ def initialize(params, context)
4
+ @params = params
5
+ @context = context
6
+ @files = {}
7
+ end
8
+
9
+ def build
10
+ @files["browsers.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: "browser")), DeviceDecorator)
11
+ @files["cities.csv"] = to_csv(CityQuery.call(merged_params), CityDecorator)
12
+ @files["countries.csv"] = to_csv(CountryQuery.call(merged_params), CountryDecorator)
13
+ @files["devices.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: :device_type)), DeviceDecorator)
14
+ @files["entry_pages.csv"] = to_csv(EntryPagesQuery.call(merged_params), EntryPageDecorator)
15
+ @files["exit_pages.csv"] = to_csv(ExitPagesQuery.call(merged_params), ExitPageDecorator)
16
+ @files["operating_systems.csv"] = to_csv(DeviceQuery.call(merged_params(devices_type: "os")), DeviceDecorator)
17
+ @files["top_pages.csv"] = to_csv(TopPageQuery.call(merged_params), TopPageDecorator)
18
+ @files["regions.csv"] = to_csv(RegionQuery.call(merged_params), RegionDecorator)
19
+ @files["sources.csv"] = to_csv(SourceQuery.call(merged_params), SourceDecorator)
20
+ ["campaign", "content", "medium", "source", "term"].each do |utm|
21
+ @files["utm_#{utm.pluralize}.csv"] = to_csv(CampaignQuery.call(merged_params(campaigns_type: "utm_#{utm}")), CampaignDecorator)
22
+ end
23
+ self
24
+ end
25
+
26
+ def to_zip
27
+ zip_stream = Zip::OutputStream.write_buffer do |zip|
28
+ @files.each do |filename, csv|
29
+ zip.put_next_entry(filename)
30
+ zip.write(csv)
31
+ end
32
+ end
33
+
34
+ zip_stream.rewind
35
+ zip_stream
36
+ end
37
+
38
+ private
39
+
40
+ def to_csv(query, decorator)
41
+ decorator.to_csv(query, @context)
42
+ end
43
+
44
+ def merged_params(params_to_merge = {})
45
+ @params.dup.merge(params_to_merge)
46
+ end
47
+ end
48
+ end
@@ -10,27 +10,32 @@ module AhoyCaptain
10
10
 
11
11
  def unique_visitors
12
12
  cached(:unique_visitors) do
13
- Stats::UniqueVisitorsQuery.call(params).count(:ip)
13
+ Stats::UniqueVisitorsQuery.call(params).count(:visitor_token)
14
14
  end
15
15
  end
16
16
 
17
17
  def total_visits
18
18
  cached(:total_visits) do
19
- Stats::TotalVisitorsQuery.call(params).count
19
+ Stats::TotalVisitorsQuery.call(params).count(:id)
20
20
  end
21
21
  end
22
22
 
23
23
  def total_pageviews
24
24
  cached(:total_pageviews) do
25
- Stats::TotalPageviewsQuery.call(params).count
25
+ Stats::TotalPageviewsQuery.call(params).count(:id)
26
26
  end
27
27
  end
28
28
 
29
29
  def views_per_visit
30
30
  cached(:views_per_visit) do
31
31
  begin
32
- result = Stats::AverageViewsPerVisitQuery.call(params).count
33
- (result.values.sum.to_f / result.size).round(2)
32
+ result = Stats::AverageViewsPerVisitQuery.call(params).count(:id)
33
+ count = (result.values.sum.to_f / result.size).round(2)
34
+ if count.nan?
35
+ return "0"
36
+ else
37
+ return count
38
+ end
34
39
  rescue ::ActiveRecord::StatementInvalid => e
35
40
  if e.message.include?("PG::DivisionByZero")
36
41
  return "0"
@@ -39,23 +44,26 @@ module AhoyCaptain
39
44
  end
40
45
  end
41
46
  end
42
-
43
47
  end
44
48
 
45
49
  def bounce_rate
46
50
  cached(:bounce_rate) do
47
51
  begin
48
- result = Stats::BounceRatesQuery.call(params)
49
- result[0].bounce_rate.round(2)
52
+ result = Stats::BounceRatesQuery.call(params)
53
+ average = result.average("bounce_rate")
54
+ if average
55
+ average.round(2)
56
+ else
57
+ "0"
58
+ end
50
59
  rescue ::ActiveRecord::StatementInvalid => e
51
- if e.message.include?("PG::DivisionByZero")
52
- return "0"
53
- else
54
- raise e
60
+ if e.message.include?("PG::DivisionByZero")
61
+ return "0"
62
+ else
63
+ raise e
64
+ end
55
65
  end
56
66
  end
57
-
58
- end
59
67
  end
60
68
 
61
69
  def visit_duration
@@ -63,7 +71,7 @@ module AhoyCaptain
63
71
  result = Stats::AverageVisitDurationQuery.call(params)
64
72
  duration = result[0].average_visit_duration
65
73
  if duration
66
- "#{duration.parts[:minutes]}M #{duration.parts[:seconds].round}S"
74
+ "#{duration.in_minutes.to_i}M #{duration.parts[:seconds].to_i}S"
67
75
  else
68
76
  "0M 0S"
69
77
  end
@@ -73,7 +81,7 @@ module AhoyCaptain
73
81
  private
74
82
 
75
83
  def cached(*names)
76
- 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
84
+ 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
77
85
  yield
78
86
  end
79
87
  end
@@ -9,44 +9,47 @@ module AhoyCaptain
9
9
  end
10
10
 
11
11
  def build
12
- queries = {}
13
- prev_goal = nil
14
- prev_table = nil
15
- selects = []
16
- @funnel.goals.each do |goal|
17
- if prev_goal
18
- query = ::Ahoy::Event
19
- .select("distinct ahoy_events.visit_id")
20
- .from(prev_table.to_s)
21
- .joins("inner join ahoy_events on ahoy_events.visit_id = #{prev_table}.id")
22
- .where("ahoy_events.name = ?", goal.event_name.to_s).to_sql
23
- prev_table = "#{goal.id}"
24
- selects << ["SELECT '#{prev_goal.title} > #{goal.title}' as step, count(*) from #{prev_table}"]
25
- queries[prev_table] = query
26
- else
27
- prev_table = :visitors
28
- prev_goal = goal
12
+ if AhoyCaptain.config.goals.none?
13
+ @goals = []
14
+ return self
15
+ end
29
16
 
30
- query = @event_query
31
- .select("distinct(visit_id) as id, min(time) as min_time")
32
- .where(name: goal.event_name.to_s)
33
- .group("1").to_sql
34
- selects << ["'#{goal.title}' as step, count(*) from #{prev_table}"]
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
35
23
 
36
- queries[prev_table] = query
37
- end
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
38
29
  end
39
30
 
40
- select = selects.join(" UNION ").delete_suffix(" from #{prev_table}")
41
-
31
+ # activerecord quirk / with bug
32
+ select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
33
+ select = select.delete_prefix("SELECT ")
42
34
  steps = ::Ahoy::Event.with(
43
- queries
44
- ).select(select).from(prev_table).order("count desc")
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 = []
45
41
 
46
- @steps = ::Ahoy::Event.with(steps: steps).select("step, count, lag(count, 1) over () as lag, abs(count::numeric - lag(count, 1) over ())::integer as drop_off, round((1.0 - count::numeric/GREATEST(lag(count, 1) over (), 1)),2) as conversion_rate").from("steps")
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
47
49
  self
48
50
  end
49
51
 
52
+
50
53
  def total
51
54
  @event_query.distinct(:visitor_token).count
52
55
  end
@@ -6,44 +6,53 @@ module AhoyCaptain
6
6
  @goals = nil
7
7
  end
8
8
 
9
+ # this is a dumpster fire
9
10
  def build
10
11
  if AhoyCaptain.config.goals.none?
11
12
  @goals = []
12
13
  return self
13
14
  end
14
- queries = {}
15
- selects = []
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"]
16
20
  last_goal = nil
17
- map = {}
18
- AhoyCaptain.config.goals.each do |goal|
19
- queries[goal.id] = @event_query.select("count(distinct(visit_id)) as uniques, count(name) as total, name").where(name: goal.event_name).group("name")
20
- selects << ["SELECT total, uniques, name from #{goal.id}"]
21
- map[goal.event_name] = goal
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
22
34
  last_goal = goal
23
35
  end
36
+
37
+ # activerecord quirk / with bug
24
38
  select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
25
39
  select = select.delete_prefix("SELECT ")
26
40
  steps = ::Ahoy::Event.with(
27
- queries
28
- ).select(select).from("#{last_goal.id}")
29
-
30
- items = ::Ahoy::Event.with(steps: steps).select("total, uniques, name, 0 as conversion_rate").from("steps").index_by(&:name)
31
- @goals = []
32
- map.each do |name, _|
33
- if items[name]
34
- items[name].name = map[name].title
35
- items[name].conversion_rate = ((items[name].total / total.to_d) * 100).round(2) * 100
36
- @goals << items[name]
37
- else
38
- @goals << OpenStruct.new(name: map[name].title, uniques: 0, total: 0, conversion_rate: 0)
39
- end
40
- end
41
+ queries,
42
+ ).select(select).from("#{last_goal.id}").order("sort_order asc").index_by(&:name)
43
+ totals = steps.delete("_internal_total_visits_")
41
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
42
51
  self
43
52
  end
44
53
 
45
- def total
46
- @total ||= @event_query.distinct(:visitor_token).count
54
+ def total_visitors
55
+ @total_visitors ||= @event_query.select(:visit_id).distinct.count
47
56
  end
48
57
 
49
58
  def as_json
@@ -21,6 +21,9 @@ module AhoyCaptain
21
21
  @query = query
22
22
  end
23
23
 
24
+ def inspect
25
+ "<#{self.class.name}>"
26
+ end
24
27
  protected
25
28
 
26
29
  def build
@@ -50,6 +53,7 @@ module AhoyCaptain
50
53
  EventQuery.call(params)
51
54
  end
52
55
 
56
+ # this could be better
53
57
  def ransack_params_for(type)
54
58
  ransackable_params = {}
55
59
 
@@ -58,12 +62,13 @@ module AhoyCaptain
58
62
  visit: (AhoyCaptain.visit.ransackable_attributes + AhoyCaptain.visit.ransackable_scopes).map(&:to_s),
59
63
  event: (AhoyCaptain.event.ransackable_attributes + AhoyCaptain.event.ransackable_scopes).map(&:to_s),
60
64
  }
65
+
61
66
  pattern = /(?:_not_eq|_eq|_in|_not_in|_cont|_not_cont|_i_cont)$/
62
67
  params[:q].each do |key, value|
63
68
  attribute_name = key.gsub(pattern, '')
64
- if type == :event && ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key)
69
+ if type == :event && (ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key))
65
70
  ransackable_params["visit_#{key}"] = value
66
- elsif type == :visit && ransackable_attributes[:event].include?(attribute_name) || ransackable_attributes[:event].include?(key)
71
+ elsif type == :visit && (ransackable_attributes[:event].include?(attribute_name) || ransackable_attributes[:event].include?(key))
67
72
  ransackable_params["events_#{key}"] = value
68
73
  else
69
74
  ransackable_params[key] = value
@@ -90,8 +95,8 @@ module AhoyCaptain
90
95
  if range.size == 2
91
96
  ransackable_params["started_at_gt"] = range[0]
92
97
  ransackable_params["started_at_lt"] = range[1]
93
- ransackable_params["events_time_gt"] = range[0]
94
- ransackable_params["events_time_lt"] = range[1]
98
+ ransackable_params["events_time_gteq"] = range[0]
99
+ ransackable_params["events_time_lteq"] = range[1]
95
100
  else
96
101
  ransackable_params["started_at_gt"] = range[0]
97
102
  ransackable_params["events_time_gt"] = range[0]
@@ -99,7 +104,6 @@ module AhoyCaptain
99
104
  end
100
105
  end
101
106
 
102
-
103
107
  ransackable_params
104
108
  end
105
109
 
@@ -0,0 +1,14 @@
1
+ module AhoyCaptain
2
+ class CampaignQuery < ApplicationQuery
3
+ def build
4
+ visit_query
5
+ .select(
6
+ "COALESCE(#{params[:campaigns_type]}, 'Direct/None') as label",
7
+ "count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) as count",
8
+ "sum(count(COALESCE(#{params[:campaigns_type]}, 'Direct/None'))) OVER() as total_count"
9
+ )
10
+ .group("COALESCE(#{params[:campaigns_type]}, 'Direct/None')")
11
+ .order(Arel.sql("count(COALESCE(#{params[:campaigns_type]}, 'Direct/None')) desc"))
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module AhoyCaptain
2
+ class CityQuery < ApplicationQuery
3
+ def build
4
+ visit_query
5
+ .select("city, country, count(concat(city, region, country)) as count, sum(count(concat(city, region, country))) over() as total_count")
6
+ .where.not(city: nil)
7
+ .group("city, region, country")
8
+ .order(Arel.sql "count(concat(city, region, country)) desc")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module AhoyCaptain
2
+ class CountryQuery < ApplicationQuery
3
+ def build
4
+ visit_query
5
+ .reselect("country as label, count(country) as count, sum(count(country)) OVER() as total_count")
6
+ .group("country")
7
+ .order("count(country) desc")
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module AhoyCaptain
2
+ class DeviceQuery < ApplicationQuery
3
+ def build
4
+ visit_query
5
+ .select("#{params[:devices_type]} as label", "count(#{params[:devices_type]}) as count", "sum(count(#{params[:devices_type]})) over() as total_count")
6
+ .group(params[:devices_type])
7
+ .order("count(#{params[:devices_type]}) desc")
8
+ end
9
+ end
10
+ end