Ga import improvements (#1784)
* Do not link Google account for import * Record start date * Fix testspull/1749/head
parent
2ff1cdda7c
commit
203f87520b
@ -1,48 +0,0 @@
|
||||
defmodule Mix.Tasks.CallGoogle do
|
||||
use Mix.Task
|
||||
use Plausible.Repo
|
||||
require Logger
|
||||
|
||||
@token "ya29.A0ARrdaM-GC-C38oUNmoUlica3v2IGT1eACtvreAFcM4zCYrpQxHdTFfhpmWQfiYAblnKPVRkt_fW-jTTwJ_ScXtkaqq6P_XiBbPIa3g9yJ_F6bP2tEOeHgq07aPlPwHvqXaK8I8gmwtO86VvJLvmhXolHtlXN0g"
|
||||
@view_id "204615196"
|
||||
@dimensions ["ga:date"]
|
||||
@metrics ["ga:users", "ga:pageviews", "ga:bounces", "ga:sessions", "ga:sessionDuration"]
|
||||
|
||||
def run(_) do
|
||||
Mix.Task.run("app.start")
|
||||
|
||||
report = %{
|
||||
viewId: @view_id,
|
||||
dateRanges: [
|
||||
%{
|
||||
# The earliest valid date
|
||||
startDate: "2005-01-01",
|
||||
endDate: "2019-12-05"
|
||||
}
|
||||
],
|
||||
dimensions: Enum.map(@dimensions, &%{name: &1, histogramBuckets: []}),
|
||||
metrics: Enum.map(@metrics, &%{expression: &1}),
|
||||
hideTotals: true,
|
||||
hideValueRanges: true,
|
||||
orderBys: [
|
||||
%{
|
||||
fieldName: "ga:date",
|
||||
sortOrder: "DESCENDING"
|
||||
}
|
||||
],
|
||||
pageSize: 100_00,
|
||||
pageToken: ""
|
||||
}
|
||||
|
||||
Logger.debug(report)
|
||||
|
||||
res =
|
||||
HTTPoison.post!(
|
||||
"https://analyticsreporting.googleapis.com/v4/reports:batchGet",
|
||||
Jason.encode!(%{reportRequests: [report]}),
|
||||
Authorization: "Bearer #{@token}"
|
||||
)
|
||||
|
||||
Logger.debug(res.body)
|
||||
end
|
||||
end
|
||||
@ -1,27 +1,22 @@
|
||||
<%= form_for @conn, "/#{URI.encode_www_form(@site.domain)}/settings/google-import", [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"], fn f -> %>
|
||||
<%= form_for @conn, Routes.site_path(@conn, :import_from_google_view_id, @site.domain), [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"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Import from Google Analytics</h2>
|
||||
|
||||
<%= hidden_input(f, :access_token, value: @access_token) %>
|
||||
|
||||
<%= case @view_ids do %>
|
||||
<% {:ok, view_ids} -> %>
|
||||
|
||||
<div class="mt-6 text-sm text-gray-500">
|
||||
You can choose the view ID that will be imported to the <%= @site.domain %> dashboard.
|
||||
</div>
|
||||
|
||||
<div class="my-6">
|
||||
<%= label f, :view_id, "Google Analytics view id", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= select f, :view_id, view_ids, prompt: "(Choose view id)", required: "true", 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" %>
|
||||
Choose the view in your Google Analytics account that will be imported to the <%= @site.domain %> dashboard.
|
||||
</div>
|
||||
|
||||
<div class="my-6">
|
||||
<%= label f, :end_date, "Import end date", class: "block text-sm font-medium text-gray-700" %>
|
||||
<div class="mt-1">
|
||||
<%= date_input f, :end_date, value: Timex.today() |> Date.to_iso8601(), required: "true", class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
|
||||
</div>
|
||||
<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, prompt: "(Choose view)", required: "true", 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" %>
|
||||
</div>
|
||||
<% {:error, error} -> %>
|
||||
<p class="text-gray-700 dark:text-gray-300 mt-6">The following error occurred when fetching your Google Analytics view ids.</p>
|
||||
<p class="text-red-700 font-medium mt-3"><%= error %></p>
|
||||
<% end %>
|
||||
<%= submit "Import", class: "button" %>
|
||||
<%= submit "Continue ->", class: "button mt-6" %>
|
||||
<% end %>
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
<%= form_for @conn, Routes.site_path(@conn, :import_from_google, @site.domain), [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"], fn f -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Import from Google Analytics</h2>
|
||||
|
||||
<%= hidden_input(f, :access_token, value: @access_token) %>
|
||||
|
||||
<%= case @start_date do %>
|
||||
<% {:ok, start_date} -> %>
|
||||
|
||||
<div class="mt-6 text-sm text-gray-500">
|
||||
Choose the view in your Google Analytics account that will be imported to the <%= @site.domain %> dashboard.
|
||||
</div>
|
||||
|
||||
<div class="mt-3">
|
||||
<%= label f, :view_id, "Google Analytics view", class: "block text-sm font-medium text-gray-700" %>
|
||||
<%= text_input f, :view_id, value: @view_id, readonly: "true", 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" %>
|
||||
</div>
|
||||
<div class="mt-6 text-sm text-gray-500">
|
||||
We will import historical data from your first Google Analytics visitor to your first Plausible visitor. If you'd
|
||||
like to import data for a different time period, please contact support.
|
||||
</div>
|
||||
<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" %>
|
||||
</div>
|
||||
<div class="align-middle pt-8">→</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" %>
|
||||
</div>
|
||||
</div>
|
||||
<% {:error, error} -> %>
|
||||
<p class="text-gray-700 dark:text-gray-300 mt-6">The following error occurred when fetching your Google Analytics data.</p>
|
||||
<p class="text-red-700 font-medium mt-3"><%= error %></p>
|
||||
<% end %>
|
||||
|
||||
<%= submit "Confirm import", class: "button mt-6" %>
|
||||
<% end %>
|
||||
Loading…
Reference in New Issue