From 0f58d56c11841f4feb7010b9962df015765168d9 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 23 Mar 2022 11:19:34 +0200 Subject: [PATCH] Get real start date for date range --- lib/plausible/google/api.ex | 53 +++++++++++++++++++ .../controllers/site_controller.ex | 3 +- .../site/import_from_google_confirm.html.eex | 7 +-- .../templates/site/settings_general.html.eex | 9 ++-- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/lib/plausible/google/api.ex b/lib/plausible/google/api.ex index 91f9c28ad..d941f3d62 100644 --- a/lib/plausible/google/api.ex +++ b/lib/plausible/google/api.ex @@ -157,6 +157,59 @@ defmodule Plausible.Google.Api do end end + def get_analytics_start_date(view_id, token) do + report = %{ + viewId: view_id, + dateRanges: [ + %{ + # The earliest valid date + startDate: "2005-01-01", + endDate: Timex.today() |> Date.to_iso8601() + } + ], + dimensions: [%{name: "ga:date", histogramBuckets: []}], + metrics: [%{expression: "ga:pageviews"}], + hideTotals: true, + hideValueRanges: true, + orderBys: [ + %{ + fieldName: "ga:date", + sortOrder: "ASCENDING" + } + ], + pageSize: 1 + } + + res = + HTTPoison.post!( + "https://analyticsreporting.googleapis.com/v4/reports:batchGet", + Jason.encode!(%{reportRequests: [report]}), + [Authorization: "Bearer #{token}"], + timeout: 30_000, + recv_timeout: 30_000 + ) + + case res.status_code do + 200 -> + report = List.first(Jason.decode!(res.body)["reports"]) + + date = + case report["data"]["rows"] do + [%{"dimensions" => [date_str]}] -> + Timex.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date() + + _ -> + nil + end + + {:ok, date} + + _ -> + Sentry.capture_message("Error fetching Google view ID", extra: Jason.decode!(res.body)) + {:error, res.body} + end + end + # Each element is: {dataset, dimensions, metrics} @request_data [ { diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index b5dfe7309..1e8f39acd 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -660,8 +660,7 @@ defmodule PlausibleWeb.SiteController do def import_from_google_confirm(conn, %{"access_token" => access_token, "view_id" => view_id}) do site = conn.assigns[:site] - # TODO: Plausible.Google.Api.get_analytics_start_date(access_token) - start_date = {:ok, ~D[2019-01-02]} + start_date = Plausible.Google.Api.get_analytics_start_date(view_id, access_token) end_date = Plausible.Stats.Clickhouse.pageview_start_date_local(site) {:ok, view_ids} = Plausible.Google.Api.get_analytics_view_ids(access_token) diff --git a/lib/plausible_web/templates/site/import_from_google_confirm.html.eex b/lib/plausible_web/templates/site/import_from_google_confirm.html.eex index 422feb184..07c68b286 100644 --- a/lib/plausible_web/templates/site/import_from_google_confirm.html.eex +++ b/lib/plausible_web/templates/site/import_from_google_confirm.html.eex @@ -12,7 +12,8 @@
<%= label f, :view_id, "Google Analytics view", class: "block text-sm font-medium text-gray-700" %> - <%= select f, :view_id, @view_ids, readonly: "true", selected: @selected_view_id, class: "mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %> + <%= select f, :view_display, @view_ids, disabled: "true", selected: @selected_view_id, class: "bg-gray-50 mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %> + <%= hidden_input f, :view_id, readonly: "true", value: @selected_view_id %>
Import historical data from your first Google Analytics visitor to your first Plausible visitor @@ -20,12 +21,12 @@
<%= label f, :start_date, "From", class: "block text-sm font-medium text-gray-700" %> - <%= date_input f, :start_date, value: start_date, readonly: "true", class: "mt-1 block w-full px-3 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %> + <%= date_input f, :start_date, value: start_date, readonly: "true", class: "bg-gray-50 mt-1 block w-full px-3 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %>
→
<%= label f, :end_date, "To", class: "block text-sm font-medium text-gray-700" %> - <%= date_input f, :end_date, value: @end_date, readonly: "true", class: "mt-1 block w-full px-3 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %> + <%= date_input f, :end_date, value: @end_date, readonly: "true", class: "bg-gray-50 mt-1 block w-full px-3 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md" %>
<% {:error, error} -> %> diff --git a/lib/plausible_web/templates/site/settings_general.html.eex b/lib/plausible_web/templates/site/settings_general.html.eex index 83fee3166..eb7157a31 100644 --- a/lib/plausible_web/templates/site/settings_general.html.eex +++ b/lib/plausible_web/templates/site/settings_general.html.eex @@ -66,13 +66,16 @@
  • - Forget Imported Data + Import from <%= @site.imported_data.source %> + + +

    - Removes all data imported from <%= @site.imported_data.source %> + <%= PlausibleWeb.EmailView.date_format(@site.imported_data.start_date) %> - <%= PlausibleWeb.EmailView.date_format(@site.imported_data.end_date) %>

    - <%= link("Forget imported stats", to: "/#{URI.encode_www_form(@site.domain)}/settings/forget-imported", method: :delete, class: "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150") %> + <%= link("Clear imported stats", to: "/#{URI.encode_www_form(@site.domain)}/settings/forget-imported", method: :delete, class: "inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150") %>
  • <% @site.imported_data && @site.imported_data.status == "error" -> %>