Class: Matey::BounceRateComponent

Inherits:
ApplicationComponent show all
Defined in:
app/components/matey/bounce_rate_component.rb

Instance Method Summary collapse

Methods inherited from ApplicationComponent

#before_render

Methods included from ColorSchemeHelper

#color_scheme

Constructor Details

#initialize(events:, visits:, limit: 5, color_scheme: "neutral") ⇒ BounceRateComponent

Returns a new instance of BounceRateComponent.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/components/matey/bounce_rate_component.rb', line 4

def initialize(events:, visits:, limit: 5, color_scheme: "neutral")
  # Determine the total number of user sessions to the website
  @total_number_of_user_visits = events.pluck(:visit_id).uniq.count

  # First we group by visit_id and scope to visits containing ONLY 1 event
  @visits_having_only_one_event = events.group(:visit_id).having("count(ahoy_events.id) == 1")
  # We then count each unique occurence of a visit with 1 event
  @total_number_of_single_event_visits = @visits_having_only_one_event.uniq.count

  # Determine pages in which the most bounces occur
  @single_event_visits_landing_page_count = visits.where(id: @visits_having_only_one_event.pluck(:visit_id)).pluck(:landing_page).tally

  # Scope the results to the given limit and sort them in descending order
  @most_bounced_pages = @single_event_visits_landing_page_count.sort_by { |controller_name_and_action, count| count }.last(limit).reverse

  # Get the percentage as #-One-Page-Visits / Total-#-Of-Visits
  @percentage_of_visits_that_were_bounced = ((@total_number_of_single_event_visits.to_f / ((@total_number_of_user_visits == 0) ? 1 : @total_number_of_user_visits)) * 100).round(1)

  @color_scheme = color_scheme(scheme: color_scheme)
end