Add end date to GA import
parent
526588f8ed
commit
e1d6d119b0
@ -0,0 +1,48 @@
|
||||
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
|
||||
@ -0,0 +1,27 @@
|
||||
<%= 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 -> %>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Import from Google Analytics</h2>
|
||||
|
||||
<%= 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" %>
|
||||
</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>
|
||||
<% {: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" %>
|
||||
<% end %>
|
||||
Loading…
Reference in New Issue