Module: Dispatch::WidgetHelper

Defined in:
app/helpers/dispatch/widget_helper.rb

Instance Method Summary collapse

Instance Method Details

#dispatch_error_tracker_tag(tags: {}) ⇒ Object

Render the browser exception tracker (captures uncaught JS errors and unhandled promise rejections). Place once in your layout’s <head>.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/dispatch/widget_helper.rb', line 39

def dispatch_error_tracker_tag(tags: {})
  config = Dispatch::Rails.configuration
  return nil if config.errors_only? # no browser surface in an API-only app
  return nil unless config.configured? && config.capture_browser_errors
  return nil unless config.environment_enabled?

  user = config.user.respond_to?(:call) ? config.user.call(self) : nil
  normalized_user = user && { email: user[:email], id: user[:external_id] || user[:id] }.compact

  render(
    partial: "dispatch/error_tracker",
    locals: {
      error_config_json: {
        endpoint: config.effective_error_endpoint,
        apiKey: config.api_key,
        environment: config.effective_environment,
        release: config.release,
        sampleRate: config.error_sample_rate,
        captureClicks: config.capture_clicks,
        user: normalized_user,
        tags: tags
      }.to_json
    }
  )
end

#dispatch_widget_tag(severity: nil, labels: nil, extra_metadata: {}) ⇒ Object

Render the floating bug-report widget.

Options:

severity:       one of "low", "medium", "high", "critical" — pre-classifies reports
                from this page (useful for marking a /checkout page as critical-by-default)
labels:         array of strings merged into metadata.labels so reports can be tagged per-surface
extra_metadata: arbitrary hash merged into the metadata blob


10
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
# File 'app/helpers/dispatch/widget_helper.rb', line 10

def dispatch_widget_tag(severity: nil, labels: nil, extra_metadata: {})
  config = Dispatch::Rails.configuration
  return nil if config.errors_only? # headless / API-only apps have no widget
  return nil unless config.configured?

  user = config.user.respond_to?(:call) ? config.user.call(self) : nil
   = config..respond_to?(:call) ? config..call(self) : {}

   = .to_h.merge()
  [:labels] = Array(labels).map(&:to_s) if labels.present?
  [:severity_hint] = severity.to_s if severity.present?

  render(
    partial: "dispatch/widget",
    locals: {
      api_key: config.api_key,
      endpoint: config.endpoint,
      user: user,
      metadata: ,
      severity: severity&.to_s,
      capture_console: config.capture_console,
      capture_clicks: config.capture_clicks,
      button_position: config.button_position
    }
  )
end