Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mirror-raw-user-agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-rails": patch
---

fix: also send the request user agent as $raw_user_agent, the standardized property PostHog's server-side classification reads
6 changes: 5 additions & 1 deletion posthog-rails/lib/posthog/rails/request_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def extract(request)
request_method = request_value(request, :request_method) || request_value(request, :method)
add_property(properties, '$request_method', request_method)
add_property(properties, '$request_path', request_value(request, :path) || request_value(request, :path_info))
add_property(properties, '$user_agent', TracingHeaders.extract_header(request, 'User-Agent'))
user_agent = TracingHeaders.extract_header(request, 'User-Agent')
add_property(properties, '$user_agent', user_agent)
# Mirrored into $raw_user_agent, the standardized property PostHog's
# server-side classification (e.g. bot detection) reads
add_property(properties, '$raw_user_agent', user_agent)
add_property(properties, '$ip', client_ip(request))
properties
end
Expand Down
4 changes: 4 additions & 0 deletions spec/posthog/rails/request_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def call_with(headers = nil, path: '/api/test', **header_keywords, &block)
expect(message[:properties]['$request_method']).to eq('POST')
expect(message[:properties]['$request_path']).to eq('/api/test')
expect(message[:properties]['$user_agent']).to eq('RSpec Agent')
expect(message[:properties]['$raw_user_agent']).to eq('RSpec Agent')
expect(message[:properties]['$ip']).to eq('203.0.113.10')
end

Expand Down Expand Up @@ -92,6 +93,7 @@ def call_with(headers = nil, path: '/api/test', **header_keywords, &block)
expect(message[:properties]['$session_id']).to be_nil
expect(message[:properties]['$request_path']).to eq('/api/test')
expect(message[:properties]['$user_agent']).to eq('RSpec Agent')
expect(message[:properties]['$raw_user_agent']).to eq('RSpec Agent')
expect(message[:properties]['$process_person_profile']).to be false
end

Expand Down Expand Up @@ -339,6 +341,7 @@ def action_name
expect(message[:properties]['$session_id']).to eq('exception-session')
expect(message[:properties]['$request_path']).to eq('/boom')
expect(message[:properties]['$user_agent']).to eq('Exception Agent')
expect(message[:properties]['$raw_user_agent']).to eq('Exception Agent')
end

it 'disables tracing headers for exceptions while preserving request metadata' do
Expand Down Expand Up @@ -371,6 +374,7 @@ def action_name
expect(message[:properties]['$process_person_profile']).to be false
expect(message[:properties]['$request_path']).to eq('/boom')
expect(message[:properties]['$user_agent']).to eq('Disabled Context Agent')
expect(message[:properties]['$raw_user_agent']).to eq('Disabled Context Agent')
expect(message[:properties]['$ip']).to eq('203.0.113.11')
end
end
Loading