sitegoose 0.3.5 → 0.3.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce69bd61fa38892bea9ea285bebd0688fd618deacd1715ae19ec14e3c7f61f02
4
- data.tar.gz: 83dcc0b5e52dab1c905b45dcb25c20bc242e96cd9c06cde495c5283d2a2812cd
3
+ metadata.gz: 309be56528382651cd8349212625ff2fd18a0d8eaa8ccf89feb37cc2dcf0b4c9
4
+ data.tar.gz: 8ae02d206bb48807ee2dd1f897ed6df9626d66d6d778b7a8ecb5add053c64d59
5
5
  SHA512:
6
- metadata.gz: a433baa5a8baa140e54ec32e47778576e8f37a1978216ffd91fe5ec059b2ccab3a187bec96aead0f5d3f54dc12abb1ee33beceb8d0942a99a4804d6ba6593aee
7
- data.tar.gz: 215d28e1a1ac655eb425c112be445495bbdf14dd03ea454dc32bd7b433054b7a3d40fcc9c835557674c1c75ce307d91be707f552e1d5b336f525f80137824c43
6
+ metadata.gz: 4b2813f31c8dd0e34d7fb96c34385d1a8dd3b516b33158672674b843cd9018dca539c8657c2e0af13a551d5825d346f343d92ff2f51d4e39eab38baf947d28ee
7
+ data.tar.gz: cab8dad76633bfbfdfd68646f653ada807c7ffe6e6e09705a52a3f685a05060cb32c7473b0769a22db4e2a497b940df89f09c0141cae18e43b673c132507d46d
data/README.md CHANGED
@@ -12,25 +12,30 @@ gem "sitegoose"
12
12
 
13
13
  ## Rails
14
14
 
15
- Nothing to add to the middleware stack: the gem's Railtie puts it first.
15
+ Add the gem and put your keys in the environment (site settings has all
16
+ three, ready to copy). That's all: the gem's Railtie puts its middleware
17
+ first.
16
18
 
17
- ```ruby
18
- # config/initializers/sitegoose.rb
19
- SiteGoose.configure(
20
- site_id: "YOUR_SITE_ID",
21
- secret: ENV["ANALYTICS_SITE_SECRET"], # site settings; keep it on the server
22
- endpoint: "https://YOUR_COLLECTOR_URL",
23
- # trust_proxy: 1, # behind Nginx or a load balancer
24
- # trust_proxy: "cloudflare", # every request through Cloudflare
25
- )
19
+ ```bash
20
+ ANALYTICS_SITE_ID=YOUR_SITE_ID
21
+ ANALYTICS_SITE_SECRET=YOUR_SITE_SECRET
22
+ ANALYTICS_ENDPOINT=https://YOUR_COLLECTOR_URL
23
+ # ANALYTICS_TRUST_PROXY=1 # behind Nginx or a load balancer
24
+ # ANALYTICS_TRUST_PROXY=cloudflare # every request through Cloudflare
25
+ # ANALYTICS_SERVER_NAME=api # several services on one site
26
26
  ```
27
27
 
28
- Or keep it in the credentials (`bin/rails credentials:edit`), under
29
- `sitegoose:` with the same keys, or in `config.sitegoose`.
30
- The environment works too: `ANALYTICS_SITE_ID`, `ANALYTICS_SITE_SECRET`,
31
- `ANALYTICS_ENDPOINT`, `ANALYTICS_SERVER_NAME`, `ANALYTICS_TRUST_PROXY`.
32
- Lowest first: the environment, credentials, `config.sitegoose`,
33
- `configure`.
28
+ With `dotenv-rails`, `.env` works as usual: the platform is made on first
29
+ use, after it has loaded.
30
+
31
+ Options in code go in an initializer, or the credentials
32
+ (`bin/rails credentials:edit`, under `sitegoose:`), or `config.sitegoose`.
33
+ Lowest first: the environment, credentials, `config.sitegoose`, `configure`.
34
+
35
+ ```ruby
36
+ # config/initializers/sitegoose.rb (optional)
37
+ SiteGoose.configure(trust_proxy: 1, errors: { capture_logging: true })
38
+ ```
34
39
 
35
40
  Then, on their own:
36
41
 
@@ -47,10 +52,11 @@ In your code: `SiteGoose.track(...)`, or `SiteGoose.platform`.
47
52
  ## Sinatra and other Rack apps
48
53
 
49
54
  ```ruby
