rails_mind 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +35 -1
- data/CONTRIBUTING.md +6 -7
- data/README.md +11 -6
- data/docs/compatibility.md +31 -14
- data/docs/sdk.md +141 -39
- data/lib/generators/rails_mind/install/install_generator.rb +0 -12
- data/lib/generators/rails_mind/install/templates/initializer.rb.tt +8 -1
- data/lib/rails_mind/client.rb +3 -2
- data/lib/rails_mind/configuration.rb +8 -2
- data/lib/rails_mind/context.rb +8 -2
- data/lib/rails_mind/diagnostic.rb +18 -3
- data/lib/rails_mind/integrations/rails.rb +76 -9
- data/lib/rails_mind/page_view.rb +176 -0
- data/lib/rails_mind/redactor.rb +127 -12
- data/lib/rails_mind/transport.rb +14 -0
- data/lib/rails_mind/version.rb +1 -1
- data/lib/rails_mind/visitor.rb +134 -0
- data/lib/tasks/rails_mind.rake +8 -2
- metadata +5 -5
- data/javascript/rails_mind.js +0 -49
- data/lib/rails_mind/integrations/ahoy.rb +0 -68
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
require "date"
|
|
2
|
+
require "openssl"
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
require "rack/utils"
|
|
6
|
+
|
|
7
|
+
module RailsMind
|
|
8
|
+
# Who is looking at a page before they sign in. By default a random ID kept in
|
|
9
|
+
# a first-party cookie; when no cookie can be set, a hash of the day, the IP
|
|
10
|
+
# and the user agent that changes every local day. The IP never leaves the app.
|
|
11
|
+
module Visitor
|
|
12
|
+
COOKIE = "_rails_mind_visitor".freeze
|
|
13
|
+
# The gem mints lowercase version 4 UUIDs. Anything else in the cookie came
|
|
14
|
+
# from somewhere else and is replaced, so hostile values never travel.
|
|
15
|
+
ID = /\A[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/
|
|
16
|
+
COOKIE_LIFETIME = 2 * 365 * 24 * 60 * 60
|
|
17
|
+
# Purpose-specific, derived from secret_key_base. Never the ingest key:
|
|
18
|
+
# RailsMind receives that with every batch and could brute-force the IPs.
|
|
19
|
+
KEY_SALT = "rails_mind visitor v1".freeze
|
|
20
|
+
|
|
21
|
+
# Rack env keys shared by the request boundary and the page-view subscriber.
|
|
22
|
+
ENV_ID = "rails_mind.visitor_id".freeze
|
|
23
|
+
ENV_COOKIE = "rails_mind.visitor_cookie".freeze
|
|
24
|
+
ENV_DAILY = "rails_mind.daily_visitor_id".freeze
|
|
25
|
+
WRITE = "rails_mind.write_visitor_cookie".freeze
|
|
26
|
+
SEED_DAILY = "rails_mind.seed_daily_visitor".freeze
|
|
27
|
+
PUBLIC = /(?:\A|[,\s])public(?:\z|[,\s=])/i
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
def mint = SecureRandom.uuid
|
|
31
|
+
|
|
32
|
+
# The first valid ID among the request's cookies of that name.
|
|
33
|
+
def from_cookie(header)
|
|
34
|
+
return unless header.is_a?(String)
|
|
35
|
+
header.split(";").each do |pair|
|
|
36
|
+
name, value = pair.split("=", 2)
|
|
37
|
+
next unless name.to_s.strip == COOKIE
|
|
38
|
+
value = value.to_s.strip
|
|
39
|
+
return value if value.match?(ID)
|
|
40
|
+
end
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def daily_hash(remote_ip, user_agent, date = today)
|
|
45
|
+
ip = remote_ip.to_s
|
|
46
|
+
secret = key
|
|
47
|
+
return if ip.empty? || secret.nil? || secret.empty?
|
|
48
|
+
OpenSSL::HMAC.hexdigest("SHA256", secret, "#{date.iso8601}\n#{ip}\n#{user_agent}")[0, 32]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# One request keeps one hash, even across local midnight.
|
|
52
|
+
def daily_id(request)
|
|
53
|
+
env = request.env
|
|
54
|
+
return env[ENV_DAILY] if env.key?(ENV_DAILY)
|
|
55
|
+
env[ENV_DAILY] = daily_hash(request.remote_ip, request.user_agent)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The local day in the app's time zone, the day RailsMind counts in.
|
|
59
|
+
def today
|
|
60
|
+
zone = Time.zone_default if Time.respond_to?(:zone_default)
|
|
61
|
+
(zone ? zone.now : Time.now.utc).to_date
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def key = @key ||= derive_key
|
|
65
|
+
|
|
66
|
+
def derive_key
|
|
67
|
+
return unless defined?(::Rails) && ::Rails.respond_to?(:application)
|
|
68
|
+
::Rails.application&.key_generator&.generate_key(KEY_SALT, 32)
|
|
69
|
+
rescue StandardError
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# At the request boundary: the visitor's cookie ID, or a fresh one for a
|
|
74
|
+
# browser navigation that may become a counted page view, goes into context
|
|
75
|
+
# so every event of the request carries it. Only such a request can set the
|
|
76
|
+
# cookie, so others without one (POSTs, JSON and API GETs, Turbo Frames,
|
|
77
|
+
# XHRs, prefetches) get no ID that would never be stored. Without the
|
|
78
|
+
# cookie, the daily hash waits for the controller, where the app's own
|
|
79
|
+
# remote IP is known.
|
|
80
|
+
def start(env)
|
|
81
|
+
client = RailsMind.client
|
|
82
|
+
config = client.config
|
|
83
|
+
return unless client.collector && config.analytics && config.page_views
|
|
84
|
+
unless config.visitor_cookie
|
|
85
|
+
env[SEED_DAILY] = true
|
|
86
|
+
return
|
|
87
|
+
end
|
|
88
|
+
incoming = from_cookie(env["HTTP_COOKIE"])
|
|
89
|
+
env[ENV_COOKIE] = incoming if incoming
|
|
90
|
+
context = Context.current
|
|
91
|
+
return unless context[:visitor_id].to_s.empty?
|
|
92
|
+
id = incoming || (mint if navigation?(env))
|
|
93
|
+
return unless id
|
|
94
|
+
env[ENV_ID] = id
|
|
95
|
+
context[:visitor_id] = id
|
|
96
|
+
rescue StandardError
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# A GET a browser makes for a whole HTML page, the only request whose
|
|
101
|
+
# response can count as a page view and set the cookie.
|
|
102
|
+
def navigation?(env)
|
|
103
|
+
return false unless env["REQUEST_METHOD"] == "GET" && env["HTTP_ACCEPT"].to_s.include?("text/html")
|
|
104
|
+
return false unless env["HTTP_TURBO_FRAME"].to_s.empty?
|
|
105
|
+
return false if env["HTTP_X_REQUESTED_WITH"].to_s.casecmp?("XMLHttpRequest")
|
|
106
|
+
%w[HTTP_SEC_PURPOSE HTTP_X_SEC_PURPOSE].none? { |header| env[header].to_s.downcase.include?("prefetch") }
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# When a controller starts, after ActionDispatch::RemoteIp: the daily hash
|
|
110
|
+
# goes into context so the request's events carry the page view's visitor.
|
|
111
|
+
def seed_daily(request)
|
|
112
|
+
return unless request&.get_header(SEED_DAILY) && Context.current[:visitor_id].to_s.empty?
|
|
113
|
+
id = daily_id(request)
|
|
114
|
+
Context.current[:visitor_id] = id if id
|
|
115
|
+
rescue StandardError
|
|
116
|
+
nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# After the app: a counted page view marked the request, so the cookie is
|
|
120
|
+
# set or refreshed, unless the final response may be cached publicly.
|
|
121
|
+
def finish(env, response)
|
|
122
|
+
write = env[WRITE]
|
|
123
|
+
id = env[ENV_ID]
|
|
124
|
+
return unless write && id
|
|
125
|
+
headers = response[1]
|
|
126
|
+
return if (headers["cache-control"] || headers["Cache-Control"]).to_s.match?(PUBLIC)
|
|
127
|
+
Rack::Utils.set_cookie_header!(headers, COOKIE, value: id, path: "/", expires: Time.now + COOKIE_LIFETIME,
|
|
128
|
+
httponly: true, same_site: :lax, secure: write[:secure] == true)
|
|
129
|
+
rescue StandardError
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
data/lib/tasks/rails_mind.rake
CHANGED
|
@@ -7,9 +7,15 @@ namespace :rails_mind do
|
|
|
7
7
|
exit 1 unless result[:success]
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
desc "Inspect dependencies and report the non-destructive integration plan"
|
|
10
|
+
desc "Inspect dependencies and report the non-destructive integration plan; fails while collection is off"
|
|
11
11
|
task doctor: :environment do
|
|
12
12
|
require "rails_mind/diagnostic"
|
|
13
|
-
|
|
13
|
+
diagnostic = RailsMind::Diagnostic.new(root: Rails.root)
|
|
14
|
+
puts JSON.pretty_generate(diagnostic.report.merge(collector: RailsMind.stats))
|
|
15
|
+
problems = diagnostic.problems
|
|
16
|
+
unless problems.empty?
|
|
17
|
+
warn problems
|
|
18
|
+
exit 1
|
|
19
|
+
end
|
|
14
20
|
end
|
|
15
21
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_mind
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- RailsMind
|
|
@@ -49,8 +49,8 @@ dependencies:
|
|
|
49
49
|
- - "<"
|
|
50
50
|
- !ruby/object:Gem::Version
|
|
51
51
|
version: '1'
|
|
52
|
-
description: The RailsMind client for Rails request, error, job, mailer,
|
|
53
|
-
telemetry, with optional
|
|
52
|
+
description: The RailsMind client for Rails page views and request, error, job, mailer,
|
|
53
|
+
and business-event telemetry, with optional Flipper and OpenTelemetry integrations.
|
|
54
54
|
executables: []
|
|
55
55
|
extensions: []
|
|
56
56
|
extra_rdoc_files: []
|
|
@@ -62,7 +62,6 @@ files:
|
|
|
62
62
|
- docs/agents.md
|
|
63
63
|
- docs/compatibility.md
|
|
64
64
|
- docs/sdk.md
|
|
65
|
-
- javascript/rails_mind.js
|
|
66
65
|
- lib/generators/rails_mind/agent/agent_generator.rb
|
|
67
66
|
- lib/generators/rails_mind/agent/templates/headers.rb.tt
|
|
68
67
|
- lib/generators/rails_mind/install/install_generator.rb
|
|
@@ -76,14 +75,15 @@ files:
|
|
|
76
75
|
- lib/rails_mind/diagnostic.rb
|
|
77
76
|
- lib/rails_mind/installation_check.rb
|
|
78
77
|
- lib/rails_mind/integrations/action_mailer.rb
|
|
79
|
-
- lib/rails_mind/integrations/ahoy.rb
|
|
80
78
|
- lib/rails_mind/integrations/open_telemetry.rb
|
|
81
79
|
- lib/rails_mind/integrations/rails.rb
|
|
80
|
+
- lib/rails_mind/page_view.rb
|
|
82
81
|
- lib/rails_mind/railtie.rb
|
|
83
82
|
- lib/rails_mind/redactor.rb
|
|
84
83
|
- lib/rails_mind/sampling.rb
|
|
85
84
|
- lib/rails_mind/transport.rb
|
|
86
85
|
- lib/rails_mind/version.rb
|
|
86
|
+
- lib/rails_mind/visitor.rb
|
|
87
87
|
- lib/tasks/rails_mind.rake
|
|
88
88
|
homepage: https://github.com/cmartyn/rails_mind
|
|
89
89
|
licenses:
|
data/javascript/rails_mind.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
// Uses the application's Ahoy endpoint/cookies. Never put the server token here.
|
|
2
|
-
// Do not run alongside another automatic pageview handler.
|
|
3
|
-
const installations = new WeakMap()
|
|
4
|
-
|
|
5
|
-
export function start({ ahoy, document: doc = document, turbo = true, pageviews = true, frames = false,
|
|
6
|
-
page = () => doc.querySelector('meta[name="rails-mind-page"]')?.content || "page" } = {}) {
|
|
7
|
-
if (!ahoy || typeof ahoy.track !== "function") throw new TypeError("Pass the existing Ahoy instance")
|
|
8
|
-
if (installations.has(doc)) return installations.get(doc)
|
|
9
|
-
const listeners = []
|
|
10
|
-
let visit = 0
|
|
11
|
-
let trackedVisit = -1
|
|
12
|
-
|
|
13
|
-
function on(name, listener) {
|
|
14
|
-
doc.addEventListener(name, listener)
|
|
15
|
-
listeners.push([name, listener])
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function trackPage() {
|
|
19
|
-
if (!pageviews || trackedVisit === visit || doc.documentElement.hasAttribute("data-turbo-preview")) return
|
|
20
|
-
trackedVisit = visit
|
|
21
|
-
ahoy.track("$pageview", { page: String(page()).slice(0, 160), navigation: turbo ? "turbo" : "document" })
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (turbo) {
|
|
25
|
-
on("turbo:visit", () => { visit += 1 })
|
|
26
|
-
on("turbo:load", trackPage)
|
|
27
|
-
} else {
|
|
28
|
-
on("DOMContentLoaded", trackPage)
|
|
29
|
-
}
|
|
30
|
-
// Supports loading the adapter after the initial page load, without duplicating
|
|
31
|
-
// a subsequent turbo:load for that same initial navigation.
|
|
32
|
-
if (doc.readyState === "complete") trackPage()
|
|
33
|
-
|
|
34
|
-
if (frames) on("turbo:frame-load", (event) => {
|
|
35
|
-
const label = event.target?.dataset?.analyticsFrame
|
|
36
|
-
if (label) ahoy.track("$frame_view", { frame: String(label).slice(0, 100), page: String(page()).slice(0, 160) })
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
const api = {
|
|
40
|
-
// The caller records a business outcome only after it actually succeeds.
|
|
41
|
-
track: (name, properties = {}) => ahoy.track(name, properties),
|
|
42
|
-
stop() {
|
|
43
|
-
listeners.forEach(([name, listener]) => doc.removeEventListener(name, listener))
|
|
44
|
-
installations.delete(doc)
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
installations.set(doc, api)
|
|
48
|
-
return api
|
|
49
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
require "ahoy"
|
|
2
|
-
|
|
3
|
-
module RailsMind
|
|
4
|
-
module Integrations
|
|
5
|
-
module AhoyDelivery
|
|
6
|
-
def rails_mind_visit(data)
|
|
7
|
-
RailsMind.client.emit(:visit, "$visit", id: data[:visit_token], occurred_at: data[:started_at],
|
|
8
|
-
visitor_id: data[:visitor_token], user_id: data[:user_id]&.to_s,
|
|
9
|
-
properties: data.slice(:referring_domain, :utm_source, :utm_medium, :utm_campaign, :browser, :os, :device_type))
|
|
10
|
-
rescue StandardError
|
|
11
|
-
false
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def rails_mind_event(data)
|
|
15
|
-
RailsMind.client.emit(:event, data[:name], id: data[:event_id], occurred_at: data[:time],
|
|
16
|
-
visitor_id: ahoy&.visitor_token, user_id: data[:user_id]&.to_s,
|
|
17
|
-
properties: data.fetch(:properties, {}).merge(visit_id: data[:visit_token]))
|
|
18
|
-
rescue StandardError
|
|
19
|
-
false
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def rails_mind_authenticate(data)
|
|
23
|
-
RailsMind.client.emit(:event, "$identify", visitor_id: ahoy&.visitor_token,
|
|
24
|
-
user_id: data[:user_id]&.to_s, properties: { visit_id: data[:visit_token] })
|
|
25
|
-
rescue StandardError
|
|
26
|
-
false
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Explicit hosted-only option. Set `class Ahoy::Store < ...` in the app.
|
|
31
|
-
# This intentionally has no visit ActiveRecord model or geocoding persistence.
|
|
32
|
-
class AhoyStore < ::Ahoy::BaseStore
|
|
33
|
-
include AhoyDelivery
|
|
34
|
-
|
|
35
|
-
def track_visit(data) = rails_mind_visit(data)
|
|
36
|
-
def track_event(data) = rails_mind_event(data)
|
|
37
|
-
def authenticate(data) = rails_mind_authenticate(data)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Explicit coexistence option. Prepend once to the customer's existing Store.
|
|
41
|
-
# The existing store runs first; its arguments, exceptions and return value survive.
|
|
42
|
-
module AhoyMirror
|
|
43
|
-
include AhoyDelivery
|
|
44
|
-
|
|
45
|
-
def track_visit(data)
|
|
46
|
-
result = super
|
|
47
|
-
rails_mind_visit(data)
|
|
48
|
-
result
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def track_event(data)
|
|
52
|
-
result = super
|
|
53
|
-
rails_mind_event(data)
|
|
54
|
-
result
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def authenticate(data)
|
|
58
|
-
result = super
|
|
59
|
-
rails_mind_authenticate(data)
|
|
60
|
-
result
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def self.mirror_ahoy!(store = ::Ahoy::Store)
|
|
65
|
-
store.prepend(AhoyMirror) unless store.ancestors.include?(AhoyMirror)
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
end
|