Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Parse Google Analytics failed response before reporting to Sentry (#2354)

Vinicius Brasil95aafb477d46
diff --git a/lib/plausible/google/http.ex b/lib/plausible/google/http.ex
index bb324168fd..6b32513793 100644
--- a/lib/plausible/google/http.ex
+++ b/lib/plausible/google/http.ex
@@ -42,8 +42,8 @@ defmodule Plausible.Google.HTTP do
{:ok, report} <- convert_to_maps(report) do
{:ok, {report, token}}
else
- {:ok, %{status: _non_http_200, body: body}} ->
- Sentry.Context.set_extra_context(%{google_analytics_response: body})
+ {:ok, %{status: _non_http_200, body: _body} = response} ->
+ report_failed_request_to_sentry(response)
{:error, :request_failed}
{:error, cause} ->
@@ -51,6 +51,16 @@ defmodule Plausible.Google.HTTP do
end
end
+ defp report_failed_request_to_sentry(%{status: status, body: body}) do
+ case Jason.decode(body) do
+ {:ok, %{} = body} ->
+ Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: status}})
+
+ _error ->
+ Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: status}})
+ end
+ end
+
defp parse_report_from_response(raw_body) do
with {:ok, map} <- Jason.decode(raw_body),
%{"reports" => [report | _]} <- map do