50
- use SiteGoose::Middleware,
51
- site_id: "YOUR_SITE_ID", secret: ENV["ANALYTICS_SITE_SECRET"], endpoint: "https://YOUR_COLLECTOR_URL"
55
+ use SiteGoose::Middleware # the keys from ANALYTICS_* in the environment, as for Rails
52
56
  ```
53
57
 
58
+ Or pass them: `use SiteGoose::Middleware, site_id: "...", secret: ENV["ANALYTICS_SITE_SECRET"], endpoint: "https://..."`.
59
+
54
60
  Put it first; added twice (by hand next to the Railtie, or by an engine),
55
61
  the second copy passes requests straight on. Sinatra's route patterns (`/users/:id`) and its error pages are
56
62
  read on their own; set `set :raise_errors, false` (production's default) or
@@ -400,13 +406,20 @@ platform = SiteGoose.platform
400
406
  session = platform.session_id(request) # the visit, from the X-Analytics-Session header
401
407
 
402
408
  platform.track("purchase", { "numeric_value" => "49.90", "currency" => "EUR", "event_uid" => "order-#{order.id}" }, session_id: session)
403
- platform.identify(user.id, session_id: session)
409
+ platform.identify(user.id, session_id: session, traits: { plan: "pro", seats: 5, trial: false }) # traits are optional
404
410
  platform.lead({ "email" => "ana@example.com", "name" => "Ana", "status" => "qualified" })
405
411
  ```
406
412
 
407
413
  `track` sends in the background; `identify` and `lead` wait for the answer
408
414
  (up to 5 seconds).
409
415
 
416
+ **User traits:** `identify` takes optional `traits:`, a hash of name to text,
417
+ number or `true`/`false` (a symbol value is sent as its name). Arrays, hashes
418
+ and other values are left out (and `traits` is left off when nothing is left).
419
+ The collector does the rest: names are lowercased with spaces and dashes as
420
+ `_` (up to 40 letters, digits and `_`), text is cut at 200 characters, values
421
+ that look like an email address are dropped, and at most 20 are kept.
422
+
410
423
  **Link to the visit:** send the tracker's session ID with your site's requests:
411
424
 
