Class: AhoyCaptain::Stats::BounceRatesQuery
- Inherits:
-
BaseQuery
- Object
- ApplicationQuery
- BaseQuery
- AhoyCaptain::Stats::BounceRatesQuery
- Defined in:
- app/queries/ahoy_captain/stats/bounce_rates_query.rb
Instance Attribute Summary
Attributes inherited from ApplicationQuery
Instance Method Summary collapse
Methods included from LazyComparableQuery
Methods included from ComparableQuery
Methods inherited from ApplicationQuery
call, inherited, #initialize, #inspect
Constructor Details
This class inherits a constructor from AhoyCaptain::ApplicationQuery
Instance Method Details
#build ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'app/queries/ahoy_captain/stats/bounce_rates_query.rb', line 4 def build 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)") 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") 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)") daily_bounce_rate = ::Ahoy::Visit.select("total_visits.date, (single_page_visits.count::FLOAT / total_visits.count) * 100 as bounce_rate") .from("total_visits") .joins("join single_page_visits ON total_visits.date = single_page_visits.date") ::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") end |