ahoy_captain 0.77 → 0.81
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascript/ahoy_captain/application.js +4 -4
- data/app/assets/javascript/ahoy_captain/controllers/application.js +5 -5
- data/app/assets/javascript/ahoy_captain/controllers/application_controller.js +9 -10
- 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_controller.js +64 -58
- data/app/assets/javascript/ahoy_captain/controllers/filter_tag_controller.js +11 -8
- data/app/assets/javascript/ahoy_captain/controllers/funnel_chart_controller.js +77 -133
- data/app/assets/javascript/ahoy_captain/controllers/index.js +4 -3
- data/app/assets/javascript/ahoy_captain/controllers/interval_controller.js +10 -0
- data/app/assets/javascript/ahoy_captain/controllers/link_controller.js +22 -22
- data/app/assets/javascript/ahoy_captain/controllers/navigation_controller.js +10 -10
- data/app/assets/javascript/ahoy_captain/controllers/realtime_controller.js +7 -8
- data/app/controllers/ahoy_captain/application_controller.rb +17 -15
- 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 +15 -0
- data/app/controllers/ahoy_captain/realtimes_controller.rb +1 -1
- 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 +61 -0
- data/app/controllers/ahoy_captain/stats/bounce_rates_controller.rb +3 -2
- data/app/controllers/ahoy_captain/stats/total_pageviews_controller.rb +1 -1
- data/app/controllers/ahoy_captain/stats/total_visits_controller.rb +1 -1
- data/app/controllers/ahoy_captain/stats/unique_visitors_controller.rb +1 -1
- data/app/controllers/ahoy_captain/stats/views_per_visits_controller.rb +7 -6
- data/app/controllers/ahoy_captain/stats/visit_durations_controller.rb +1 -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/models/ahoy_captain/export.rb +48 -0
- data/app/presenters/ahoy_captain/dashboard_presenter.rb +24 -16
- data/app/presenters/ahoy_captain/funnel_presenter.rb +32 -29
- data/app/presenters/ahoy_captain/goals_presenter.rb +32 -23
- data/app/queries/ahoy_captain/application_query.rb +5 -2
- 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 +16 -1
- 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_visit_duration_query.rb +3 -7
- data/app/queries/ahoy_captain/stats/bounce_rates_query.rb +9 -6
- 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 +2 -2
- data/app/queries/ahoy_captain/stats/visit_duration_query.rb +4 -4
- data/app/queries/ahoy_captain/top_page_query.rb +13 -0
- data/app/queries/ahoy_captain/visit_query.rb +12 -12
- data/app/views/ahoy_captain/goals/index.html.erb +5 -5
- data/app/views/ahoy_captain/roots/show.html.erb +1 -1
- data/app/views/ahoy_captain/stats/base/index.html.erb +9 -1
- data/app/views/ahoy_captain/stats/show.html.erb +1 -1
- data/config/routes.rb +1 -0
- data/lib/ahoy_captain/ahoy/event_methods.rb +1 -1
- data/lib/ahoy_captain/configuration.rb +2 -2
- data/lib/ahoy_captain/engine.rb +1 -0
- data/lib/ahoy_captain/goals.rb +8 -4
- data/lib/ahoy_captain/period_collection.rb +1 -1
- data/lib/ahoy_captain/version.rb +1 -1
- data/lib/generators/ahoy_captain/migration_generator.rb +21 -0
- data/lib/generators/ahoy_captain/templates/config.rb.tt +18 -3
- data/lib/generators/ahoy_captain/templates/migration.rb.tt +7 -0
- metadata +42 -4
- data/app/models/ahoy_captain/current.rb +0 -9
- data/app/models/ahoy_captain/url_helpers.rb +0 -6
@@ -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(:
|
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
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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.
|
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
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
15
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
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
|
46
|
-
@
|
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
|
@@ -90,8 +93,8 @@ module AhoyCaptain
|
|
90
93
|
if range.size == 2
|
91
94
|
ransackable_params["started_at_gt"] = range[0]
|
92
95
|
ransackable_params["started_at_lt"] = range[1]
|
93
|
-
ransackable_params["
|
94
|
-
ransackable_params["
|
96
|
+
ransackable_params["events_time_gteq"] = range[0]
|
97
|
+
ransackable_params["events_time_lteq"] = range[1]
|
95
98
|
else
|
96
99
|
ransackable_params["started_at_gt"] = range[0]
|
97
100
|
ransackable_params["events_time_gt"] = range[0]
|
@@ -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
|
|
@@ -3,7 +3,22 @@ module AhoyCaptain
|
|
3
3
|
include Rangeable
|
4
4
|
|
5
5
|
def build
|
6
|
-
::
|
6
|
+
shared_context = Ransack::Context.for(AhoyCaptain.event)
|
7
|
+
|
8
|
+
search_parents = AhoyCaptain.event.ransack(
|
9
|
+
ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }, context: shared_context
|
10
|
+
)
|
11
|
+
search_children = AhoyCaptain.visit.ransack(
|
12
|
+
ransack_params_for(:visit).reject { |k,v| k.start_with?("event_") }.transform_keys { |key| "visit_#{key}" }, context: shared_context
|
13
|
+
)
|
14
|
+
|
15
|
+
shared_conditions = [search_parents, search_children].map { |search|
|
16
|
+
Ransack::Visitor.new.accept(search.base)
|
17
|
+
}
|
18
|
+
|
19
|
+
AhoyCaptain.event.joins(shared_context.join_sources)
|
20
|
+
.where(shared_conditions.reduce(&:or))
|
21
|
+
|
7
22
|
end
|
8
23
|
|
9
24
|
def within_range
|
@@ -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
|
@@ -2,13 +2,9 @@ module AhoyCaptain
|
|
2
2
|
module Stats
|
3
3
|
class AverageVisitDurationQuery < ApplicationQuery
|
4
4
|
def build
|
5
|
-
|
6
|
-
|
7
|
-
.
|
8
|
-
|
9
|
-
::Ahoy::Visit.joins("INNER JOIN #{::AhoyCaptain.event.table_name} ON #{::AhoyCaptain.event.table_name}.visit_id = visit_durations.visit_id")
|
10
|
-
.reselect("avg(visit_duration) as average_visit_duration")
|
11
|
-
.from(events, :visit_durations)
|
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")
|
7
|
+
.joins("LEFT JOIN (#{max_events.to_sql}) as max_events ON #{AhoyCaptain.visit.table_name}.id = max_events.visit_id")
|
12
8
|
end
|
13
9
|
end
|
14
10
|
end
|
@@ -2,13 +2,16 @@ module AhoyCaptain
|
|
2
2
|
module Stats
|
3
3
|
class BounceRatesQuery < ApplicationQuery
|
4
4
|
def build
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
+
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")
|
7
|
+
single_page_visits = ::Ahoy::Visit.select("date(subquery.started_at) as date, count(*) as count").from("(#{subquery.to_sql}) as subquery").group("date(started_at)")
|
8
|
+
daily_bounce_rate = ::Ahoy::Visit.select("total_visits.date, (single_page_visits.count::FLOAT / total_visits.count) * 100 as bounce_rate")
|
9
|
+
.from("total_visits")
|
10
|
+
.joins("join single_page_visits ON total_visits.date = single_page_visits.date")
|
11
|
+
|
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")
|
11
13
|
end
|
14
|
+
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
@@ -4,9 +4,9 @@ module AhoyCaptain
|
|
4
4
|
def build
|
5
5
|
events = event_query
|
6
6
|
.joins(:visit)
|
7
|
-
.select("#{::AhoyCaptain.visit.table_name}.started_at as started_at, count(name) / count(distinct visit_id) as views_per_visit")
|
7
|
+
.select("#{::AhoyCaptain.visit.table_name}.started_at as started_at, count(#{AhoyCaptain.event.table_name}.name) / count(distinct #{AhoyCaptain.event.table_name}.visit_id) as views_per_visit")
|
8
8
|
.where(name: AhoyCaptain.config.event[:view_name])
|
9
|
-
.group("started_at, visit_id")
|
9
|
+
.group("#{AhoyCaptain.visit.table_name}.started_at, #{AhoyCaptain.event.table_name}.visit_id")
|
10
10
|
|
11
11
|
::Ahoy::Visit
|
12
12
|
.select("views_per_visit as views_per_visit")
|
@@ -3,13 +3,13 @@ module AhoyCaptain
|
|
3
3
|
class VisitDurationQuery < ApplicationQuery
|
4
4
|
def build
|
5
5
|
events = event_query
|
6
|
-
.
|
7
|
-
.group("visit_id")
|
6
|
+
.reselect("max(#{AhoyCaptain.event.table_name}.time) - min(#{AhoyCaptain.event.table_name}.time) as duration, #{AhoyCaptain.event.table_name}.visit_id")
|
7
|
+
.group("#{AhoyCaptain.event.table_name}.visit_id")
|
8
8
|
|
9
9
|
::Ahoy::Visit
|
10
|
-
.
|
11
|
-
.select("duration as duration, ahoy_visits.started_at")
|
10
|
+
.select("duration as duration, started_at")
|
12
11
|
.from(events, :views_per_visit_table)
|
12
|
+
.joins("inner join #{AhoyCaptain.visit.table_name} on ahoy_visits.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
|
@@ -3,22 +3,22 @@ module AhoyCaptain
|
|
3
3
|
include Rangeable
|
4
4
|
|
5
5
|
def build
|
6
|
-
::
|
7
|
-
end
|
6
|
+
shared_context = Ransack::Context.for(AhoyCaptain.visit)
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
search_parents = AhoyCaptain.visit.ransack(
|
9
|
+
ransack_params_for(:visit).reject { |k,v| k.start_with?("events_") }, context: shared_context
|
10
|
+
)
|
11
|
+
search_children = AhoyCaptain.event.ransack(
|
12
|
+
ransack_params_for(:event).reject { |k,v| k.start_with?("visit_") }.transform_keys { |key| "events_#{key}" }, context: shared_context
|
13
|
+
)
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
shared_conditions = [search_parents, search_children].map { |search|
|
16
|
+
Ransack::Visitor.new.accept(search.base)
|
17
|
+
}
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
AhoyCaptain.visit.joins(shared_context.join_sources)
|
20
|
+
.where(shared_conditions.reduce(&:or))
|
20
21
|
|
21
|
-
self
|
22
22
|
end
|
23
23
|
|
24
24
|
def is_a?(other)
|