From c5dd5495152a9024266f7ccd4eb8482e0452944c Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 13 Feb 2024 15:17:14 +0000 Subject: [PATCH] Use original_fullpath for request path This changes the event to get the request path from the `original_fullpath` method rather than the `path` method. I'm making this change as the `original_fullpath` represents the path of the page as requested by the user, not necessarily the page being rendered. In most cases the original path and the path will be the same, but there are some scenarios where it is. Specifically, when rendering a custom error page in Rails which is mounted at `/404` the path ends up being the value `/404` rather than the path which triggered the 404 page. --- lib/dfe/analytics/event.rb | 2 +- spec/dfe/analytics/event_spec.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/dfe/analytics/event.rb b/lib/dfe/analytics/event.rb index d3534ba8..a34394f6 100644 --- a/lib/dfe/analytics/event.rb +++ b/lib/dfe/analytics/event.rb @@ -36,7 +36,7 @@ def with_request_details(rack_request) request_uuid: rack_request.uuid, request_user_agent: ensure_utf8(rack_request.user_agent), request_method: rack_request.method, - request_path: ensure_utf8(rack_request.path), + request_path: ensure_utf8(rack_request.original_fullpath.split('?').first), request_query: hash_to_kv_pairs(Rack::Utils.parse_query(rack_request.query_string)), request_referer: ensure_utf8(rack_request.referer), anonymised_user_agent_and_ip: anonymised_user_agent_and_ip(rack_request) diff --git a/spec/dfe/analytics/event_spec.rb b/spec/dfe/analytics/event_spec.rb index 1e57e9dd..d5b7b740 100644 --- a/spec/dfe/analytics/event_spec.rb +++ b/spec/dfe/analytics/event_spec.rb @@ -14,8 +14,8 @@ 'request_uuid' => '123', 'request_user_agent' => 'SomeClient', 'request_method' => 'GET', - 'request_path' => '/', - 'request_query' => [], + 'request_path' => '/path', + 'request_query' => [{ 'key' => 'a', 'value' => ['b'] }], 'request_referer' => nil }) end @@ -213,8 +213,8 @@ def fake_request(overrides = {}) attrs = { uuid: '123', method: 'GET', - path: '/', - query_string: '', + original_fullpath: '/path?a=b', + query_string: 'a=b', referer: nil, user_agent: 'SomeClient', remote_ip: '1.2.3.4'