Get real start date for date range

pull/1749/head
Uku Taht 5 years ago
parent 445b8c5d43
commit 0f58d56c11

@ -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 [
{

@ -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)

@ -12,7 +12,8 @@
<div class="mt-3">
<%= 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 %>
</div>
<div class="mt-6 text-sm text-gray-500">
Import historical data from your first Google Analytics visitor to your first Plausible visitor
@ -20,12 +21,12 @@
<div class="flex justify-between mt-3">
<div class="w-36">
<%= 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" %>
</div>
<div class="align-middle pt-8">&rarr;</div>
<div class="w-36">
<%= 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" %>
</div>
</div>
<% {:error, error} -> %>

@ -66,13 +66,16 @@
<li class="py-4 flex items-center justify-between space-x-4">
<div class="flex flex-col">
<p class="text-sm leading-5 font-medium text-gray-900 dark:text-gray-100">
Forget Imported Data
Import from <%= @site.imported_data.source %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 inline -mt-1 text-green-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
</svg>
</p>
<p class="text-sm leading-5 text-gray-500 dark:text-gray-200">
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) %>
</p>
</div>
<%= 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") %>
</li>
<% @site.imported_data && @site.imported_data.status == "error" -> %>

Loading…
Cancel
Save