diff --git a/lib/plausible/imported/site.ex b/lib/plausible/imported/site.ex index d0bf41ac82..910ad3a14a 100644 --- a/lib/plausible/imported/site.ex +++ b/lib/plausible/imported/site.ex @@ -3,6 +3,8 @@ defmodule Plausible.Imported do use Timex require Logger + @missing_values ["(none)", "(not set)", "(not provided)", "(other)"] + @tables ~w( imported_visitors imported_sources imported_pages imported_entry_pages imported_exit_pages imported_locations imported_devices imported_browsers @@ -18,7 +20,13 @@ defmodule Plausible.Imported do def from_google_analytics(nil, _site_id, _metric), do: nil def from_google_analytics(data, site_id, table) do - Enum.map(data, fn row -> new_from_google_analytics(site_id, table, row) end) + Enum.reduce(data, [], fn row, acc -> + if Map.get(row.dimensions, "ga:date") in @missing_values do + acc + else + [new_from_google_analytics(site_id, table, row) | acc] + end + end) end defp parse_number(nr) do @@ -165,7 +173,6 @@ defmodule Plausible.Imported do |> NaiveDateTime.to_date() end - @missing_values ["(none)", "(not set)", "(not provided)"] defp default_if_missing(value, default \\ nil) defp default_if_missing(value, default) when value in @missing_values, do: default defp default_if_missing(value, _default), do: value diff --git a/test/plausible/imported/imported_test.exs b/test/plausible/imported/imported_test.exs index 3b7285ee79..68700af95f 100644 --- a/test/plausible/imported/imported_test.exs +++ b/test/plausible/imported/imported_test.exs @@ -941,5 +941,65 @@ defmodule Plausible.ImportedTest do assert visit_duration["value"] == 3_479_033 end + + test "skips empty dates from import", %{conn: conn, site: site} do + import_data( + [ + %{ + dimensions: %{"ga:date" => "20210101"}, + metrics: %{ + "ga:users" => "1", + "ga:pageviews" => "1", + "ga:bounces" => "0", + "ga:sessions" => "1", + "ga:sessionDuration" => "60" + } + }, + %{ + dimensions: %{"ga:date" => "(other)"}, + metrics: %{ + "ga:users" => "1", + "ga:pageviews" => "1", + "ga:bounces" => "0", + "ga:sessions" => "1", + "ga:sessionDuration" => "60" + } + } + ], + site.id, + "imported_visitors" + ) + + conn = + get( + conn, + "/api/stats/#{site.domain}/top-stats?period=month&date=2021-01-01&with_imported=true" + ) + + assert %{ + "top_stats" => [ + %{ + "change" => 100, + "name" => "Unique visitors", + "value" => 1 + }, + %{ + "change" => 100, + "name" => "Total pageviews", + "value" => 1 + }, + %{ + "change" => nil, + "name" => "Bounce rate", + "value" => 0 + }, + %{ + "change" => 100, + "name" => "Visit duration", + "value" => 60 + } + ] + } = json_response(conn, 200) + end end end