Skip rows with empty dates from GA imports (#2359)

This commit is contained in:
Vinicius Brasil 2022-10-24 07:13:52 -03:00 committed by GitHub
parent 9a61a10273
commit e48851a9ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 2 deletions

View File

@ -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

View File

@ -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