Skip to content

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

Avoid crashing on empty GA4 HTTP report response (#4094)

Adrian Gruntkowski6bbc8d69a468
diff --git a/lib/plausible/google/ga4/api.ex b/lib/plausible/google/ga4/api.ex
index 0cc26e16a7..64da14ba0f 100644
--- a/lib/plausible/google/ga4/api.ex
+++ b/lib/plausible/google/ga4/api.ex
@@ -137,6 +137,13 @@ defmodule Plausible.Google.GA4.API do
sleep_time = Keyword.get(opts, :sleep_time, @backoff_factor)
case GA4.HTTP.get_report(report_request) do
+ {:ok, {_, 0}} ->
+ Logger.debug(
+ "[#{inspect(__MODULE__)}:#{report_request.property}] Fetched empty response for #{report_request.dataset}"
+ )
+
+ :ok
+
{:ok, {rows, row_count}} ->
Logger.debug(
"[#{inspect(__MODULE__)}:#{report_request.property}] Fetched #{length(rows)} rows of total #{row_count} with offset #{report_request.offset} for #{report_request.dataset}"
diff --git a/lib/plausible/google/ga4/http.ex b/lib/plausible/google/ga4/http.ex
index 39eb97d423..a41635555e 100644
--- a/lib/plausible/google/ga4/http.ex
+++ b/lib/plausible/google/ga4/http.ex
@@ -51,7 +51,7 @@ defmodule Plausible.Google.GA4.HTTP do
with {:ok, %{body: body}} <- response,
{:ok, report} <- parse_report_from_response(body),
- row_count <- Map.fetch!(report, "rowCount"),
+ row_count <- Map.get(report, "rowCount", 0),
{:ok, report} <- convert_to_maps(report) do
{:ok, {report, row_count}}
else
@@ -130,6 +130,10 @@ defmodule Plausible.Google.GA4.HTTP do
{:ok, report}
end
+ defp convert_to_maps(%{"dimensionHeaders" => _, "metricHeaders" => _}) do
+ {:ok, []}
+ end
+
defp convert_to_maps(response) do
Logger.error(
"Google Analytics 4: Failed to read report in response. Reason: #{inspect(response)}"
diff --git a/test/plausible/imported/google_analytics4_test.exs b/test/plausible/imported/google_analytics4_test.exs
index d89912c1da..7bde5f0673 100644
--- a/test/plausible/imported/google_analytics4_test.exs
+++ b/test/plausible/imported/google_analytics4_test.exs
@@ -150,6 +150,87 @@ defmodule Plausible.Imported.GoogleAnalytics4Test do
assert_active_visitors(site_import)
end
+ test "handles empty response payload gracefully", %{user: user, site: site} do
+ future = DateTime.add(DateTime.utc_now(), 3600, :second)
+
+ empty_custom_events = %{
+ "reports" => [
+ %{
+ "dimensionHeaders" => [
+ %{"name" => "date"},
+ %{"name" => "eventName"},
+ %{"name" => "linkUrl"}
+ ],
+ "kind" => "analyticsData#runReport",
+ "metadata" => %{"currencyCode" => "USD", "timeZone" => "Etc/GMT"},
+ "metricHeaders" => [
+ %{"name" => "totalUsers", "type" => "TYPE_INTEGER"},
+ %{"name" => "eventCount", "type" => "TYPE_INTEGER"}
+ ]
+ }
+ ]
+ }
+
+ {:ok, job} =
+ Plausible.Imported.GoogleAnalytics4.new_import(
+ site,
+ user,
+ label: "properties/123456",
+ property: "properties/123456",
+ start_date: ~D[2024-01-01],
+ end_date: ~D[2024-01-31],
+ access_token: "redacted_access_token",
+ refresh_token: "redacted_refresh_token",
+ token_expires_at: DateTime.to_iso8601(future)
+ )
+
+ site_import = Plausible.Imported.get_import(site, job.args.import_id)
+
+ opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
+
+ opts = Keyword.put(opts, :flush_interval_ms, 10)
+
+ for report <- Enum.take(@full_report_mock, 4) do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
+ assert [{"Authorization", "Bearer redacted_access_token"}] == headers
+ {:ok, %Finch.Response{status: 200, body: report}}
+ end)
+ end
+
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
+ assert [{"Authorization", "Bearer redacted_access_token"}] == headers
+ {:ok, %Finch.Response{status: 200, body: empty_custom_events}}
+ end)
+
+ for report <- Enum.drop(@full_report_mock, 5) do
+ expect(Plausible.HTTPClient.Mock, :post, fn _url, headers, _body, _opts ->
+ assert [{"Authorization", "Bearer redacted_access_token"}] == headers
+ {:ok, %Finch.Response{status: 200, body: report}}
+ end)
+ end
+
+ assert :ok = GoogleAnalytics4.import_data(site_import, opts)
+
+ Enum.each(Plausible.Imported.tables(), fn table ->
+ count =
+ case table do
+ "imported_sources" -> 210
+ "imported_visitors" -> 31
+ "imported_pages" -> 3340
+ "imported_entry_pages" -> 2934
+ "imported_exit_pages" -> 0
+ "imported_locations" -> 2291
+ "imported_devices" -> 93
+ "imported_browsers" -> 233
+ "imported_operating_systems" -> 1068
+ "imported_custom_events" -> 0
+ end
+
+ query = from(imported in table, where: imported.site_id == ^site.id)
+ assert await_clickhouse_count(query, count)
+ end)
+ end
+
test "handles rate limiting gracefully", %{user: user, site: site} do
future = DateTime.add(DateTime.utc_now(), 3600, :second)