mirror of
https://github.com/plausible/analytics.git
synced 2024-09-11 18:07:33 +03:00
Add label to site imports and populate it (#3914)
This commit is contained in:
parent
1d017e86a1
commit
ba5b80a8c0
@ -155,7 +155,7 @@ defmodule Plausible.Imported.Importer do
|
||||
def new_import(source, site, user, opts, before_start_fun) do
|
||||
import_params =
|
||||
opts
|
||||
|> Keyword.take([:start_date, :end_date, :legacy])
|
||||
|> Keyword.take([:label, :start_date, :end_date, :legacy])
|
||||
|> Keyword.put(:source, source)
|
||||
|> Map.new()
|
||||
|
||||
|
@ -18,6 +18,7 @@ defmodule Plausible.Imported.SiteImport do
|
||||
schema "site_imports" do
|
||||
field :start_date, :date
|
||||
field :end_date, :date
|
||||
field :label, :string
|
||||
field :source, Ecto.Enum, values: ImportSources.names()
|
||||
field :status, Ecto.Enum, values: @statuses
|
||||
field :legacy, :boolean, default: false
|
||||
@ -32,11 +33,14 @@ defmodule Plausible.Imported.SiteImport do
|
||||
defmacro unquote(status)(), do: unquote(status)
|
||||
end
|
||||
|
||||
@spec label(t() | Site.ImportedData.t()) :: String.t()
|
||||
def label(%__MODULE__{source: source, label: label}) do
|
||||
build_label(ImportSources.by_name(source).label(), label)
|
||||
end
|
||||
|
||||
# NOTE: this is necessary for backwards compatibility
|
||||
# with legacy imports
|
||||
@spec label(t() | Site.ImportedData.t()) :: String.t()
|
||||
def label(%__MODULE__{source: source}), do: ImportSources.by_name(source).label()
|
||||
def label(%Site.ImportedData{source: source}), do: source
|
||||
def label(%Site.ImportedData{source: source}), do: build_label(source, nil)
|
||||
|
||||
@spec from_legacy(Site.ImportedData.t()) :: t()
|
||||
def from_legacy(%Site.ImportedData{} = data) do
|
||||
@ -60,7 +64,7 @@ defmodule Plausible.Imported.SiteImport do
|
||||
@spec create_changeset(Site.t(), User.t(), map()) :: Ecto.Changeset.t()
|
||||
def create_changeset(site, user, params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [:source, :start_date, :end_date, :legacy])
|
||||
|> cast(params, [:label, :source, :start_date, :end_date, :legacy])
|
||||
|> validate_required([:source])
|
||||
|> put_assoc(:site, site)
|
||||
|> put_assoc(:imported_by, user)
|
||||
@ -85,4 +89,7 @@ defmodule Plausible.Imported.SiteImport do
|
||||
def fail_changeset(site_import) do
|
||||
change(site_import, status: failed())
|
||||
end
|
||||
|
||||
defp build_label(source, nil), do: source
|
||||
defp build_label(source, label), do: "#{source} (#{label})"
|
||||
end
|
||||
|
@ -195,6 +195,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
|
||||
site,
|
||||
current_user,
|
||||
property: property_or_view,
|
||||
label: property_or_view,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
access_token: access_token,
|
||||
@ -206,6 +207,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
|
||||
site,
|
||||
current_user,
|
||||
view_id: property_or_view,
|
||||
label: property_or_view,
|
||||
start_date: start_date,
|
||||
end_date: end_date,
|
||||
access_token: access_token,
|
||||
|
@ -34,6 +34,7 @@ defmodule Plausible.Imported.GoogleAnalytics4Test do
|
||||
Plausible.Imported.GoogleAnalytics4.new_import(
|
||||
site,
|
||||
user,
|
||||
label: "properties/123456",
|
||||
property: "properties/123456",
|
||||
start_date: ~D[2024-02-20],
|
||||
end_date: Date.utc_today(),
|
||||
@ -44,6 +45,8 @@ defmodule Plausible.Imported.GoogleAnalytics4Test do
|
||||
|
||||
site_import = Plausible.Imported.get_import(job.args.import_id)
|
||||
|
||||
assert site_import.label == "properties/123456"
|
||||
|
||||
opts = job |> Repo.reload!() |> Map.get(:args) |> GoogleAnalytics4.parse_args()
|
||||
|
||||
opts = Keyword.put(opts, :flush_interval_ms, 10)
|
||||
|
@ -15,7 +15,8 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
|
||||
|
||||
assert {:ok, job} =
|
||||
UniversalAnalytics.new_import(site, user,
|
||||
view_id: 123,
|
||||
view_id: "123",
|
||||
label: "123",
|
||||
start_date: "2023-10-01",
|
||||
end_date: "2024-01-02",
|
||||
access_token: "access123",
|
||||
@ -28,7 +29,7 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
|
||||
args:
|
||||
%{
|
||||
"import_id" => import_id,
|
||||
"view_id" => 123,
|
||||
"view_id" => "123",
|
||||
"start_date" => "2023-10-01",
|
||||
"end_date" => "2024-01-02",
|
||||
"access_token" => "access123",
|
||||
@ -40,6 +41,7 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
|
||||
assert [
|
||||
%{
|
||||
id: ^import_id,
|
||||
label: "123",
|
||||
source: :universal_analytics,
|
||||
start_date: ~D[2023-10-01],
|
||||
end_date: ~D[2024-01-02],
|
||||
@ -59,7 +61,7 @@ defmodule Plausible.Imported.UniversalAnalyticsTest do
|
||||
|
||||
assert opts = [_ | _] = UniversalAnalytics.parse_args(args)
|
||||
|
||||
assert opts[:view_id] == 123
|
||||
assert opts[:view_id] == "123"
|
||||
assert opts[:date_range] == Date.range(~D[2023-10-01], ~D[2024-01-02])
|
||||
assert opts[:auth] == {"access123", "refresh123", expires_at}
|
||||
end
|
||||
|
@ -658,7 +658,10 @@ defmodule PlausibleWeb.SiteControllerTest do
|
||||
|
||||
_site_import1 = insert(:site_import, site: site, status: SiteImport.pending())
|
||||
_site_import2 = insert(:site_import, site: site, status: SiteImport.importing())
|
||||
site_import3 = insert(:site_import, site: site, status: SiteImport.completed())
|
||||
|
||||
site_import3 =
|
||||
insert(:site_import, label: "123456", site: site, status: SiteImport.completed())
|
||||
|
||||
_site_import4 = insert(:site_import, site: site, status: SiteImport.failed())
|
||||
|
||||
populate_stats(site, site_import3.id, [
|
||||
@ -672,6 +675,7 @@ defmodule PlausibleWeb.SiteControllerTest do
|
||||
buttons = find(resp, ~s|button[data-method="delete"]|)
|
||||
assert length(buttons) == 5
|
||||
|
||||
assert resp =~ "Google Analytics (123456)"
|
||||
assert resp =~ "(98 page views)"
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user