Class: Sentiero::Analytics::WebVitalsAnalyzer

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

Overview

Aggregates Web Vitals per page URL, scanning the store on read. The recorder emits one "__perf" custom event per metric report carrying value, rating; ratings come from the client web-vitals library and are tallied as-is. Per-segment math (last-wins collapse, rating histogram, worst-sample) lives in VitalsCollector, shared with PageReportAnalyzer.

Constant Summary collapse

MAX_URLS =

Cap on distinct URLs tracked; sessions scan newest-first, so the cap keeps the most recently visited URLs.

200
MAX_SAMPLES_PER_METRIC =
2000

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 inherited from Analyzer

#initialize

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

This class inherits a constructor from Sentiero::Analytics::Analyzer

Instance Method Details

#analyze(limit: nil, since: nil, until_time: nil) ⇒ Object

Samples are attributed per page segment so each report lands on the page it measured. Within a segment, repeated reports of the same metric collapse to the LAST one (one sample == one page view's final value) so re-emitted candidates and reloads cannot inflate counts or skew percentiles.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sentiero/analytics/web_vitals_analyzer.rb', line 24

def analyze(limit: nil, since: nil, until_time: nil)
  pages = {} # url => VitalsCollector
  accumulation_capped = false

  _scanned, hit_cap = scan_sessions(limit: limit, since: since, until_time: until_time) do |summary, window_id, events|
    each_page_segment(events) do |url, segment, anchor|
      next unless url

      collector = collector_for(pages, url)
      unless collector
        accumulation_capped = true
        next
      end

      collector.collect(segment, session_id: summary[:session_id], window_id: window_id, anchor: anchor)
      accumulation_capped = true if collector.capped
    end
  end

  {
    pages: pages.transform_values(&:summarize),
    was_truncated: accumulation_capped || hit_cap
  }
end