Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Do not error when api token refresh fails

Uku Taht9100bbf59dad
diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.ex
index 9420fa56a5..3313225e0a 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 a03d666c95..91126c0bce 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 %>
- <p class="text-gray-700 mt-6 font-bold">
- 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.
- </p>
- <% else %>
- <p class="text-gray-700 mt-6">
- 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.
- </p>
- <% end %>
+ <%= case @search_console_domains do %>
+ <% {:ok, domains} -> %>
+ <%= if @site.google_auth.property && !(@site.google_auth.property in domains) do %>
+ <p class="text-gray-700 mt-6 font-bold">
+ 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.
+ </p>
+ <% else %>
+ <p class="text-gray-700 mt-6">
+ 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.
+ </p>
+ <% end %>
- <%= form_for Plausible.Site.GoogleAuth.changeset(@site.google_auth), "/#{URI.encode_www_form(@site.domain)}/settings/google", [class: "max-w-xs"], fn f -> %>
- <div class="my-6">
- <div class="inline-block relative w-full">
- <%= 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" %>
- <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-pink-500">
- <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/></svg>
+ <%= form_for Plausible.Site.GoogleAuth.changeset(@site.google_auth), "/#{URI.encode_www_form(@site.domain)}/settings/google", [class: "max-w-xs"], fn f -> %>
+ <div class="my-6">
+ <div class="inline-block relative w-full">
+ <%= 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" %>
+ <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-pink-500">
+ <svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z"/></svg>
+ </div>
+ </div>
</div>
- </div>
- </div>
- <%= submit "Save", class: "button" %>
+ <%= submit "Save", class: "button" %>
+ <% end %>
+ <% {:error, error} -> %>
+ <p class="text-gray-700 mt-6">The following error happened when fetching your Google Search Console domains.</p>
+ <p class="text-red-700 font-medium mt-3"><%= error %></p>
<% end %>
<% else %>
<%= button("Continue with Google", to: Plausible.Google.Api.authorize_url(@site.id), class: "button mt-4") %>