Skip to content

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

Add screen for user metric

Uku Tahtad79678ed70c
diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex
index d1c22ac5a9..6a23939941 100644
--- a/lib/plausible_web/controllers/auth_controller.ex
+++ b/lib/plausible_web/controllers/auth_controller.ex
@@ -545,7 +545,7 @@ defmodule PlausibleWeb.AuthController do
"import" ->
redirect(conn,
to:
- Routes.site_path(conn, :import_from_google_form, site.domain,
+ Routes.site_path(conn, :import_from_google_view_id_form, site.domain,
access_token: res["access_token"]
)
)
diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex
index 21d92ccc07..728e835f46 100644
--- a/lib/plausible_web/controllers/site_controller.ex
+++ b/lib/plausible_web/controllers/site_controller.ex
@@ -638,14 +638,30 @@ defmodule PlausibleWeb.SiteController do
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/general")
end
- def import_from_google_form(conn, %{"access_token" => access_token}) do
+ def import_from_google_user_metric_notice(conn, %{
+ "view_id" => view_id,
+ "access_token" => access_token
+ }) do
+ site = conn.assigns[:site]
+
+ conn
+ |> assign(:skip_plausible_tracking, true)
+ |> render("import_from_google_user_metric_form.html",
+ site: site,
+ view_id: view_id,
+ access_token: access_token,
+ layout: {PlausibleWeb.LayoutView, "focus.html"}
+ )
+ end
+
+ def import_from_google_view_id_form(conn, %{"access_token" => access_token}) do
site = conn.assigns[:site]
view_ids = Plausible.Google.Api.get_analytics_view_ids(access_token)
conn
|> assign(:skip_plausible_tracking, true)
- |> render("import_from_google.html",
+ |> render("import_from_google_view_id_form.html",
access_token: access_token,
site: site,
view_ids: view_ids,
@@ -653,16 +669,32 @@ defmodule PlausibleWeb.SiteController do
)
end
+ # see https://stackoverflow.com/a/57416769
+ @google_analytics_new_user_metric_date ~D[2016-08-24]
def import_from_google_view_id(conn, %{"view_id" => view_id, "access_token" => access_token}) do
site = conn.assigns[:site]
+ start_date = Plausible.Google.Api.get_analytics_start_date(view_id, access_token)
- redirect(conn,
- to:
- Routes.site_path(conn, :import_from_google_confirm, site.domain,
- view_id: view_id,
- access_token: access_token
- )
- )
+ case start_date do
+ {:ok, date} ->
+ if Timex.before?(date, @google_analytics_new_user_metric_date) do
+ redirect(conn,
+ to:
+ Routes.site_path(conn, :import_from_google_user_metric_notice, site.domain,
+ view_id: view_id,
+ access_token: access_token
+ )
+ )
+ else
+ redirect(conn,
+ to:
+ Routes.site_path(conn, :import_from_google_confirm, site.domain,
+ view_id: view_id,
+ access_token: access_token
+ )
+ )
+ end
+ end
end
def import_from_google_confirm(conn, %{"access_token" => access_token, "view_id" => view_id}) do
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 39f2b77b92..5cfb4f5717 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -238,8 +238,16 @@ defmodule PlausibleWeb.Router do
delete "/:website", SiteController, :delete_site
delete "/:website/stats", SiteController, :reset_stats
- get "/:website/import/google-analytics", SiteController, :import_from_google_form
+ get "/:website/import/google-analytics/view-id",
+ SiteController,
+ :import_from_google_view_id_form
+
post "/:website/import/google-analytics/view-id", SiteController, :import_from_google_view_id
+
+ get "/:website/import/google-analytics/user-metric",
+ SiteController,
+ :import_from_google_user_metric_notice
+
get "/:website/import/google-analytics/confirm", SiteController, :import_from_google_confirm
post "/:website/settings/google-import", SiteController, :import_from_google
delete "/:website/settings/forget-imported", SiteController, :forget_imported
diff --git a/lib/plausible_web/templates/site/import_from_google_user_metric_form.html.eex b/lib/plausible_web/templates/site/import_from_google_user_metric_form.html.eex
new file mode 100644
index 0000000000..54789c3429
--- /dev/null
+++ b/lib/plausible_web/templates/site/import_from_google_user_metric_form.html.eex
@@ -0,0 +1,26 @@
+<div class="max-w-md w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8">;
+ <h2 class="text-xl font-black dark:text-gray-100">Import from Google Analytics</h2>
+
+ <div class="mt-6 text-sm text-gray-500">
+ <p>
+ <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 inline text-orange-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
+ <path stroke-linecap="round" stroke-linejoin="round" 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" />
+ </svg>
+
+ Important: Since your GA property includes data from before 23rd August 2016, you have to take an extra step to make sure we can import data smoothly.
+ </p>
+
+ <ol class="mt-4">
+ <li>1. Navigate to the GA property you want to import from</li>
+ <li>2. Go to Admin &gt; Property Settings &gt; User Analysis</li>
+ <li>3. Make sure <i>Enable Users Metric in Reporting</i> is <b>OFF</b></li>
+ </ol>
+
+ <p class="mt-4">
+ The setting may take a few minutes to take effect. If your imported data is showing 0 visitors in unexpected places, it's probably caused by this and you
+ can try importing again later.
+ </p>
+ </div>
+
+ <%= link("Continue ->", to: Routes.site_path(@conn, :import_from_google_confirm, @site.domain, view_id: @view_id, access_token: @access_token), class: "button mt-6") %>
+</div>
diff --git a/lib/plausible_web/templates/site/import_from_google.html.eex b/lib/plausible_web/templates/site/import_from_google_view_id_form.html.eex
similarity index 100%
rename from lib/plausible_web/templates/site/import_from_google.html.eex
rename to lib/plausible_web/templates/site/import_from_google_view_id_form.html.eex