412
425
  ```js
@@ -6,7 +6,8 @@
6
6
  #
7
7
  # - track(): events, batched to POST /events/server-events in the background.
8
8
  # Each gets an event_uid, so a retried batch can be told apart from new events.
9
- # - identify(): links the browser's visit to your user ID (/events/identify).
9
+ # - identify(): links the browser's visit to your user ID (/events/identify),
10
+ # with optional traits about the user (plan, seats...).
10
11
  # - lead(): adds or updates a lead (/events/lead).
11
12
  # identify() and lead() wait for the answer (up to 5 seconds).
12
13
 
@@ -79,14 +80,34 @@ module SiteGoose
79
80
  ok
80
81
  end
81
82
 
82
- # Links the visit to your user ID. true when stored.
83
- def identify(user_id, session_id = "")
83
+ # Links the visit to your user ID. true when stored. traits: what you know
84
+ # about the user, name => text, number or true/false
85
+ # ({ plan: "pro", seats: 5 }); anything else is left out, and the
86
+ # collector cleans the names and values.
87
+ def identify(user_id, session_id = "", traits = nil)
84
88
  return false if user_id.to_s.empty? || session_id.to_s.empty? || !@core.on?("analytics")
85
89
 
86
- res = @core.request("analytics", "/events/identify", body: { "user_id" => user_id.to_s, "session_id" => session_id.to_s })
90
+ body = { "user_id" => user_id.to_s, "session_id" => session_id.to_s }
91
+ kept = self.class.scalar_traits(traits)
92
+ body["traits"] = kept unless kept.empty?
93
+ res = @core.request("analytics", "/events/identify", body: body)
87
94
  res ? res.ok? : false
88
95
  end
89
96
 
97
+ # The traits that are text (a symbol as its name), a finite number or
98
+ # true/false, with their names as strings.
99
+ def self.scalar_traits(traits)
100
+ return {} unless traits.is_a?(Hash)
101
+
102
+ traits.each_with_object({}) do |(name, value), kept|
103
+ case value
104
+ when String, Integer, true, false then kept[name.to_s] = value
105
+ when Symbol then kept[name.to_s] = value.to_s
106
+ when Float then kept[name.to_s] = value if value.finite?
107
+ end
108
+ end
109
+ end
110
+
90
111
  # fields: email or phone, and name, status, tags, note, deal fields...
91
112
  # Returns {"status", "lead_id"}, or nil.
92
113
  def lead(fields, session_id = "")
@@ -438,7 +438,24 @@ module SiteGoose
438
438
  nil
439
439
  end
440
440
 
441
- HEADER_NAME = /\A[A-Za-z][A-Za-z0-9-]{0,63}\z/.freeze
441
+ # The nine headers the dashboard can produce (expresscollect/Analytics/
442
+ # platform.js, headersOf), lower case. A list of what's allowed, not of
443
+ # what isn't: whoever got into our servers still couldn't make this app
444
+ # grant another site its users' data (Access-Control-Allow-*), send every
445
+ # page elsewhere (Refresh), log everyone out (Clear-Site-Data) or change
446
+ # what a response is. Shared cases: sdk/protocols/header-cases.json.
447
+ SECURITY_HEADERS = %w[strict-transport-security x-content-type-options x-frame-options referrer-policy
448
+ permissions-policy cross-origin-opener-policy content-security-policy
449
+ content-security-policy-report-only reporting-endpoints].freeze
450
+ HEADER_VALUE = /\A[\x20-\x7e\t]*\z/.freeze
451
+
452
+ # One of SECURITY_HEADERS, in any case, with a value of visible ASCII,
453
+ # spaces and tabs (no line breaks, nothing a server rejects), at most
454
+ # 4,000 characters.
455
+ def self.safe_header?(pair)
456
+ pair.is_a?(Array) && pair.size == 2 && pair[0].is_a?(String) && SECURITY_HEADERS.include?(pair[0].downcase) &&
457
+ pair[1].is_a?(String) && pair[1].size <= 4000 && pair[1].match?(HEADER_VALUE)
458
+ end
442
459
 
443
460
  def sync_switches
444
461
  headers = { "X-Platform-Modules" => @loaded.join(",") }
@@ -454,11 +471,7 @@ module SiteGoose
454
471
  @switches = data["modules"]
455
472
  @flags = data["flags"].is_a?(Hash) ? data["flags"] : {}
456
473
  @maintenance = data["maintenance"].is_a?(Hash) ? data["maintenance"] : { "on" => false }
457
- # Only well-formed headers (no line breaks can get through).
458
- @headers = (data["headers"].is_a?(Array) ? data["headers"] : []).select do |pair|
459
- pair.is_a?(Array) && pair.size == 2 && pair[0].is_a?(String) && pair[0].match?(HEADER_NAME) &&
460
- pair[1].is_a?(String) && !pair[1].include?("\r") && !pair[1].include?("\n") && pair[1].size <= 4000
461
- end
474
+ @headers = (data["headers"].is_a?(Array) ? data["headers"] : []).select { |pair| Core.safe_header?(pair) }
462
475
  @etag = res.headers["etag"].to_s
463
476
  end
464
477
  [MIN_SYNC, (data["sync"].is_a?(Numeric) && data["sync"].positive? ? data["sync"].to_i : RETRY)].max
@@ -264,9 +264,10 @@ module SiteGoose
264
264
  analytics ? analytics.track(name, fields, session_id) : ""
265
265
  end
266
266
 
267
- def identify(user_id, session_id: "")
267
+ # traits: optional user properties, name => text, number or true/false.
268
+ def identify(user_id, session_id: "", traits: nil)
268
269
  analytics = @modules["analytics"]
269
- analytics ? analytics.identify(user_id, session_id) : false
270
+ analytics ? analytics.identify(user_id, session_id, traits) : false
270
271
  end
271
272
 
272
273
  def lead(fields, session_id: "")
@@ -33,6 +33,19 @@ module SiteGoose
33
33
  # The collector reads these; everything else, cookies above all, stays here.
34
34
  PASS_HEADERS = %w[content-type user-agent accept-language referer origin sec-fetch-site sec-ch-ua
35
35
  sec-ch-ua-mobile sec-ch-ua-platform].freeze
36
+ # What a collector answer may be served as from the site's own domain. Its
37
+ # type comes from the collector, so anything else becomes plain text, and
38
+ # nosniff stops a browser guessing: a server that is no longer ours still
39
+ # can't answer <prefix>/api/... with a page that runs as the customer's own
40
+ # site. Shared cases: sdk/protocols/proxy-answer-cases.json.
41
+ ANSWER_TYPES = %w[application/json text/plain image/webp].freeze
42
+
43
+ def self.answer_type(value)
44
+ given = value.is_a?(String) ? value : ""
45
+ kind = given.split(";", 2).first.to_s.strip.downcase
46
+ ANSWER_TYPES.include?(kind) && given.match?(/\A[\x20-\x7e]*\z/) ? given : "text/plain; charset=utf-8"
47
+ end
48
+
36
49
  SAFE_PATH = %r{\A[A-Za-z0-9/_.-]{1,120}\z}.freeze
37
50
  FILE_PATH = %r{\Av(\d+(?:\.\d+\.\d+)?)/([a-z]+\.js)\z}.freeze
38
51
  # These run on the app's own request threads, so they are short, and a
@@ -238,8 +251,8 @@ module SiteGoose
238
251
  begin
239
252
  answer = fetch(url, method: method, headers: out, body: method == "POST" ? body.to_s : nil)
240
253
  answer.code.to_i >= 500 ? failed : worked
241
- passed = {}
242
- %w[content-type cache-control retry-after].each do |name|
254
+ passed = { "content-type" => Proxy.answer_type(answer["content-type"]), "x-content-type-options" => "nosniff" }
255
+ %w[cache-control retry-after].each do |name|
243
256
  value = answer[name]
244
257
  passed[name] = value if value && !value.empty?
245
258
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SiteGoose
4
- VERSION = "0.3.5"
4
+ VERSION = "0.3.6"
5
5
  end
data/lib/sitegoose.rb CHANGED
@@ -207,8 +207,8 @@ module SiteGoose
207
207
  platform&.track(name, fields, session_id: session_id) || ""
208
208
  end
209
209
 
210
- def identify(user_id, session_id: "")
211
- platform ? platform.identify(user_id, session_id: session_id) : false
210
+ def identify(user_id, session_id: "", traits: nil)
211
+ platform ? platform.identify(user_id, session_id: session_id, traits: traits) : false
212
212
  end
213
213
 
214
214
  def lead(fields, session_id: "")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitegoose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - steviec87
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-07 00:00:00.000000000 Z
11
+ date: 2026-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack