diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.ex index 9420fa56a..3313225e0 100644 --- a/lib/plausible/google/api.ex +++ b/lib/plausible/google/api.ex @@ -26,19 +26,23 @@ defmodule Plausible.Google.Api do end def fetch_verified_properties(auth) do - auth = refresh_if_needed(auth) - - res = - HTTPoison.get!("https://www.googleapis.com/webmasters/v3/sites", - "Content-Type": "application/json", - Authorization: "Bearer #{auth.access_token}" - ) - - Jason.decode!(res.body) - |> Map.get("siteEntry", []) - |> Enum.filter(fn site -> site["permissionLevel"] in @verified_permission_levels end) - |> Enum.map(fn site -> site["siteUrl"] end) - |> Enum.map(fn url -> String.trim_trailing(url, "/") end) + with {:ok, auth} <- refresh_if_needed(auth) do + res = + HTTPoison.get!("https://www.googleapis.com/webmasters/v3/sites", + "Content-Type": "application/json", + Authorization: "Bearer #{auth.access_token}" + ) + + domains = Jason.decode!(res.body) + |> Map.get("siteEntry", []) + |> Enum.filter(fn site -> site["permissionLevel"] in @verified_permission_levels end) + |> Enum.map(fn site -> site["siteUrl"] end) + |> Enum.map(fn url -> String.trim_trailing(url, "/") end) + + {:ok, domains} + else + err -> err + end end defp property_base_url(property) do @@ -49,7 +53,14 @@ defmodule Plausible.Google.Api do end def fetch_stats(site, query, limit) do - auth = refresh_if_needed(site.google_auth) + with {:ok, auth} <- refresh_if_needed(site.google_auth) do + do_fetch_stats(auth, query, limit) + else + err -> err + end + end + + defp do_fetch_stats(auth, query, limit) do property = URI.encode_www_form(auth.property) base_url = property_base_url(auth.property) filter_groups = if query.filters["page"] do @@ -101,7 +112,7 @@ defmodule Plausible.Google.Api do if Timex.before?(auth.expires, Timex.now() |> Timex.shift(seconds: 30)) do refresh_token(auth) else - auth + {:ok, auth} end end @@ -117,11 +128,15 @@ defmodule Plausible.Google.Api do body = Jason.decode!(res.body) - Plausible.Site.GoogleAuth.changeset(auth, %{ - access_token: body["access_token"], - expires: NaiveDateTime.utc_now() |> NaiveDateTime.add(body["expires_in"]) - }) - |> Plausible.Repo.update!() + if res.status_code == 200 do + Plausible.Site.GoogleAuth.changeset(auth, %{ + access_token: body["access_token"], + expires: NaiveDateTime.utc_now() |> NaiveDateTime.add(body["expires_in"]) + }) + |> Plausible.Repo.update() + else + {:error, body["error"]} + end end defp client_id() do diff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eex index a03d666c9..91126c0bc 100644 --- a/lib/plausible_web/templates/site/settings.html.eex +++ b/lib/plausible_web/templates/site/settings.html.eex @@ -108,27 +108,33 @@ <%= link("Unlink Google account", to: "/#{URI.encode_www_form(@site.domain)}/settings/google", class: "inline-block mt-4 px-4 py-2 border border-gray-300 text-sm leading-5 font-medium rounded-md text-red-700 bg-white hover:text-red-500 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150", method: "delete") %> - <%= if @site.google_auth.property && !(@site.google_auth.property in @search_console_domains) do %> -

- NB: Your Google account does not have access to your currently configured property, <%= @site.google_auth.property %>. Please select a verified property from the list below. -

- <% else %> -

- Select the Google Search Console property you would like to pull keyword data from. If you don't see your domain, <%= link("set it up and verify", to: "https://docs.#{base_domain()}/google-search-console-integration", class: "text-indigo-500") %> on Search Console first. -

- <% end %> + <%= case @search_console_domains do %> + <% {:ok, domains} -> %> + <%= if @site.google_auth.property && !(@site.google_auth.property in domains) do %> +

+ NB: Your Google account does not have access to your currently configured property, <%= @site.google_auth.property %>. Please select a verified property from the list below. +

+ <% else %> +

+ Select the Google Search Console property you would like to pull keyword data from. If you don't see your domain, <%= link("set it up and verify", to: "https://docs.#{base_domain()}/google-search-console-integration", class: "text-indigo-500") %> on Search Console first. +

+ <% end %> - <%= form_for Plausible.Site.GoogleAuth.changeset(@site.google_auth), "/#{URI.encode_www_form(@site.domain)}/settings/google", [class: "max-w-xs"], fn f -> %> -
-
- <%= select f, :property, @search_console_domains, prompt: "(Choose property)", class: "block appearance-none w-full bg-gray-100 text-gray-700 cursor-pointer hover:border-gray-500 p-2 pr-8 rounded leading-normal focus:outline-none" %> -
- + <%= form_for Plausible.Site.GoogleAuth.changeset(@site.google_auth), "/#{URI.encode_www_form(@site.domain)}/settings/google", [class: "max-w-xs"], fn f -> %> +
+
+ <%= select f, :property, @domains, prompt: "(Choose property)", class: "block appearance-none w-full bg-gray-100 text-gray-700 cursor-pointer hover:border-gray-500 p-2 pr-8 rounded leading-normal focus:outline-none" %> +
+ +
+
-
-
- <%= submit "Save", class: "button" %> + <%= submit "Save", class: "button" %> + <% end %> + <% {:error, error} -> %> +

The following error happened when fetching your Google Search Console domains.

+

<%= error %>

<% end %> <% else %> <%= button("Continue with Google", to: Plausible.Google.Api.authorize_url(@site.id), class: "button mt-4") %>