Module: Sentiero::Web::ScriptTag

Extended by:
Escaping
Defined in:
lib/sentiero/web/script_tag.rb

Constant Summary

Constants included from Escaping

Escaping::HTML_UNSAFE_IN_SCRIPT, Escaping::HTML_UNSAFE_IN_SCRIPT_PATTERN

Class Method Summary collapse

Methods included from Escaping

escape_html, escape_js_string, escape_json

Class Method Details

.render(events_url:, recorder_url: nil) ⇒ Object



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

def self.render(events_url:, recorder_url: nil)
  config = Sentiero.configuration

  recorder_url ||= default_recorder_url(events_url)

  json_data = {
    eventsUrl: events_url,
    flushIntervalMs: config.flush_interval_ms,
    flushEventThreshold: config.flush_event_threshold,
    recorderOptions: config.effective_recorder_options,
    crossTabSessions: config.cross_tab_sessions,
    redaction: config.redaction.to_client_hash,
    # Seconds on the Ruby side (matches retention_period's unit); ms on
    # the wire so the client can compare directly against Date.now().
    sessionIdleTimeoutMs: config.session_idle_timeout * 1000,
    sessionMaxAgeMs: config.session_max_age * 1000
  }
  json_data[:captureMetadata] = true if config.
  json_data[:captureErrors] = true if config.capture_errors
  json_data[:trackNavigation] = true if config.track_navigation
  json_data[:trackCustomEvents] = true if config.track_custom_events
  json_data[:captureWebVitals] = true if config.capture_web_vitals
  json_data[:captureClicks] = true if config.capture_clicks
  json_data[:trackForms] = true if config.track_forms
  json_data[:optOutCookieName] = config.opt_out_cookie_name if config.user_opt_out
  json_data[:respectGpc] = true if config.respect_gpc

  config_json = JSON.generate(json_data)

  safe_json = escape_json(config_json)
  escaped_recorder_url = escape_html(recorder_url.to_s)

  <<~HTML
    <script type="application/json" id="sentiero-config">#{safe_json}</script>
    <script src="#{escaped_recorder_url}"></script>
  HTML
end