Skip to content

Commit

Permalink
Make final touches to Imports & Exports (#4025)
Browse files Browse the repository at this point in the history
* Make final touches to Imports & Exports

* Change import content copy depending on CSV imports and exports flag state

* Remove unused aliases
  • Loading branch information
zoldar committed Apr 19, 2024
1 parent c105807 commit fede2f0
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 711 deletions.
10 changes: 4 additions & 6 deletions lib/plausible/google/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
end

def fetch_access_token!(code) do
Expand Down
10 changes: 8 additions & 2 deletions lib/plausible/imported.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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: site
Expand Down
5 changes: 0 additions & 5 deletions lib/plausible_web/components/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ defmodule PlausibleWeb.Components.Settings do

import 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")
end
30 changes: 6 additions & 24 deletions lib/plausible_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -697,25 +697,15 @@ defmodule PlausibleWeb.AuthController do
end

def 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" do
Routes.site_path(conn, :settings_imports_exports, site.domain)
else
Routes.site_path(conn, :settings_integrations, site.domain)
end

case error do
Expand Down Expand Up @@ -750,14 +740,7 @@ defmodule PlausibleWeb.AuthController do
def google_auth_callback(conn, %{"code" => code, "state" => state}) do
res = 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"])
Expand All @@ -769,8 +752,7 @@ defmodule PlausibleWeb.AuthController do
Routes.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)
)
)

Expand Down
60 changes: 15 additions & 45 deletions lib/plausible_web/controllers/google_analytics_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) do
site = conn.assigns.site

Expand All @@ -31,7 +30,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
expires_at: expires_at,
start_date: start_date,
end_date: end_date,
legacy: legacy,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
end
Expand All @@ -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
) do
site = 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) do
Google.API.list_properties_and_views(access_token)
else
Google.UA.API.list_views(access_token)
end

error =
Expand All @@ -84,7 +76,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
site: conn.assigns.site,
properties_and_views: properties_and_views,
selected_property_or_view_error: error,
legacy: legacy,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)

Expand Down Expand Up @@ -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
) do
site = 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),
Expand All @@ -156,15 +141,14 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
refresh_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)
Expand Down Expand Up @@ -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
}) do
site = conn.assigns.site

start_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}} ->
Expand All @@ -230,7 +208,6 @@ defmodule PlausibleWeb.GoogleAnalyticsController do
start_date: start_date,
end_date: end_date,
property?: Google.API.property?(property_or_view),
legacy: legacy,
layout: {PlausibleWeb.LayoutView, "focus.html"}
)

Expand Down Expand Up @@ -274,30 +251,23 @@ 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
}) do
site = conn.assigns.site
current_user = conn.assigns.current_user

start_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,
start_date: start_date,
end_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),
Expand Down
30 changes: 7 additions & 23 deletions lib/plausible_web/controllers/site_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,11 @@ defmodule PlausibleWeb.SiteController do
Plausible.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",
Expand All @@ -278,18 +267,13 @@ defmodule PlausibleWeb.SiteController do
def settings_imports_exports(conn, _params) do
site = 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"}
)
end

def update_google_auth(conn, %{"google_auth" => attrs}) do
Expand Down
7 changes: 5 additions & 2 deletions lib/plausible_web/live/imports_exports_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
end

def render(assigns) do
Expand Down Expand Up @@ -79,7 +82,7 @@ defmodule PlausibleWeb.Live.ImportsExportsSettings do
class="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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
) %>
Expand Down
Loading

0 comments on commit fede2f0

Please sign in to comment.