Class: AhoyCaptain::Stats::BounceRatesQuery

Inherits:
BaseQuery show all
Defined in:
app/queries/ahoy_captain/stats/bounce_rates_query.rb

Instance Attribute Summary

Attributes inherited from ApplicationQuery

#params

Instance Method Summary collapse

Methods included from LazyComparableQuery

#with_lazy_comparison

Methods included from ComparableQuery

#with_comparison

Methods inherited from ApplicationQuery

call, inherited, #initialize, #inspect

Constructor Details

This class inherits a constructor from AhoyCaptain::ApplicationQuery

Instance Method Details

#buildObject



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