Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Make final touches to Imports & Exports (#4025)
Adrian Gruntkowskifede2f0a8a84
diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.exindex 7088503194..3e863c2f82 100644--- a/lib/plausible/google/api.ex+++ b/lib/plausible/google/api.ex@@ -16,16 +16,14 @@ defmodule Plausible.Google.API do@verified_permission_levels ["siteOwner", "siteFullUser", "siteRestrictedUser"]- def search_console_authorize_url(site_id, redirect_to) do+ def search_console_authorize_url(site_id) do"https://accounts.google.com/o/oauth2/v2/auth?client_id=#{client_id()}&redirect_uri=#{redirect_uri()}&prompt=consent&response_type=code&access_type=offline&scope=#{@search_console_scope}&state=" <>- Jason.encode!([site_id, redirect_to])+ Jason.encode!([site_id, "search-console"])end- def import_authorize_url(site_id, redirect_to, opts \\ []) do- legacy = Keyword.get(opts, :legacy, true)-+ def import_authorize_url(site_id) do"https://accounts.google.com/o/oauth2/v2/auth?client_id=#{client_id()}&redirect_uri=#{redirect_uri()}&prompt=consent&response_type=code&access_type=offline&scope=#{@import_scope}&state=" <>- Jason.encode!([site_id, redirect_to, legacy])+ Jason.encode!([site_id, "import"])enddef fetch_access_token!(code) dodiff --git a/lib/plausible/imported.ex b/lib/plausible/imported.exindex c5f94e27a3..5e7bc1f905 100644--- a/lib/plausible/imported.ex+++ b/lib/plausible/imported.ex@@ -43,8 +43,14 @@ defmodule Plausible.Imported do@spec tables() :: [String.t()]def tables, do: @table_names- @spec max_complete_imports() :: non_neg_integer()- def max_complete_imports(), do: @max_complete_imports+ @spec max_complete_imports(Site.t()) :: non_neg_integer()+ def max_complete_imports(site) do+ if FunWithFlags.enabled?(:imports_exports, for: site) do+ @max_complete_imports+ else+ 1+ end+ end@spec load_import_data(Site.t()) :: Site.t()def load_import_data(%{import_data_loaded: true} = site), do: sitediff --git a/lib/plausible_web/components/settings.ex b/lib/plausible_web/components/settings.exindex ade376c1e0..ae89d4d44c 100644--- a/lib/plausible_web/components/settings.ex+++ b/lib/plausible_web/components/settings.ex@@ -7,10 +7,5 @@ defmodule PlausibleWeb.Components.Settings doimport PlausibleWeb.Components.Generic- alias Plausible.Imported.SiteImport-- require Plausible.Imported.SiteImport-embed_templates("../templates/site/settings_search_console.html")- embed_templates("../templates/site/settings_google_import.html")enddiff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.exindex 7844d5771f..1a526c09e1 100644--- a/lib/plausible_web/controllers/auth_controller.ex+++ b/lib/plausible_web/controllers/auth_controller.ex@@ -697,25 +697,15 @@ defmodule PlausibleWeb.AuthController doenddef google_auth_callback(conn, %{"error" => error, "state" => state} = params) do- [site_id, _redirected_to, legacy, _ga4] =- case Jason.decode!(state) do- [site_id, redirect_to] ->- [site_id, redirect_to, true, false]-- [site_id, redirect_to, legacy] ->- [site_id, redirect_to, legacy, false]-- [site_id, redirect_to, legacy, ga4] ->- [site_id, redirect_to, legacy, ga4]- end+ [site_id, redirected_to | _] = Jason.decode!(state)site = Repo.get(Plausible.Site, site_id)redirect_route =- if legacy do- Routes.site_path(conn, :settings_integrations, site.domain)- else+ if redirected_to == "import" doRoutes.site_path(conn, :settings_imports_exports, site.domain)+ else+ Routes.site_path(conn, :settings_integrations, site.domain)endcase error do@@ -750,14 +740,7 @@ defmodule PlausibleWeb.AuthController dodef google_auth_callback(conn, %{"code" => code, "state" => state}) dores = Plausible.Google.API.fetch_access_token!(code)- [site_id, redirect_to, legacy] =- case Jason.decode!(state) do- [site_id, redirect_to] ->- [site_id, redirect_to, true]-- [site_id, redirect_to, legacy] ->- [site_id, redirect_to, legacy]- end+ [site_id, redirect_to | _] = Jason.decode!(state)site = Repo.get(Plausible.Site, site_id)expires_at = NaiveDateTime.add(NaiveDateTime.utc_now(), res["expires_in"])@@ -769,8 +752,7 @@ defmodule PlausibleWeb.AuthController doRoutes.google_analytics_path(conn, :property_or_view_form, site.domain,access_token: res["access_token"],refresh_token: res["refresh_token"],- expires_at: NaiveDateTime.to_iso8601(expires_at),- legacy: legacy+ expires_at: NaiveDateTime.to_iso8601(expires_at)))diff --git a/lib/plausible_web/controllers/google_analytics_controller.ex b/lib/plausible_web/controllers/google_analytics_controller.exindex e98c63242c..b498e92635 100644--- a/lib/plausible_web/controllers/google_analytics_controller.ex+++ b/lib/plausible_web/controllers/google_analytics_controller.ex@@ -16,8 +16,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController do"refresh_token" => refresh_token,"expires_at" => expires_at,"start_date" => start_date,- "end_date" => end_date,- "legacy" => legacy+ "end_date" => end_date}) dosite = conn.assigns.site@@ -31,7 +30,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController doexpires_at: expires_at,start_date: start_date,end_date: end_date,- legacy: legacy,layout: {PlausibleWeb.LayoutView, "focus.html"})end@@ -41,24 +39,18 @@ defmodule PlausibleWeb.GoogleAnalyticsController do%{"access_token" => access_token,"refresh_token" => refresh_token,- "expires_at" => expires_at,- "legacy" => legacy+ "expires_at" => expires_at} = params) dosite = conn.assigns.site- redirect_route =- if legacy == "true" do- Routes.site_path(conn, :settings_integrations, site.domain)- else- Routes.site_path(conn, :settings_imports_exports, site.domain)- end+ redirect_route = Routes.site_path(conn, :settings_imports_exports, site.domain)result =- if legacy == "true" do- Google.UA.API.list_views(access_token)- else+ if FunWithFlags.enabled?(:imports_exports, for: site) doGoogle.API.list_properties_and_views(access_token)+ else+ Google.UA.API.list_views(access_token)enderror =@@ -84,7 +76,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController dosite: conn.assigns.site,properties_and_views: properties_and_views,selected_property_or_view_error: error,- legacy: legacy,layout: {PlausibleWeb.LayoutView, "focus.html"})@@ -123,18 +114,12 @@ defmodule PlausibleWeb.GoogleAnalyticsController do"property_or_view" => property_or_view,"access_token" => access_token,"refresh_token" => refresh_token,- "expires_at" => expires_at,- "legacy" => legacy+ "expires_at" => expires_at} = params) dosite = conn.assigns.site- redirect_route =- if legacy == "true" do- Routes.site_path(conn, :settings_integrations, site.domain)- else- Routes.site_path(conn, :settings_imports_exports, site.domain)- end+ redirect_route = Routes.site_path(conn, :settings_imports_exports, site.domain)with {:ok, api_start_date} <-Google.API.get_analytics_start_date(access_token, property_or_view),@@ -156,15 +141,14 @@ defmodule PlausibleWeb.GoogleAnalyticsController dorefresh_token: refresh_token,expires_at: expires_at,start_date: Date.to_iso8601(start_date),- end_date: Date.to_iso8601(end_date),- legacy: legacy+ end_date: Date.to_iso8601(end_date)))else{:error, error} when error in [:no_data, :no_time_window] ->params =params- |> Map.take(["access_token", "refresh_token", "expires_at", "legacy"])+ |> Map.take(["access_token", "refresh_token", "expires_at"])|> Map.put("error", Atom.to_string(error))property_or_view_form(conn, params)@@ -201,20 +185,14 @@ defmodule PlausibleWeb.GoogleAnalyticsController do"refresh_token" => refresh_token,"expires_at" => expires_at,"start_date" => start_date,- "end_date" => end_date,- "legacy" => legacy+ "end_date" => end_date}) dosite = conn.assigns.sitestart_date = Date.from_iso8601!(start_date)end_date = Date.from_iso8601!(end_date)- redirect_route =- if legacy == "true" do- Routes.site_path(conn, :settings_integrations, site.domain)- else- Routes.site_path(conn, :settings_imports_exports, site.domain)- end+ redirect_route = Routes.site_path(conn, :settings_imports_exports, site.domain)case Google.API.get_property_or_view(access_token, property_or_view) do{:ok, %{name: property_or_view_name, id: property_or_view}} ->@@ -230,7 +208,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController dostart_date: start_date,end_date: end_date,property?: Google.API.property?(property_or_view),- legacy: legacy,layout: {PlausibleWeb.LayoutView, "focus.html"})@@ -274,8 +251,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController do"end_date" => end_date,"access_token" => access_token,"refresh_token" => refresh_token,- "expires_at" => expires_at,- "legacy" => legacy+ "expires_at" => expires_at}) dosite = conn.assigns.sitecurrent_user = conn.assigns.current_user@@ -283,12 +259,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController dostart_date = Date.from_iso8601!(start_date)end_date = Date.from_iso8601!(end_date)- redirect_route =- if legacy == "true" do- Routes.site_path(conn, :settings_integrations, site.domain)- else- Routes.site_path(conn, :settings_imports_exports, site.domain)- end+ redirect_route = Routes.site_path(conn, :settings_imports_exports, site.domain)import_opts = [label: property_or_view,@@ -296,8 +267,7 @@ defmodule PlausibleWeb.GoogleAnalyticsController doend_date: end_date,access_token: access_token,refresh_token: refresh_token,- token_expires_at: expires_at,- legacy: legacy == "true"+ token_expires_at: expires_at]with {:ok, start_date, end_date} <- Imported.clamp_dates(site, start_date, end_date),diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex d71e9359a3..00e093bb9a 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -237,22 +237,11 @@ defmodule PlausibleWeb.SiteController doPlausible.Google.API.fetch_verified_properties(site.google_auth)end- legacy_import = Plausible.Imported.get_legacy_import(site)-- imported_pageviews =- if legacy_import do- Plausible.Stats.Clickhouse.imported_pageview_count(site)- else- 0- end-has_plugins_tokens? = Plausible.Plugins.API.Tokens.any?(site)conn|> render("settings_integrations.html",site: site,- legacy_import: legacy_import,- imported_pageviews: imported_pageviews,has_plugins_tokens?: has_plugins_tokens?,search_console_domains: search_console_domains,dogfood_page_path: "/:dashboard/settings/integrations",@@ -278,18 +267,13 @@ defmodule PlausibleWeb.SiteController dodef settings_imports_exports(conn, _params) dosite = conn.assigns.site- if FunWithFlags.enabled?(:imports_exports, for: site) do- conn- |> render("settings_imports_exports.html",- site: site,- dogfood_page_path: "/:dashboard/settings/imports-exports",- connect_live_socket: true,- layout: {PlausibleWeb.LayoutView, "site_settings.html"}- )- else- conn- |> redirect(external: Routes.site_path(conn, :settings, site.domain))- end+ conn+ |> render("settings_imports_exports.html",+ site: site,+ dogfood_page_path: "/:dashboard/settings/imports-exports",+ connect_live_socket: true,+ layout: {PlausibleWeb.LayoutView, "site_settings.html"}+ )enddef update_google_auth(conn, %{"google_auth" => attrs}) dodiff --git a/lib/plausible_web/live/imports_exports_settings.ex b/lib/plausible_web/live/imports_exports_settings.exindex c1b83ed721..548ea7edf6 100644--- a/lib/plausible_web/live/imports_exports_settings.ex+++ b/lib/plausible_web/live/imports_exports_settings.ex@@ -35,10 +35,13 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do|> assign_new(:current_user, fn ->Plausible.Repo.get(Plausible.Auth.User, user_id)end)+ |> assign_new(:max_imports, fn %{site: site} ->+ Imported.max_complete_imports(site)+ end):ok = Imported.listen()- {:ok, assign(socket, max_imports: Imported.max_complete_imports())}+ {:ok, socket}enddef render(assigns) do@@ -79,7 +82,7 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings doclass="w-36 h-20"theme="bright"disabled={@import_in_progress? or @at_maximum?}- href={Plausible.Google.API.import_authorize_url(@site.id, "import", legacy: false)}+ href={Plausible.Google.API.import_authorize_url(@site.id)}><img src="/images/icon/google_analytics_logo.svg" alt="Google Analytics import" /></.button_link>diff --git a/lib/plausible_web/templates/google_analytics/confirm.html.heex b/lib/plausible_web/templates/google_analytics/confirm.html.heexindex c3d37d8db0..f8f2eb4b83 100644--- a/lib/plausible_web/templates/google_analytics/confirm.html.heex+++ b/lib/plausible_web/templates/google_analytics/confirm.html.heex@@ -4,7 +4,6 @@<%= hidden_input(f, :access_token, value: @access_token) %><%= hidden_input(f, :refresh_token, value: @refresh_token) %><%= hidden_input(f, :expires_at, value: @expires_at) %>- <%= hidden_input(f, :legacy, value: @legacy) %><div class="mt-6 text-sm text-gray-500 dark:text-gray-200">Stats from this@@ -56,8 +55,7 @@property_or_view: @selected_property_or_view,access_token: @access_token,refresh_token: @refresh_token,- expires_at: @expires_at,- legacy: @legacy+ expires_at: @expires_at)}class="underline text-indigo-600"diff --git a/lib/plausible_web/templates/google_analytics/property_or_view_form.html.heex b/lib/plausible_web/templates/google_analytics/property_or_view_form.html.heexindex 775355947f..dd20748ff4 100644--- a/lib/plausible_web/templates/google_analytics/property_or_view_form.html.heex+++ b/lib/plausible_web/templates/google_analytics/property_or_view_form.html.heex@@ -4,7 +4,6 @@<%= hidden_input(f, :access_token, value: @access_token) %><%= hidden_input(f, :refresh_token, value: @refresh_token) %><%= hidden_input(f, :expires_at, value: @expires_at) %>- <%= hidden_input(f, :legacy, value: @legacy) %><div class="mt-6 text-sm text-gray-500 dark:text-gray-200">Choose the property or view in your Google Analytics account that will be imported to the <%= @site.domain %> dashboard.diff --git a/lib/plausible_web/templates/google_analytics/user_metric_form.html.heex b/lib/plausible_web/templates/google_analytics/user_metric_form.html.heexindex 6a969b3328..0f65b725ad 100644--- a/lib/plausible_web/templates/google_analytics/user_metric_form.html.heex+++ b/lib/plausible_web/templates/google_analytics/user_metric_form.html.heex@@ -40,8 +40,7 @@property_or_view: @property_or_view,access_token: @access_token,refresh_token: @refresh_token,- expires_at: @expires_at,- legacy: @legacy+ expires_at: @expires_at)}class="underline text-indigo-600"@@ -58,8 +57,7 @@refresh_token: @refresh_token,expires_at: @expires_at,start_date: @start_date,- end_date: @end_date,- legacy: @legacy+ end_date: @end_date),class: "button sm:w-auto w-full") %>diff --git a/lib/plausible_web/templates/site/settings_google_import.html.heex b/lib/plausible_web/templates/site/settings_google_import.html.heexdeleted file mode 100644index bb6d2c4d6b..0000000000--- a/lib/plausible_web/templates/site/settings_google_import.html.heex+++ /dev/null@@ -1,124 +0,0 @@-<div class="shadow bg-white dark:bg-gray-800 sm:rounded-md sm:overflow-hidden py-6 px-4 sm:p-6">- <header class="relative border-b border-gray-200 pb-4">- <h2 class="text-lg leading-6 font-medium text-gray-900 dark:text-gray-100">- Google Analytics Data Import- </h2>- <p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">- Import existing data from your Google Analytics account.- </p>- <PlausibleWeb.Components.Generic.docs_info slug="google-analytics-import" />- </header>-- <%= if Keyword.get(Application.get_env(:plausible, :google), :client_id) do %>- <%= cond do %>- <% @legacy_import && @legacy_import.status in [SiteImport.pending(), SiteImport.importing()] -> %>- <li class="py-4 flex items-center justify-between space-x-4">- <div class="flex flex-col">- <p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">- Import from Google Analytics- <svg- class="animate-spin -mr-1 ml-1 h-4 w-4 inline text-indigo-600"- xmlns="http://www.w3.org/2000/svg"- fill="none"- viewBox="0 0 24 24"- >- <circle- class="opacity-25"- cx="12"- cy="12"- r="10"- stroke="currentColor"- stroke-width="4"- >- </circle>- <path- class="opacity-75"- fill="currentColor"- d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"- >- </path>- </svg>- </p>- <p class="text-sm leading-5 text-gray-500 dark:text-gray-200">- From <%= PlausibleWeb.EmailView.date_format(@legacy_import.start_date) %> to <%= PlausibleWeb.EmailView.date_format(- @legacy_import.end_date- ) %>- </p>- </div>- <%= link("Cancel import",- to: "/#{URI.encode_www_form(@site.domain)}/settings/forget-imported",- method: :delete,- class:- "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150"- ) %>- </li>- <% @legacy_import && @legacy_import.status == SiteImport.completed() -> %>- <li class="py-4 flex items-center justify-between space-x-4">- <div class="flex flex-col">- <p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">- Import from Google Analytics- <svg- class="h-4 w-4 inline ml-1 -mt-1 text-green-600"- xmlns="http://www.w3.org/2000/svg"- fill="none"- viewBox="0 0 24 24"- stroke="currentColor"- stroke-width="2"- >- <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />- </svg>- </p>- <p class="text-sm leading-5 text-gray-500 dark:text-gray-200">- From <%= PlausibleWeb.EmailView.date_format(@legacy_import.start_date) %> to <%= PlausibleWeb.EmailView.date_format(- @legacy_import.end_date- ) %>- </p>- </div>- <%= link(- "Clear " <>- PlausibleWeb.StatsView.large_number_format(@imported_pageviews) <>- " Imported Pageviews",- to: "/#{URI.encode_www_form(@site.domain)}/settings/forget-imported",- method: :delete,- class:- "inline-block mt-4 px-4 py-2 text-sm leading-5 font-medium text-red-600 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150"- ) %>- </li>- <% true -> %>- <%= if @legacy_import && @legacy_import.status == SiteImport.failed() do %>- <div class="text-sm mt-2 text-gray-900 dark:text-gray-100">- Your latest import has failed. You can try importing again by clicking the button below. If you try multiple times and the import keeps failing, please contact support.- </div>- <% end %>- <PlausibleWeb.Components.Google.button- id="analytics-connect"- to={Plausible.Google.API.import_authorize_url(@site.id, "import", legacy: true)}- />- <% end %>- <% else %>- <div class="my-8 text-center text-lg">- <svg- class="block mx-auto mb-4 w-6 h-6 text-yellow-500"- fill="none"- stroke="currentColor"- viewBox="0 0 24 24"- xmlns="http://www.w3.org/2000/svg"- >- <path- stroke-linecap="round"- stroke-linejoin="round"- stroke-width="2"- d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"- >- </path>- </svg>- <p class="text-gray-900 dark:text-gray-200">- An extra step is needed to set up your Plausible Analytics Self Hosted for the Google Search Console integration.- Find instructions <%= link("here",- to: "https://plausible.io/docs/self-hosting-configuration#google-search-integration",- class: "text-indigo-500"- ) %>- </p>- </div>- <% end %>-</div>diff --git a/lib/plausible_web/templates/site/settings_imports_exports.html.heex b/lib/plausible_web/templates/site/settings_imports_exports.html.heexindex 439c0d0657..983d88ccc5 100644--- a/lib/plausible_web/templates/site/settings_imports_exports.html.heex+++ b/lib/plausible_web/templates/site/settings_imports_exports.html.heex@@ -4,7 +4,12 @@Import Data</h2><p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">- Import existing data from external sources. Pick one of the options below to start a new import.+ Import existing data from external sources.+ <%= if FunWithFlags.enabled?(:csv_imports_exports, for: @site) do %>+ Pick one of the options below to start a new import.+ <% else %>+ Click below to start a new import.+ <% end %></p><PlausibleWeb.Components.Generic.docs_info slug="google-analytics-import" />diff --git a/lib/plausible_web/templates/site/settings_integrations.html.heex b/lib/plausible_web/templates/site/settings_integrations.html.heexindex bc10f7eef9..79ac2220e8 100644--- a/lib/plausible_web/templates/site/settings_integrations.html.heex+++ b/lib/plausible_web/templates/site/settings_integrations.html.heex@@ -3,14 +3,6 @@search_console_domains={@search_console_domains}/>-<%= if not FunWithFlags.enabled?(:imports_exports, for: @site) do %>- <PlausibleWeb.Components.Settings.settings_google_import- site={@site}- imported_pageviews={@imported_pageviews}- legacy_import={@legacy_import}- />-<% end %>-<section:if={@has_plugins_tokens? || @conn.query_params["new_token"]}class="shadow bg-white dark:bg-gray-800 sm:rounded-md sm:overflow-hidden"diff --git a/lib/plausible_web/templates/site/settings_search_console.html.heex b/lib/plausible_web/templates/site/settings_search_console.html.heexindex c615fab3aa..480da73282 100644--- a/lib/plausible_web/templates/site/settings_search_console.html.heex+++ b/lib/plausible_web/templates/site/settings_search_console.html.heex@@ -82,7 +82,7 @@<% else %><PlausibleWeb.Components.Google.buttonid="search-console-connect"- to={Plausible.Google.API.search_console_authorize_url(@site.id, "search-console")}+ to={Plausible.Google.API.search_console_authorize_url(@site.id)}/><div class="text-gray-700 dark:text-gray-300 mt-8">NB: You also need to set up your site ondiff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.exindex ca93c07eae..bc4c52d357 100644--- a/lib/plausible_web/views/layout_view.ex+++ b/lib/plausible_web/views/layout_view.ex@@ -49,6 +49,13 @@ defmodule PlausibleWeb.LayoutView doenddef settings_tabs(conn) do+ imports_exports_key =+ if FunWithFlags.enabled?(:csv_imports_exports, for: conn.assigns.site) do+ "Imports & Exports"+ else+ "Imports"+ end+[%{key: "General", value: "general", icon: :rocket_launch},%{key: "People", value: "people", icon: :users},@@ -59,9 +66,7 @@ defmodule PlausibleWeb.LayoutView doend,%{key: "Custom Properties", value: "properties", icon: :document_text},%{key: "Integrations", value: "integrations", icon: :arrow_path_rounded_square},- if FunWithFlags.enabled?(:imports_exports, for: conn.assigns.site) do- %{key: "Imports & Exports", value: "imports-exports", icon: :arrows_up_down}- end,+ %{key: imports_exports_key, value: "imports-exports", icon: :arrows_up_down},%{key: "Shields",icon: :shield_exclamation,diff --git a/lib/plausible_web/views/site_view.ex b/lib/plausible_web/views/site_view.exindex b394dbf4c0..a3ae89f776 100644--- a/lib/plausible_web/views/site_view.ex+++ b/lib/plausible_web/views/site_view.ex@@ -2,10 +2,6 @@ defmodule PlausibleWeb.SiteView douse PlausibleWeb, :viewuse Plausible- alias Plausible.Imported.SiteImport-- require Plausible.Imported.SiteImport-def plausible_url doPlausibleWeb.Endpoint.url()enddiff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exsindex 461ccf2e1d..c55356b186 100644--- a/test/plausible_web/controllers/auth_controller_test.exs+++ b/test/plausible_web/controllers/auth_controller_test.exs@@ -1484,7 +1484,7 @@ defmodule PlausibleWeb.AuthControllerTest doconn = get(conn, Routes.auth_path(conn, :google_auth_callback), callback_params)assert redirected_to(conn, 302) ==- Routes.site_path(conn, :settings_integrations, site.domain)+ Routes.site_path(conn, :settings_imports_exports, site.domain)assert Phoenix.Flash.get(conn.assigns.flash, :error) =~"unable to authenticate your Google Analytics"diff --git a/test/plausible_web/controllers/google_analytics_controller_test.exs b/test/plausible_web/controllers/google_analytics_controller_test.exsindex ccaccf946d..770e13484c 100644--- a/test/plausible_web/controllers/google_analytics_controller_test.exs+++ b/test/plausible_web/controllers/google_analytics_controller_test.exs@@ -24,8 +24,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"refresh_token" => "foo","expires_at" => "2022-09-22T20:01:37.112777","start_date" => "2020-02-22",- "end_date" => "2022-09-22",- "legacy" => "true"+ "end_date" => "2022-09-22"})|> html_response(200)@@ -36,8 +35,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest dorefresh_token: "foo",expires_at: "2022-09-22T20:01:37.112777",start_date: "2020-02-22",- end_date: "2022-09-22",- legacy: "true"+ end_date: "2022-09-22")|> String.replace("&", "&")end@@ -46,7 +44,16 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest dodescribe "GET /:website/import/google-analytics/property-or-view" dosetup [:create_user, :log_in, :create_new_site]- test "lists Google Analytics views (legacy)", %{conn: conn, site: site} do+ test "lists Google Analytics views and properties", %{conn: conn, site: site} do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn _url, _opts ->+ body = "fixture/ga4_list_properties.json" |> File.read!() |> Jason.decode!()+ {:ok, %Finch.Response{body: body, status: 200}}+ end+ )+expect(Plausible.HTTPClient.Mock,:get,@@ -61,221 +68,177 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do|> get("/#{site.domain}/import/google-analytics/property-or-view", %{"access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "true"+ "expires_at" => "2022-09-22T20:01:37.112777"})|> html_response(200)assert response =~ "57238190 - one.test"assert response =~ "54460083 - two.test"+ assert response =~ "account.one - GA4 (properties/428685906)"+ assert response =~ "GA4 - Flood-It! (properties/153293282)"+ assert response =~ "GA4 - Google Merch Shop (properties/213025502)"end- test "lists Google Analytics views and properties", %{conn: conn, site: site} do+ test "redirects to imports and exports on auth error with flash error", %{+ conn: conn,+ site: site+ } doexpect(Plausible.HTTPClient.Mock,:get,fn _url, _opts ->- body = "fixture/ga4_list_properties.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}+ {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: %{}}}}end)+ conn =+ conn+ |> get("/#{site.domain}/import/google-analytics/property-or-view", %{+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })++ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "We were unable to authenticate your Google Analytics account"+ end++ test "redirects to imports and exports on timeout error with flash error", %{+ conn: conn,+ site: site+ } doexpect(Plausible.HTTPClient.Mock,:get,fn _url, _opts ->- body = "fixture/ga_list_views.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}+ {:error, %Mint.TransportError{reason: :timeout}}end)- response =+ conn =conn|> get("/#{site.domain}/import/google-analytics/property-or-view", %{"access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})- |> html_response(200)- assert response =~ "57238190 - one.test"- assert response =~ "54460083 - two.test"- assert response =~ "account.one - GA4 (properties/428685906)"- assert response =~ "GA4 - Flood-It! (properties/153293282)"- assert response =~ "GA4 - Google Merch Shop (properties/213025502)"+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google Analytics API has timed out."end- for {legacy, view} <- [{"true", :settings_integrations}, {"false", :settings_imports_exports}] do- test "redirects to #{view} on auth error with flash error (legacy: #{legacy})", %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _opts ->- {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: %{}}}}- end- )+ test "redirects to imports and exports on list retrival failure with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn _url, _opts ->+ {:error, %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}+ end+ )- conn =- conn- |> get("/#{site.domain}/import/google-analytics/property-or-view", %{- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "We were unable to authenticate your Google Analytics account"- end-- test "redirects to #{view} on timeout error with flash error (legacy: #{legacy})", %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _opts ->- {:error, %Mint.TransportError{reason: :timeout}}- end- )+ conn =+ conn+ |> get("/#{site.domain}/import/google-analytics/property-or-view", %{+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })- conn =- conn- |> get("/#{site.domain}/import/google-analytics/property-or-view", %{- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Google Analytics API has timed out."- end-- test "redirects to #{view} on list retrival failure with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _opts ->- {:error,- %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}- end- )+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )- conn =- conn- |> get("/#{site.domain}/import/google-analytics/property-or-view", %{- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "We were unable to list your Google Analytics properties and views"- end+ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "We were unable to list your Google Analytics properties and views"endenddescribe "POST /:website/import/google-analytics/property-or-view" dosetup [:create_user, :log_in, :create_new_site]- for legacy <- ["true", "false"] do- test "redirects to user metrics notice (UA legacy: #{legacy})", %{conn: conn, site: site} do- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- body = "fixture/ga_start_date.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}- end- )+ test "redirects to user metrics notice", %{conn: conn, site: site} do+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ body = "fixture/ga_start_date.json" |> File.read!() |> Jason.decode!()+ {:ok, %Finch.Response{body: body, status: 200}}+ end+ )- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- body = "fixture/ga_end_date.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}- end- )+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ body = "fixture/ga_end_date.json" |> File.read!() |> Jason.decode!()+ {:ok, %Finch.Response{body: body, status: 200}}+ end+ )- conn =- conn- |> post("/#{site.domain}/import/google-analytics/property-or-view", %{- "property_or_view" => "57238190",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) =~- "/#{URI.encode_www_form(site.domain)}/import/google-analytics/user-metric"- end-- test "redirects to confirmation (UA legacy: #{legacy})", %{conn: conn, site: site} do- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- body = "fixture/ga_start_date_later.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}- end- )+ conn =+ conn+ |> post("/#{site.domain}/import/google-analytics/property-or-view", %{+ "property_or_view" => "57238190",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- body = "fixture/ga_end_date.json" |> File.read!() |> Jason.decode!()- {:ok, %Finch.Response{body: body, status: 200}}- end- )+ assert redirected_to(conn, 302) =~+ "/#{URI.encode_www_form(site.domain)}/import/google-analytics/user-metric"+ end++ test "redirects to confirmation", %{conn: conn, site: site} do+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ body = "fixture/ga_start_date_later.json" |> File.read!() |> Jason.decode!()+ {:ok, %Finch.Response{body: body, status: 200}}+ end+ )- conn =- conn- |> post("/#{site.domain}/import/google-analytics/property-or-view", %{- "property_or_view" => "57238190",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) =~- "/#{URI.encode_www_form(site.domain)}/import/google-analytics/confirm"- end+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ body = "fixture/ga_end_date.json" |> File.read!() |> Jason.decode!()+ {:ok, %Finch.Response{body: body, status: 200}}+ end+ )++ conn =+ conn+ |> post("/#{site.domain}/import/google-analytics/property-or-view", %{+ "property_or_view" => "57238190",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })++ assert redirected_to(conn, 302) =~+ "/#{URI.encode_www_form(site.domain)}/import/google-analytics/confirm"endtest "redirects to confirmation (GA4)", %{conn: conn, site: site} do@@ -303,8 +266,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"property_or_view" => "properties/428685906","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert redirected_to(conn, 302) =~@@ -365,8 +327,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"property_or_view" => "properties/428685906","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})|> html_response(200)@@ -415,117 +376,110 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"property_or_view" => "properties/428685906","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})|> html_response(200)assert response =~ "No data found. Nothing to import."end- for {legacy, view} <- [{"true", :settings_integrations}, {"false", :settings_imports_exports}] do- test "redirects to #{view} on failed property/view choice with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- {:error,- %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}- end- )+ test "redirects to imports and exports on failed property/view choice with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ {:error, %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}+ end+ )- conn =- conn- |> post("/#{site.domain}/import/google-analytics/property-or-view", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "We were unable to retrieve information from Google Analytics"- end-- test "redirects to #{view} on expired authentication with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: "Access denied"}}}- end- )+ conn =+ conn+ |> post("/#{site.domain}/import/google-analytics/property-or-view", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })- conn =- conn- |> post("/#{site.domain}/import/google-analytics/property-or-view", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Google Analytics authentication seems to have expired."- end-- test "redirects to #{view} on timeout with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :post,- fn _url, _opts, _params ->- {:error, %Mint.TransportError{reason: :timeout}}- end- )+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "We were unable to retrieve information from Google Analytics"+ end++ test "redirects to imports and exports on expired authentication with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: "Access denied"}}}+ end+ )++ conn =+ conn+ |> post("/#{site.domain}/import/google-analytics/property-or-view", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })- conn =- conn- |> post("/#{site.domain}/import/google-analytics/property-or-view", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Google Analytics API has timed out."- end+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google Analytics authentication seems to have expired."+ end++ test "redirects to imports and exports on timeout with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :post,+ fn _url, _opts, _params ->+ {:error, %Mint.TransportError{reason: :timeout}}+ end+ )++ conn =+ conn+ |> post("/#{site.domain}/import/google-analytics/property-or-view", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })++ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google Analytics API has timed out."endend@@ -550,8 +504,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"refresh_token" => "foo","expires_at" => "2022-09-22T20:01:37.112777","start_date" => "2012-01-18",- "end_date" => "2022-09-22",- "legacy" => "true"+ "end_date" => "2022-09-22"})|> html_response(200)@@ -565,8 +518,6 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest doassert text_of_attr(response, ~s|input[name=expires_at]|, "value") =="2022-09-22T20:01:37.112777"- assert text_of_attr(response, ~s|input[name=legacy]|, "value") == "true"-assert text_of_attr(response, ~s|input[name=property_or_view]|, "value") == "57238190"assert text_of_attr(response, ~s|input[name=start_date]|, "value") == "2012-01-18"@@ -592,8 +543,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"refresh_token" => "foo","expires_at" => "2022-09-22T20:01:37.112777","start_date" => "2024-02-22",- "end_date" => "2024-02-26",- "legacy" => "true"+ "end_date" => "2024-02-26"})|> html_response(200)@@ -607,8 +557,6 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest doassert text_of_attr(response, ~s|input[name=expires_at]|, "value") =="2022-09-22T20:01:37.112777"- assert text_of_attr(response, ~s|input[name=legacy]|, "value") == "true"-assert text_of_attr(response, ~s|input[name=property_or_view]|, "value") =="properties/428685444"@@ -617,115 +565,109 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest doassert text_of_attr(response, ~s|input[name=end_date]|, "value") == "2024-02-26"end- for {legacy, view} <- [{"true", :settings_integrations}, {"false", :settings_imports_exports}] do- test "redirects to #{view} on failed property/view retrieval with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _params ->- {:error,- %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}- end- )+ test "redirects to imports and exports on failed property/view retrieval with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn _url, _params ->+ {:error, %HTTPClient.Non200Error{reason: %{status: 500, body: "Internal server error"}}}+ end+ )- conn =- conn- |> get("/#{site.domain}/import/google-analytics/confirm", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "start_date" => "2024-02-22",- "end_date" => "2024-02-26",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "We were unable to retrieve information from Google Analytics"- end-- test "redirects to #{view} on expired authentication with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _params ->- {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: "Access denied"}}}- end- )+ conn =+ conn+ |> get("/#{site.domain}/import/google-analytics/confirm", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777",+ "start_date" => "2024-02-22",+ "end_date" => "2024-02-26"+ })- conn =- conn- |> get("/#{site.domain}/import/google-analytics/confirm", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "start_date" => "2024-02-22",- "end_date" => "2024-02-26",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Google Analytics authentication seems to have expired."- end-- test "redirects to #{view} on timeout with flash error (legacy: #{legacy})",- %{- conn: conn,- site: site- } do- expect(- Plausible.HTTPClient.Mock,- :get,- fn _url, _params ->- {:error, %Mint.TransportError{reason: :timeout}}- end- )+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "We were unable to retrieve information from Google Analytics"+ end++ test "redirects to imports and exports on expired authentication with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn _url, _params ->+ {:error, %HTTPClient.Non200Error{reason: %{status: 403, body: "Access denied"}}}+ end+ )++ conn =+ conn+ |> get("/#{site.domain}/import/google-analytics/confirm", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777",+ "start_date" => "2024-02-22",+ "end_date" => "2024-02-26"+ })- conn =- conn- |> get("/#{site.domain}/import/google-analytics/confirm", %{- "property_or_view" => "properties/428685906",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "start_date" => "2024-02-22",- "end_date" => "2024-02-26",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Google Analytics API has timed out."- end+ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google Analytics authentication seems to have expired."+ end++ test "redirects to imports and exports on timeout with flash error",+ %{+ conn: conn,+ site: site+ } do+ expect(+ Plausible.HTTPClient.Mock,+ :get,+ fn _url, _params ->+ {:error, %Mint.TransportError{reason: :timeout}}+ end+ )++ conn =+ conn+ |> get("/#{site.domain}/import/google-analytics/confirm", %{+ "property_or_view" => "properties/428685906",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777",+ "start_date" => "2024-02-22",+ "end_date" => "2024-02-26"+ })++ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Google Analytics API has timed out."endend@@ -740,8 +682,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert redirected_to(conn, 302) ==@@ -762,12 +703,11 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "true"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(conn, :settings_integrations, site.domain)+ PlausibleWeb.Router.Helpers.site_path(conn, :settings_imports_exports, site.domain)[site_import] = Plausible.Imported.list_all_imports(site)@@ -776,7 +716,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest doassert site_import.status == SiteImport.pending()end- test "redirects to imports and exports when creating UA job with legacy set to false", %{+ test "redirects to imports and exports when creating UA job", %{conn: conn,site: site} do@@ -787,8 +727,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert redirected_to(conn, 302) ==@@ -808,8 +747,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert [%{id: import_id, legacy: false}] = Plausible.Imported.list_all_imports(site)@@ -835,11 +773,10 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "true"+ "expires_at" => "2022-09-22T20:01:37.112777"})- assert [%{id: import_id, legacy: true}] = Plausible.Imported.list_all_imports(site)+ assert [%{id: import_id, legacy: false}] = Plausible.Imported.list_all_imports(site)assert_enqueued(worker: Plausible.Workers.ImportAnalytics,@@ -872,8 +809,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "false"+ "expires_at" => "2022-09-22T20:01:37.112777"})conn =@@ -883,8 +819,7 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest do"end_date" => "2022-03-01","access_token" => "token","refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => "true"+ "expires_at" => "2022-09-22T20:01:37.112777"})assert Phoenix.Flash.get(conn.assigns.flash, :error) =~@@ -895,43 +830,40 @@ defmodule PlausibleWeb.GoogleAnalyticsControllerTest doassert job.args.import_id == import_idend- for {legacy, view} <- [{"true", :settings_integrations}, {"false", :settings_imports_exports}] do- test "redirects to #{view} with no time window error flash error (legacy: #{legacy})", %{- conn: conn,- site: site- } do- start_date = ~D[2022-01-12]- end_date = ~D[2024-03-13]-- _existing_import =- insert(:site_import,- site: site,- start_date: start_date,- end_date: end_date,- status: :completed- )-- conn =- post(conn, "/#{site.domain}/settings/google-import", %{- "property_or_view" => "123456",- "start_date" => "2023-03-01",- "end_date" => "2022-03-01",- "access_token" => "token",- "refresh_token" => "foo",- "expires_at" => "2022-09-22T20:01:37.112777",- "legacy" => unquote(legacy)- })-- assert redirected_to(conn, 302) ==- PlausibleWeb.Router.Helpers.site_path(- conn,- unquote(view),- site.domain- )-- assert Phoenix.Flash.get(conn.assigns.flash, :error) =~- "Import failed. No data could be imported because date range overlaps with existing data."- end+ test "redirects to imports and exports with no time window error flash error", %{+ conn: conn,+ site: site+ } do+ start_date = ~D[2022-01-12]+ end_date = ~D[2024-03-13]++ _existing_import =+ insert(:site_import,+ site: site,+ start_date: start_date,+ end_date: end_date,+ status: :completed+ )++ conn =+ post(conn, "/#{site.domain}/settings/google-import", %{+ "property_or_view" => "123456",+ "start_date" => "2023-03-01",+ "end_date" => "2022-03-01",+ "access_token" => "token",+ "refresh_token" => "foo",+ "expires_at" => "2022-09-22T20:01:37.112777"+ })++ assert redirected_to(conn, 302) ==+ PlausibleWeb.Router.Helpers.site_path(+ conn,+ :settings_imports_exports,+ site.domain+ )++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Import failed. No data could be imported because date range overlaps with existing data."endendenddiff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exsindex d6a876ff27..52942c8236 100644--- a/test/plausible_web/controllers/site_controller_test.exs+++ b/test/plausible_web/controllers/site_controller_test.exs@@ -679,7 +679,7 @@ defmodule PlausibleWeb.SiteControllerTest doendtest "disables import buttons when imports are at maximum", %{conn: conn, site: site} do- insert_list(Plausible.Imported.max_complete_imports(), :site_import,+ insert_list(Plausible.Imported.max_complete_imports(site), :site_import,site: site,status: SiteImport.completed())@@ -687,7 +687,7 @@ defmodule PlausibleWeb.SiteControllerTest doconn = get(conn, "/#{site.domain}/settings/imports-exports")assert html_response(conn, 200) =~- "Maximum of #{Plausible.Imported.max_complete_imports()} imports is reached."+ "Maximum of #{Plausible.Imported.max_complete_imports(site)} imports is reached."endtest "considers older legacy imports when showing pageview count", %{conn: conn, site: site} do