ahoy_captain 0.8 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -13
- data/app/assets/javascript/ahoy_captain/application.js +4 -4
- data/app/assets/javascript/ahoy_captain/controllers/active_links_controller.js +30 -0
- data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
- data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +16 -9
- data/app/assets/javascript/ahoy_captain/controllers/combobox_controller.js +341 -0
- data/app/assets/javascript/ahoy_captain/controllers/details_modal_controller.js +5 -5
- data/app/assets/javascript/ahoy_captain/controllers/dropdown_label_controller.js +2 -2
- data/app/assets/javascript/ahoy_captain/controllers/filter/item_controller.js +12 -0
- data/app/assets/javascript/ahoy_captain/controllers/filter_form_controller.js +13 -0
- data/app/assets/javascript/ahoy_captain/controllers/filter_modal_controller.js +45 -0
- data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +116 -104
- data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
- data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +8 -3
- data/app/assets/javascript/ahoy_captain/controllers/line_chart_controller.js +188 -0
- data/app/assets/javascript/ahoy_captain/controllers/predicate_select_controller.js +9 -0
- data/app/assets/javascript/ahoy_captain/controllers/properties_controller.js +8 -0
- data/app/assets/javascript/ahoy_captain/controllers/property_filter_controller.js +46 -0
- data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +12 -9
- data/app/assets/javascript/ahoy_captain/controllers/tile_controller.js +9 -0
- data/app/assets/javascript/ahoy_captain/controllers/toggle_controller.js +17 -0
- data/app/assets/javascript/ahoy_captain/helpers/chart_utils.js +156 -0
- data/app/assets/javascript/ahoy_captain/helpers/number_formatters.js +55 -0
- data/app/components/ahoy_captain/combobox_component.html.erb +33 -0
- data/app/components/ahoy_captain/combobox_component.rb +13 -0
- data/app/components/ahoy_captain/comparison_link_component.rb +40 -0
- data/app/components/ahoy_captain/dropdown_button_component.html.erb +5 -5
- data/app/components/ahoy_captain/dropdown_link_component.html.erb +5 -5
- data/app/components/ahoy_captain/filter/dropdown_component.html.erb +50 -0
- data/app/components/ahoy_captain/filter/dropdown_component.rb +51 -0
- data/app/components/ahoy_captain/filter/modal_component.html.erb +12 -9
- data/app/components/ahoy_captain/filter/select_component.html.erb +23 -19
- data/app/components/ahoy_captain/filter/select_component.rb +41 -9
- data/app/components/ahoy_captain/filter/tag_component.html.erb +8 -4
- data/app/components/ahoy_captain/filter/tag_component.rb +6 -30
- data/app/components/ahoy_captain/filter/tag_container_component.html.erb +2 -3
- data/app/components/ahoy_captain/filter/tag_container_component.rb +1 -8
- data/app/components/ahoy_captain/stats/comparable_container_component.html.erb +25 -0
- data/app/components/ahoy_captain/stats/comparable_container_component.rb +86 -0
- data/app/components/ahoy_captain/stats/container_component.html.erb +15 -0
- data/app/components/ahoy_captain/stats/container_component.rb +26 -0
- data/app/components/ahoy_captain/sticky_nav_component.html.erb +13 -22
- data/app/components/ahoy_captain/sticky_nav_component.rb +11 -0
- data/app/components/ahoy_captain/table_component.html.erb +4 -37
- data/app/components/ahoy_captain/table_component.rb +25 -5
- data/app/components/ahoy_captain/tables/devices_table_component.rb +11 -0
- data/app/components/ahoy_captain/tables/dynamic_table.rb +13 -0
- data/app/components/ahoy_captain/tables/dynamic_table_component.rb +204 -0
- data/app/components/ahoy_captain/tables/goals_table_component.rb +17 -0
- data/app/components/ahoy_captain/tables/header_component.html.erb +5 -0
- data/app/components/ahoy_captain/tables/header_component.rb +18 -0
- data/app/components/ahoy_captain/tables/headers/header_component.html.erb +5 -0
- data/app/components/ahoy_captain/tables/headers/header_component.rb +16 -0
- data/app/components/ahoy_captain/tables/properties_table_component.rb +27 -0
- data/app/components/ahoy_captain/tables/row_component.html.erb +4 -0
- data/app/components/ahoy_captain/tables/rows/row_component.html.erb +6 -0
- data/app/components/ahoy_captain/tables/rows/row_component.rb +40 -0
- data/app/components/ahoy_captain/tile_component.html.erb +21 -10
- data/app/components/ahoy_captain/tile_component.rb +3 -2
- data/app/components/ahoy_captain/tooltip_component.html.erb +2 -2
- data/app/controllers/ahoy_captain/application_controller.rb +19 -29
- data/app/controllers/ahoy_captain/campaigns_controller.rb +2 -10
- data/app/controllers/ahoy_captain/cities_controller.rb +2 -6
- data/app/controllers/ahoy_captain/countries_controller.rb +2 -6
- data/app/controllers/ahoy_captain/devices_controller.rb +3 -6
- data/app/controllers/ahoy_captain/entry_pages_controller.rb +2 -4
- data/app/controllers/ahoy_captain/exit_pages_controller.rb +3 -4
- data/app/controllers/ahoy_captain/exports_controller.rb +14 -0
- data/app/controllers/ahoy_captain/filters/base_controller.rb +1 -3
- data/app/controllers/ahoy_captain/filters/goals_controller.rb +9 -0
- data/app/controllers/ahoy_captain/filters/pages/actions_controller.rb +1 -1
- data/app/controllers/ahoy_captain/filters/pages/entry_pages_controller.rb +3 -3
- data/app/controllers/ahoy_captain/filters/pages/exit_pages_controller.rb +2 -3
- data/app/controllers/ahoy_captain/filters/properties/names_controller.rb +11 -0
- data/app/controllers/ahoy_captain/filters/properties/values_controller.rb +15 -0
- data/app/controllers/ahoy_captain/filters/sources_controller.rb +1 -1
- data/app/controllers/ahoy_captain/filters/utms_controller.rb +1 -1
- data/app/controllers/ahoy_captain/properties_controller.rb +41 -0
- data/app/controllers/ahoy_captain/regions_controller.rb +3 -7
- data/app/controllers/ahoy_captain/sources_controller.rb +2 -5
- data/app/controllers/ahoy_captain/stats/base_controller.rb +86 -5
- data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +2 -1
- data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +2 -1
- data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +2 -1
- data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +3 -1
- data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +3 -11
- data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +2 -1
- data/app/controllers/ahoy_captain/top_pages_controller.rb +2 -8
- data/app/decorators/ahoy_captain/application_decorator.rb +27 -3
- data/app/decorators/ahoy_captain/campaign_decorator.rb +8 -0
- data/app/decorators/ahoy_captain/city_decorator.rb +12 -0
- data/app/decorators/ahoy_captain/country_decorator.rb +10 -0
- data/app/decorators/ahoy_captain/device_decorator.rb +13 -2
- data/app/decorators/ahoy_captain/page_decorator.rb +11 -0
- data/app/decorators/ahoy_captain/region_decorator.rb +16 -0
- data/app/decorators/ahoy_captain/source_decorator.rb +7 -0
- data/app/helpers/ahoy_captain/application_helper.rb +62 -3
- data/app/models/ahoy_captain/comparison_mode.rb +72 -0
- data/app/models/ahoy_captain/export.rb +48 -0
- data/app/models/ahoy_captain/filter_parser.rb +82 -0
- data/app/models/ahoy_captain/range_from_params.rb +75 -0
- data/app/models/ahoy_captain/rangeable.rb +0 -3
- data/app/models/concerns/ahoy_captain/compare_mode.rb +19 -0
- data/app/models/concerns/ahoy_captain/limitable.rb +17 -0
- data/app/models/concerns/ahoy_captain/range_options.rb +1 -14
- data/app/presenters/ahoy_captain/dashboard_presenter.rb +18 -49
- data/app/presenters/ahoy_captain/goals_presenter.rb +3 -2
- data/app/queries/ahoy_captain/application_query.rb +78 -13
- data/app/queries/ahoy_captain/campaign_query.rb +14 -0
- data/app/queries/ahoy_captain/city_query.rb +11 -0
- data/app/queries/ahoy_captain/country_query.rb +10 -0
- data/app/queries/ahoy_captain/device_query.rb +10 -0
- data/app/queries/ahoy_captain/entry_pages_query.rb +3 -2
- data/app/queries/ahoy_captain/event_query.rb +20 -13
- data/app/queries/ahoy_captain/exit_pages_query.rb +6 -4
- data/app/queries/ahoy_captain/region_query.rb +11 -0
- data/app/queries/ahoy_captain/source_query.rb +10 -0
- data/app/queries/ahoy_captain/stats/average_views_per_visit_query.rb +11 -4
- data/app/queries/ahoy_captain/stats/average_visit_duration_query.rb +14 -2
- data/app/queries/ahoy_captain/stats/base_query.rb +18 -0
- data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +15 -1
- data/app/queries/ahoy_captain/stats/total_pageviews_query.rb +2 -2
- data/app/queries/ahoy_captain/stats/total_visitors_query.rb +1 -1
- data/app/queries/ahoy_captain/stats/unique_visitors_query.rb +1 -1
- data/app/queries/ahoy_captain/stats/views_per_visit_query.rb +1 -1
- data/app/queries/ahoy_captain/stats/visit_duration_query.rb +3 -3
- data/app/queries/ahoy_captain/top_page_query.rb +13 -0
- data/app/queries/ahoy_captain/visit_query.rb +2 -3
- data/app/queries/concerns/ahoy_captain/comparable_queries.rb +25 -0
- data/app/queries/concerns/ahoy_captain/comparable_query.rb +138 -0
- data/app/queries/concerns/ahoy_captain/lazy_comparable_query.rb +42 -0
- data/app/views/ahoy_captain/devices/_table.html.erb +2 -0
- data/app/views/ahoy_captain/devices/index.html+details.erb +1 -1
- data/app/views/ahoy_captain/devices/index.html.erb +2 -2
- data/app/views/ahoy_captain/funnels/show.html.erb +5 -2
- data/app/views/ahoy_captain/goals/index.html.erb +2 -37
- data/app/views/ahoy_captain/layouts/application.html.erb +3 -4
- data/app/views/ahoy_captain/properties/_form.html.erb +6 -0
- data/app/views/ahoy_captain/properties/index.html.erb +3 -0
- data/app/views/ahoy_captain/properties/show.html.erb +6 -0
- data/app/views/ahoy_captain/realtimes/show.html.erb +1 -1
- data/app/views/ahoy_captain/roots/_filters.html.erb +80 -0
- data/app/views/ahoy_captain/roots/show.html.erb +76 -109
- data/app/views/ahoy_captain/stats/base/index.html.erb +34 -9
- data/app/views/ahoy_captain/stats/show.html.erb +8 -55
- data/config/routes.rb +9 -0
- data/lib/ahoy_captain/ahoy/event_methods.rb +35 -74
- data/lib/ahoy_captain/ahoy/visit_methods.rb +1 -1
- data/lib/ahoy_captain/configuration.rb +18 -7
- data/lib/ahoy_captain/engine.rb +22 -0
- data/lib/ahoy_captain/filter_configuration/filter.rb +16 -0
- data/lib/ahoy_captain/filter_configuration/filter_collection.rb +48 -0
- data/lib/ahoy_captain/filters_configuration.rb +77 -0
- data/lib/ahoy_captain/goals.rb +10 -2
- data/lib/ahoy_captain/period_collection.rb +1 -1
- data/lib/ahoy_captain/predicate_label.rb +7 -0
- data/lib/ahoy_captain/version.rb +1 -1
- data/lib/ahoy_captain.rb +7 -1
- data/lib/generators/ahoy_captain/templates/config.rb.tt +32 -0
- metadata +80 -21
- data/app/assets/javascript/ahoy_captain/controllers/filter_controller.js +0 -145
- data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +0 -17
- data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +0 -43
- data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +0 -25
- data/app/models/ahoy_captain/current.rb +0 -9
- data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -1,21 +1,8 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
module RangeOptions
|
3
3
|
|
4
|
-
def period
|
5
|
-
raise NotImplementedError
|
6
|
-
end
|
7
|
-
|
8
4
|
private def range
|
9
|
-
|
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)
|
5
|
+
RangeFromParams.from_params(params)
|
19
6
|
end
|
20
7
|
end
|
21
8
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
class DashboardPresenter
|
3
|
+
include Rangeable
|
3
4
|
include RangeOptions
|
5
|
+
include CompareMode
|
4
6
|
|
5
7
|
attr_reader :params
|
6
8
|
|
@@ -9,76 +11,43 @@ module AhoyCaptain
|
|
9
11
|
end
|
10
12
|
|
11
13
|
def unique_visitors
|
12
|
-
|
13
|
-
Stats::UniqueVisitorsQuery.call(params).count(:visitor_token)
|
14
|
-
end
|
14
|
+
Stats::UniqueVisitorsQuery.call(params).with_comparison(compare_mode?).count
|
15
15
|
end
|
16
16
|
|
17
17
|
def total_visits
|
18
|
-
|
19
|
-
Stats::TotalVisitorsQuery.call(params).count(:id)
|
20
|
-
end
|
18
|
+
Stats::TotalVisitorsQuery.call(params).with_comparison(compare_mode?).count
|
21
19
|
end
|
22
20
|
|
23
21
|
def total_pageviews
|
24
|
-
|
25
|
-
Stats::TotalPageviewsQuery.call(params).count(:id)
|
26
|
-
end
|
22
|
+
Stats::TotalPageviewsQuery.call(params).with_comparison(compare_mode?).count
|
27
23
|
end
|
28
24
|
|
29
25
|
def views_per_visit
|
30
|
-
|
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
|
26
|
+
Stats::AverageViewsPerVisitQuery.call(params).with_comparison(compare_mode?).average("count")
|
42
27
|
end
|
43
28
|
|
44
29
|
def bounce_rate
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
30
|
+
query = Stats::BounceRatesQuery.call(params)
|
31
|
+
if compare_mode?
|
32
|
+
query.with_comparison(true)
|
33
|
+
else
|
34
|
+
query.average("bounce_rate").try(:round, 2) || 0
|
61
35
|
end
|
62
36
|
end
|
63
37
|
|
64
38
|
def visit_duration
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
else
|
71
|
-
"0M 0S"
|
72
|
-
end
|
39
|
+
query = Stats::AverageVisitDurationQuery.call(params)
|
40
|
+
if compare_mode?
|
41
|
+
query.with_comparison(true)
|
42
|
+
else
|
43
|
+
query[0].average_visit_duration
|
73
44
|
end
|
74
45
|
end
|
75
46
|
|
76
47
|
private
|
77
48
|
|
78
|
-
def
|
79
|
-
|
80
|
-
yield
|
81
|
-
end
|
49
|
+
def compare_mode?
|
50
|
+
params[:comparison] != 'false'
|
82
51
|
end
|
83
52
|
end
|
84
53
|
end
|
@@ -16,7 +16,7 @@ module AhoyCaptain
|
|
16
16
|
queries = {
|
17
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
18
|
}
|
19
|
-
selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr from totals"]
|
19
|
+
selects = ["SELECT unique_visits, name, total_events, sort_order, 0 as cr, '' as goal_id from totals"]
|
20
20
|
last_goal = nil
|
21
21
|
map = {}.with_indifferent_access
|
22
22
|
|
@@ -27,9 +27,10 @@ module AhoyCaptain
|
|
27
27
|
"'#{goal.id}' as name",
|
28
28
|
"count(distinct #{AhoyCaptain.event.table_name}.id) as total_events",
|
29
29
|
"#{index + 1} as sort_order",
|
30
|
+
"'#{goal.id}' as goal_id"
|
30
31
|
]
|
31
32
|
).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
|
+
selects << ["SELECT unique_visits, name, total_events, sort_order, 0::decimal as cr, '#{goal.id}' as goal_id from #{goal.id}"]
|
33
34
|
map[goal.id] = goal
|
34
35
|
last_goal = goal
|
35
36
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
+
require_relative '../concerns/ahoy_captain/comparable_queries'
|
2
|
+
|
1
3
|
module AhoyCaptain
|
2
4
|
class ApplicationQuery
|
3
5
|
# if you want to enforce returning a relation
|
4
6
|
class_attribute :strict, default: true
|
5
|
-
include Rangeable
|
6
7
|
|
7
8
|
delegate_missing_to :@query
|
8
9
|
|
@@ -53,6 +54,7 @@ module AhoyCaptain
|
|
53
54
|
EventQuery.call(params)
|
54
55
|
end
|
55
56
|
|
57
|
+
# this could be better
|
56
58
|
def ransack_params_for(type)
|
57
59
|
ransackable_params = {}
|
58
60
|
|
@@ -61,12 +63,13 @@ module AhoyCaptain
|
|
61
63
|
visit: (AhoyCaptain.visit.ransackable_attributes + AhoyCaptain.visit.ransackable_scopes).map(&:to_s),
|
62
64
|
event: (AhoyCaptain.event.ransackable_attributes + AhoyCaptain.event.ransackable_scopes).map(&:to_s),
|
63
65
|
}
|
64
|
-
|
66
|
+
|
67
|
+
pattern = /(?:#{Ransack.predicates.sorted_names_with_underscores.to_h.values.join("|")})$/
|
65
68
|
params[:q].each do |key, value|
|
66
69
|
attribute_name = key.gsub(pattern, '')
|
67
|
-
if type == :event && ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key)
|
70
|
+
if type == :event && (ransackable_attributes[:visit].include?(attribute_name) || ransackable_attributes[:visit].include?(key))
|
68
71
|
ransackable_params["visit_#{key}"] = value
|
69
|
-
elsif type == :visit && ransackable_attributes[:event].include?(attribute_name) || ransackable_attributes[:event].include?(key)
|
72
|
+
elsif type == :visit && (ransackable_attributes[:event].include?(attribute_name) || ransackable_attributes[:event].include?(key))
|
70
73
|
ransackable_params["events_#{key}"] = value
|
71
74
|
else
|
72
75
|
ransackable_params[key] = value
|
@@ -74,36 +77,57 @@ module AhoyCaptain
|
|
74
77
|
end
|
75
78
|
end
|
76
79
|
|
80
|
+
merge_params = {}
|
81
|
+
ransackable_params.each do |k,v|
|
82
|
+
transform = false
|
83
|
+
if v == AhoyCaptain.none.value
|
84
|
+
transform = true
|
85
|
+
elsif v.is_a?(Array) && v[0] == AhoyCaptain.none.value
|
86
|
+
transform = true
|
87
|
+
end
|
88
|
+
|
89
|
+
if transform
|
90
|
+
key = k.dup
|
91
|
+
ransackable_params.delete(key)
|
92
|
+
predicate = Ransack::Predicate.detect_and_strip_from_string!(key)
|
93
|
+
if predicate.include?("not")
|
94
|
+
merge_params["#{key}_not_null"] = '1'
|
95
|
+
else
|
96
|
+
merge_params["#{key}_null"] = '1'
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
ransackable_params.merge!(merge_params)
|
77
102
|
# send the right format
|
78
103
|
# ::Ahoy::Visit.ransack(events_time_lt: Time.now).result.to_sql
|
79
104
|
# is not
|
80
105
|
# ::Ahoy::Visit.ransack(events_time_lt: Time.now.to_i).result.to_sql
|
81
106
|
if range
|
82
107
|
if type == :event
|
83
|
-
if range.
|
108
|
+
if range.realtime?
|
84
109
|
ransackable_params['time_gt'] = range[0]
|
85
|
-
ransackable_params['time_lt'] = range[1]
|
86
110
|
ransackable_params["visit_started_at_gt"] = range[0]
|
87
|
-
ransackable_params["visit_started_at_lt"] = range[1]
|
88
111
|
else
|
89
112
|
ransackable_params['time_gt'] = range[0]
|
113
|
+
ransackable_params['time_lt'] = range[1]
|
90
114
|
ransackable_params["visit_started_at_gt"] = range[0]
|
115
|
+
ransackable_params["visit_started_at_lt"] = range[1]
|
91
116
|
end
|
92
117
|
elsif type == :visit
|
93
|
-
if range.
|
118
|
+
if range.realtime?
|
119
|
+
ransackable_params["started_at_gt"] = range[0]
|
120
|
+
ransackable_params["events_time_gt"] = range[0]
|
121
|
+
else
|
94
122
|
ransackable_params["started_at_gt"] = range[0]
|
95
123
|
ransackable_params["started_at_lt"] = range[1]
|
96
124
|
ransackable_params["events_time_gteq"] = range[0]
|
97
125
|
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
126
|
end
|
102
127
|
end
|
103
128
|
end
|
104
129
|
|
105
|
-
|
106
|
-
ransackable_params
|
130
|
+
ransackify(ransackable_params, type)
|
107
131
|
end
|
108
132
|
|
109
133
|
# merge both sets of ransackable params and ensure that they're being set on the correct association
|
@@ -117,5 +141,46 @@ module AhoyCaptain
|
|
117
141
|
raise ArgumentError, "use ransack_params_for(type)"
|
118
142
|
end
|
119
143
|
end
|
144
|
+
|
145
|
+
def ransackify(query, type)
|
146
|
+
return unless query
|
147
|
+
|
148
|
+
query = query.try(:permit!).try(:to_h) unless query.is_a?(Hash)
|
149
|
+
obj = query.each_with_object({}) do |(k, v), obj|
|
150
|
+
if k.starts_with?('properties.')
|
151
|
+
field = k.split('properties.').last
|
152
|
+
operation = Ransack::Predicate.detect_and_strip_from_string!(field)
|
153
|
+
|
154
|
+
raise ArgumentError, "No valid predicate for #{field}" unless operation
|
155
|
+
|
156
|
+
prefix = type == :event ? "" : "events_"
|
157
|
+
obj[:c] ||= []
|
158
|
+
|
159
|
+
obj[:c] << {
|
160
|
+
a: {
|
161
|
+
'0' => {
|
162
|
+
name: "#{prefix}properties",
|
163
|
+
ransacker_args: field
|
164
|
+
}
|
165
|
+
},
|
166
|
+
p: operation,
|
167
|
+
v: [v]
|
168
|
+
}
|
169
|
+
|
170
|
+
else
|
171
|
+
obj[k] = v
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
if type == :event
|
176
|
+
return obj
|
177
|
+
else
|
178
|
+
return obj
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
def range
|
183
|
+
RangeFromParams.from_params(params)
|
184
|
+
end
|
120
185
|
end
|
121
186
|
end
|
@@ -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 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
|
@@ -2,14 +2,15 @@ module AhoyCaptain
|
|
2
2
|
class EntryPagesQuery < ApplicationQuery
|
3
3
|
|
4
4
|
def build
|
5
|
-
max_id_query =
|
6
|
-
|
5
|
+
max_id_query = event_query.with_routes.select("min(#{AhoyCaptain.event.table_name}.id) as id").group("visit_id")
|
6
|
+
event_query.with_routes.select(
|
7
7
|
"#{AhoyCaptain.config.event[:url_column]} as url",
|
8
8
|
"count(#{AhoyCaptain.config.event[:url_column]}) as count",
|
9
9
|
"sum(count(#{AhoyCaptain.config.event[:url_column]})) over() as total_count"
|
10
10
|
)
|
11
11
|
.where(id: max_id_query)
|
12
12
|
.group(AhoyCaptain.config.event[:url_column])
|
13
|
+
.order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
|
13
14
|
end
|
14
15
|
|
15
16
|
|
@@ -1,35 +1,42 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
class EventQuery < ApplicationQuery
|
3
|
-
include Rangeable
|
4
3
|
|
5
4
|
def build
|
6
|
-
|
5
|
+
entry_pages = ransack_params_for(:event).select { |k,v| k.start_with?("entry_page") }
|
6
|
+
exit_pages = ransack_params_for(:event).select { |k,v| k.start_with?("exit_page") }
|
7
7
|
|
8
|
-
|
8
|
+
event = AhoyCaptain.event
|
9
|
+
shared_context = Ransack::Context.for(event)
|
10
|
+
|
11
|
+
search_parents = event.ransack(
|
9
12
|
ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }, context: shared_context
|
10
13
|
)
|
14
|
+
|
15
|
+
visit_params = ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") || k.start_with?("events_") || k == :c }.transform_keys { |key| "visit_#{key}" }
|
11
16
|
search_children = AhoyCaptain.visit.ransack(
|
12
|
-
|
17
|
+
visit_params, context: shared_context
|
13
18
|
)
|
14
|
-
|
15
19
|
shared_conditions = [search_parents, search_children].map { |search|
|
16
20
|
Ransack::Visitor.new.accept(search.base)
|
17
21
|
}
|
18
22
|
|
19
|
-
AhoyCaptain.event.joins(shared_context.join_sources)
|
20
|
-
.where(shared_conditions.reduce(&:or))
|
23
|
+
joined = AhoyCaptain.event.joins(shared_context.join_sources)
|
21
24
|
|
22
|
-
|
25
|
+
if entry_pages.values.any?(&:present?) || params[:controller].include?("entry_pages")
|
26
|
+
joined = joined.with_entry_pages
|
27
|
+
end
|
23
28
|
|
24
|
-
|
25
|
-
|
29
|
+
if exit_pages.values.any?(&:present?) || params[:controller].include?("exit_pages")
|
30
|
+
joined = joined.with_exit_pages
|
31
|
+
end
|
32
|
+
|
33
|
+
joined.where(shared_conditions.reduce(&:and))
|
26
34
|
end
|
27
35
|
|
28
|
-
def
|
29
|
-
@query = @query.
|
36
|
+
def page_view
|
37
|
+
@query = @query.page_view
|
30
38
|
|
31
39
|
self
|
32
40
|
end
|
33
|
-
|
34
41
|
end
|
35
42
|
end
|
@@ -2,14 +2,16 @@ module AhoyCaptain
|
|
2
2
|
class ExitPagesQuery < ApplicationQuery
|
3
3
|
|
4
4
|
def build
|
5
|
-
max_id_query =
|
6
|
-
|
5
|
+
max_id_query = event_query.with_routes.select("max(#{AhoyCaptain.event.table_name}.id) as id").group("visit_id")
|
6
|
+
event_query.with_routes.select(
|
7
7
|
"#{AhoyCaptain.config.event[:url_column]} as url",
|
8
8
|
"count(#{AhoyCaptain.config.event[:url_column]}) as count",
|
9
9
|
"sum(count(#{AhoyCaptain.config.event[:url_column]})) over() as total_count"
|
10
10
|
)
|
11
|
-
|
12
|
-
|
11
|
+
.where(id: max_id_query)
|
12
|
+
.group(AhoyCaptain.config.event[:url_column])
|
13
|
+
.order(Arel.sql "count(#{AhoyCaptain.config.event[:url_column]}) desc")
|
14
|
+
|
13
15
|
end
|
14
16
|
|
15
17
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module AhoyCaptain
|
2
|
+
class RegionQuery < ApplicationQuery
|
3
|
+
def build
|
4
|
+
visit_query
|
5
|
+
.reselect("region, country, count(concat(region, country)) as count, sum(count(region)) over() as total_count")
|
6
|
+
.where.not(region: nil)
|
7
|
+
.group("region, country")
|
8
|
+
.order(Arel.sql "count(concat(region, country)) desc")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module AhoyCaptain
|
2
|
+
class SourceQuery < ApplicationQuery
|
3
|
+
def build
|
4
|
+
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")
|
5
|
+
.where.not(referring_domain: nil)
|
6
|
+
.group("substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')")
|
7
|
+
.order(Arel.sql "count(substring(referring_domain from '(?:.*://)?(?:www\.)?([^/?]*)')) desc")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,12 +1,19 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
module Stats
|
3
3
|
# pls fix
|
4
|
-
class AverageViewsPerVisitQuery <
|
4
|
+
class AverageViewsPerVisitQuery < BaseQuery
|
5
5
|
def build
|
6
|
-
event_query.
|
7
|
-
.where(name: "$view")
|
8
|
-
.group("ahoy_visits.id")
|
6
|
+
subquery = event_query.select("count(ahoy_events.visit_id) as count").where(name: "$view").group(:visit_id)
|
9
7
|
|
8
|
+
AhoyCaptain.event.select("count").from("(#{subquery.to_sql}) as events")
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.cast_type(_column)
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.cast_value(_, value)
|
16
|
+
value.to_i
|
10
17
|
end
|
11
18
|
end
|
12
19
|
end
|
@@ -1,11 +1,23 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
module Stats
|
3
|
-
class AverageVisitDurationQuery <
|
3
|
+
class AverageVisitDurationQuery < BaseQuery
|
4
4
|
def build
|
5
5
|
max_events = event_query.select("#{AhoyCaptain.event.table_name}.visit_id, max(#{AhoyCaptain.event.table_name}.time) as created_at").group("visit_id")
|
6
|
-
visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at)) as average_visit_duration")
|
6
|
+
visit_query.select("avg((max_events.created_at - #{AhoyCaptain.visit.table_name}.started_at))::interval as average_visit_duration")
|
7
7
|
.joins("LEFT JOIN (#{max_events.to_sql}) as max_events ON #{AhoyCaptain.visit.table_name}.id = max_events.visit_id")
|
8
8
|
end
|
9
|
+
|
10
|
+
def self.cast_type(value)
|
11
|
+
ActiveRecord::Type.lookup(:string)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.cast_value(_type, value)
|
15
|
+
if value.present?
|
16
|
+
ActiveSupport::Duration.parse(value)
|
17
|
+
else
|
18
|
+
ActiveSupport::Duration.parse("P0MT0S")
|
19
|
+
end
|
20
|
+
end
|
9
21
|
end
|
10
22
|
end
|
11
23
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module AhoyCaptain
|
2
|
+
module Stats
|
3
|
+
class BaseQuery < ApplicationQuery
|
4
|
+
include ComparableQuery
|
5
|
+
include LazyComparableQuery
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def self.cast_type(column)
|
10
|
+
ActiveRecord::Type.lookup(::Ahoy::Visit.columns_hash[column.to_s].type)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.cast_value(type, value)
|
14
|
+
type.cast(value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
module Stats
|
3
|
-
class BounceRatesQuery <
|
3
|
+
class BounceRatesQuery < BaseQuery
|
4
4
|
def build
|
5
5
|
total_visits = visit_query.select("date(#{AhoyCaptain.visit.table_name}.started_at) as date, count(*) as count").group("date(#{AhoyCaptain.visit.table_name}.started_at)")
|
6
6
|
subquery = visit_query.select(:id, :started_at).joins(:events).group("#{AhoyCaptain.visit.table_name}.id, #{AhoyCaptain.visit.table_name}.started_at").having("count(#{AhoyCaptain.event.table_name}.id) = 1")
|
@@ -12,6 +12,20 @@ module AhoyCaptain
|
|
12
12
|
::Ahoy::Visit.with(total_visits: total_visits, single_page_visits: single_page_visits, daily_bounce_rate: daily_bounce_rate).select("bounce_rate, date").from("daily_bounce_rate")
|
13
13
|
end
|
14
14
|
|
15
|
+
protected
|
16
|
+
|
17
|
+
def self.cast_type(column)
|
18
|
+
ActiveRecord::Type.lookup(:decimal)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.cast_value(type, value)
|
22
|
+
if value.blank?
|
23
|
+
return 0.to_d
|
24
|
+
end
|
25
|
+
|
26
|
+
super.round(2)
|
27
|
+
end
|
28
|
+
|
15
29
|
end
|
16
30
|
end
|
17
31
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module AhoyCaptain
|
2
2
|
module Stats
|
3
|
-
class VisitDurationQuery <
|
3
|
+
class VisitDurationQuery < BaseQuery
|
4
4
|
def build
|
5
5
|
events = event_query
|
6
6
|
.reselect("max(#{AhoyCaptain.event.table_name}.time) - min(#{AhoyCaptain.event.table_name}.time) as duration, #{AhoyCaptain.event.table_name}.visit_id")
|
7
7
|
.group("#{AhoyCaptain.event.table_name}.visit_id")
|
8
8
|
|
9
9
|
::Ahoy::Visit
|
10
|
-
.select("duration as duration, started_at")
|
10
|
+
.select("duration::duration as duration, started_at")
|
11
11
|
.from(events, :views_per_visit_table)
|
12
|
-
.joins("inner join #{AhoyCaptain.visit.table_name} on
|
12
|
+
.joins("inner join #{AhoyCaptain.visit.table_name} on #{AhoyCaptain.visit.table_name}.id = views_per_visit_table.visit_id")
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module AhoyCaptain
|
2
|
+
class TopPageQuery < ApplicationQuery
|
3
|
+
def build
|
4
|
+
event_query.with_routes
|
5
|
+
.select(
|
6
|
+
"#{AhoyCaptain.config.event[:url_column]} as url",
|
7
|
+
"count(*) as count",
|
8
|
+
"sum(count(*)) over() as total_count"
|
9
|
+
).group(Arel.sql ("(#{AhoyCaptain.config.event[:url_column]})"))
|
10
|
+
.order(Arel.sql("count(#{AhoyCaptain.config.event[:url_column]}) desc"))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|