Class: Sentiero::Analytics::Segmenter

Inherits:
Analyzer
  • Object
show all
Defined in:
lib/sentiero/analytics/segmenter.rb

Overview

Filters sessions on read by browser/device/country/URL/metadata/has-errors/duration (AND-combined), scanning up to the store's limits.analytics_max_scan_sessions.

Constant Summary

Constants included from Events

Events::CUSTOM, Events::INCREMENTAL, Events::META, Events::SOURCE_INPUT, Events::SOURCE_MOUSE_INTERACTION, Events::SOURCE_SCROLL

Instance Attribute Summary

Attributes inherited from Analyzer

#store

Instance Method Summary collapse

Methods included from EntryAttribution

#earlier?, #first_meta_href, #same_origin?, #update_entry_candidate

Methods included from Bounded

#bounded_fetch, #bounded_increment

Methods included from Stats

#mean, #offset_ms, #percentile, #top_counts

Constructor Details

#initialize(store = Sentiero.store, browser: nil, device: nil, url_pattern: nil, metadata_key: nil, metadata_value: nil, metadata_match: "exact", has_errors: false, min_duration_ms: nil, max_duration_ms: nil, country: nil, since: nil, until_time: nil) ⇒ Segmenter

Returns a new instance of Segmenter.



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
# File 'lib/sentiero/analytics/segmenter.rb', line 11

def initialize(
  store = Sentiero.store,
  browser: nil,
  device: nil,
  url_pattern: nil,
  metadata_key: nil,
  metadata_value: nil,
  metadata_match: "exact",
  has_errors: false,
  min_duration_ms: nil,
  max_duration_ms: nil,
  country: nil,
  since: nil,
  until_time: nil
)
  super(store)
  @browser = presence(browser)
  @device = presence(device)
  @url_pattern = presence(url_pattern)
  @metadata_key = presence()
  @metadata_value = presence()
  @metadata_match = ( == "contains") ? "contains" : "exact"
  @has_errors = has_errors
  @min_duration_ms = min_duration_ms
  @max_duration_ms = max_duration_ms
  # Normalized: Cloudflare sends uppercase ISO-2, but a custom proc may not.
  @country = presence(country)&.upcase
  @since = since
  @until_time = until_time
end

Instance Method Details

#matching(limit: 20, offset: 0) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sentiero/analytics/segmenter.rb', line 42

def matching(limit: 20, offset: 0)
  scan_cap = store.limits.analytics_max_scan_sessions

  scanned = store.list_sessions(limit: scan_cap, offset: 0, since: @since, until_time: @until_time)
  # Collected pre-filter, in the same scan (no extra store pass): the
  # dropdown must list every scanned country, not just the current selection.
  countries = scanned.filter_map { |s| s[:metadata]&.[]("geo_country") }.uniq.sort
  matches = scanned.select { |summary| match?(summary) }

  page = matches.slice(offset, limit + 1) || []
  has_next = page.size > limit

  {
    sessions: page.first(limit),
    has_next: has_next,
    was_truncated: scanned.size >= scan_cap,
    countries: countries
  }
end