diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index d1c22ac5a..6a2393994 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 21d92ccc0..728e835f4 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 39f2b77b9..5cfb4f571 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 000000000..54789c342 --- /dev/null +++ b/lib/plausible_web/templates/site/import_from_google_user_metric_form.html.eex @@ -0,0 +1,26 @@ +
; +

Import from Google Analytics

+ +
+

+ + + + + 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. +

+ +
    +
  1. 1. Navigate to the GA property you want to import from
  2. +
  3. 2. Go to Admin > Property Settings > User Analysis
  4. +
  5. 3. Make sure Enable Users Metric in Reporting is OFF
  6. +
+ +

+ 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. +

+
+ + <%= 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") %> +
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