Class: Lookout::FunnelPresenter

Inherits:
Object
  • Object
show all
Defined in:
app/presenters/lookout/funnel_presenter.rb

Overview

this is incredibly naive and needs some tlc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(funnel, event_query) ⇒ FunnelPresenter

Returns a new instance of FunnelPresenter.



6
7
8
9
# File 'app/presenters/lookout/funnel_presenter.rb', line 6

def initialize(funnel, event_query)
  @funnel = funnel
  @event_query = event_query.joins(:visit)
end

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



5
6
7
# File 'app/presenters/lookout/funnel_presenter.rb', line 5

def steps
  @steps
end

Instance Method Details

#as_jsonObject



64
65
66
67
68
69
# File 'app/presenters/lookout/funnel_presenter.rb', line 64

def as_json
  {
    steps: @steps.as_json,
    total: total
  }
end

#buildObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/presenters/lookout/funnel_presenter.rb', line 11

def build
  if Lookout.config.goals.none? || @funnel.goals.none?
    @steps = []
    return self
  end

  queries = {
    totals: @event_query.select("count(distinct(#{Lookout.event.table_name}.visit_id)) as unique_visits, '_internal_total_visits_' as name, count(distinct #{Lookout.event.table_name}.id) as total_events, 0 as sort_order")
  }
  selects = ["SELECT unique_visits, name, total_events, sort_order from totals"]
  last_goal = nil
  map = {}.with_indifferent_access

  # Use funnel's goals in order, not all configured goals
  @funnel.goals.each_with_index do |goal, index|
    queries[goal.id] = @event_query.select("count(distinct(#{Lookout.event.table_name}.visit_id)) as unique_visits, '#{goal.id}' as name, count(distinct #{Lookout.event.table_name}.id) as total_events, #{index + 1} as sort_order").merge(goal.event_query.call).group("#{Lookout.event.table_name}.name")
    selects << ["SELECT unique_visits, name, total_events, sort_order from #{goal.id}"]
    map[goal.id] = goal
    last_goal = goal
  end

  # activerecord quirk / with bug
  select = selects.join(" UNION ").delete_suffix(" from #{last_goal.id}")
  select = select.delete_prefix("SELECT ")
  steps = ::Ahoy::Event.with(
    queries,
    ).select(select).from("#{last_goal.id}").order("sort_order asc")

  # Cast to numeric/real for division depending on database
  cast_type = Lookout::DatabaseAdapter.postgresql? ? "::numeric" : ""
  cast_division = Lookout::DatabaseAdapter.postgresql? ? 
    "total_events::numeric/lag(total_events, 1) over ()" :
    "CAST(total_events AS REAL)/lag(total_events, 1) over ()"
  
  items = ::Ahoy::Event.with(steps: steps).select("total_events, unique_visits, name, round((#{cast_division}),2) as drop_off").from("steps").order("sort_order asc").index_by(&:name)
  items.delete("_internal_total_visits_")
  @steps = []

  items.values.each do |item|
    if map[item.name]
      item.name = map[item.name].title
    end
  end

  @steps = items.values
  self
end

#to_jsonObject



71
72
73
# File 'app/presenters/lookout/funnel_presenter.rb', line 71

def to_json
  as_json.to_json
end

#totalObject



60
61
62
# File 'app/presenters/lookout/funnel_presenter.rb', line 60

def total
  @event_query.distinct(:visitor_token).count
end