2024-03-21 13:37:10 +03:00
defmodule PlausibleWeb.GoogleAnalyticsController do
2024-03-12 20:08:25 +03:00
use PlausibleWeb , :controller
plug ( PlausibleWeb.RequireAccountPlug )
plug ( PlausibleWeb.AuthorizeSiteAccess , [ :owner , :admin , :super_admin ] )
def user_metric_notice ( conn , %{
2024-03-21 13:37:10 +03:00
" property_or_view " = > property_or_view ,
2024-03-12 20:08:25 +03:00
" access_token " = > access_token ,
" refresh_token " = > refresh_token ,
" expires_at " = > expires_at ,
" legacy " = > legacy
} ) do
site = conn . assigns . site
conn
|> assign ( :skip_plausible_tracking , true )
|> render ( " user_metric_form.html " ,
site : site ,
2024-03-21 13:37:10 +03:00
property_or_view : property_or_view ,
2024-03-12 20:08:25 +03:00
access_token : access_token ,
refresh_token : refresh_token ,
expires_at : expires_at ,
legacy : legacy ,
layout : { PlausibleWeb.LayoutView , " focus.html " }
)
end
2024-03-21 13:37:10 +03:00
def property_or_view_form ( conn , %{
2024-03-12 20:08:25 +03:00
" access_token " = > access_token ,
" refresh_token " = > refresh_token ,
" expires_at " = > expires_at ,
" legacy " = > legacy
} ) do
2024-03-21 13:37:10 +03:00
site = conn . assigns . site
2024-03-12 20:08:25 +03:00
redirect_route =
if legacy == " true " do
2024-03-21 13:37:10 +03:00
Routes . site_path ( conn , :settings_integrations , site . domain )
else
Routes . site_path ( conn , :settings_imports_exports , site . domain )
end
result =
if legacy == " true " do
Plausible.Google.UA.API . list_views ( access_token )
2024-03-12 20:08:25 +03:00
else
2024-03-21 13:37:10 +03:00
Plausible.Google.API . list_properties_and_views ( access_token )
2024-03-12 20:08:25 +03:00
end
2024-03-21 13:37:10 +03:00
case result do
{ :ok , properties_and_views } ->
2024-03-12 20:08:25 +03:00
conn
|> assign ( :skip_plausible_tracking , true )
2024-03-21 13:37:10 +03:00
|> render ( " property_or_view_form.html " ,
2024-03-12 20:08:25 +03:00
access_token : access_token ,
refresh_token : refresh_token ,
expires_at : expires_at ,
site : conn . assigns . site ,
2024-03-21 13:37:10 +03:00
properties_and_views : properties_and_views ,
2024-03-12 20:08:25 +03:00
legacy : legacy ,
layout : { PlausibleWeb.LayoutView , " focus.html " }
)
{ :error , :authentication_failed } ->
conn
|> put_flash (
:error ,
" We were unable to authenticate your Google Analytics account. Please check that you have granted us permission to 'See and download your Google Analytics data' and try again. "
)
|> redirect ( external : redirect_route )
{ :error , _any } ->
conn
|> put_flash (
:error ,
" We were unable to list your Google Analytics properties. If the problem persists, please contact support for assistance. "
)
|> redirect ( external : redirect_route )
end
end
# see https://stackoverflow.com/a/57416769
@google_analytics_new_user_metric_date ~D[ 2016-08-24 ]
2024-03-21 13:37:10 +03:00
def property_or_view ( conn , %{
" property_or_view " = > property_or_view ,
2024-03-12 20:08:25 +03:00
" access_token " = > access_token ,
" refresh_token " = > refresh_token ,
" expires_at " = > expires_at ,
" legacy " = > legacy
} ) do
site = conn . assigns . site
2024-03-21 13:37:10 +03:00
start_date = Plausible.Google.API . get_analytics_start_date ( access_token , property_or_view )
2024-03-12 20:08:25 +03:00
case start_date do
{ :ok , nil } ->
2024-03-21 13:37:10 +03:00
{ :ok , properties_and_views } =
if legacy == " true " do
Plausible.Google.UA.API . list_views ( access_token )
else
Plausible.Google.API . list_properties_and_views ( access_token )
end
2024-03-12 20:08:25 +03:00
conn
|> assign ( :skip_plausible_tracking , true )
2024-03-21 13:37:10 +03:00
|> render ( " property_or_view_form.html " ,
2024-03-12 20:08:25 +03:00
access_token : access_token ,
refresh_token : refresh_token ,
expires_at : expires_at ,
site : site ,
2024-03-21 13:37:10 +03:00
properties_and_views : properties_and_views ,
selected_property_or_view_error : " No data found. Nothing to import " ,
2024-03-12 20:08:25 +03:00
legacy : legacy ,
layout : { PlausibleWeb.LayoutView , " focus.html " }
)
{ :ok , date } ->
2024-03-21 13:37:10 +03:00
action =
if Timex . before? ( date , @google_analytics_new_user_metric_date ) do
:user_metric_notice
else
:confirm
end
redirect ( conn ,
to :
Routes . google_analytics_path ( conn , action , site . domain ,
property_or_view : property_or_view ,
access_token : access_token ,
refresh_token : refresh_token ,
expires_at : expires_at ,
legacy : legacy
)
)
2024-03-12 20:08:25 +03:00
end
end
def confirm ( conn , %{
2024-03-21 13:37:10 +03:00
" property_or_view " = > property_or_view ,
2024-03-12 20:08:25 +03:00
" access_token " = > access_token ,
" refresh_token " = > refresh_token ,
" expires_at " = > expires_at ,
" legacy " = > legacy
} ) do
site = conn . assigns . site
2024-03-21 13:37:10 +03:00
start_date = Plausible.Google.API . get_analytics_start_date ( access_token , property_or_view )
2024-03-12 20:08:25 +03:00
end_date = Plausible.Sites . native_stats_start_date ( site ) || Timex . today ( site . timezone )
2024-03-21 13:37:10 +03:00
{ :ok , %{ name : property_or_view_name , id : property_or_view } } =
Plausible.Google.API . get_property_or_view ( access_token , property_or_view )
2024-03-12 20:08:25 +03:00
conn
|> assign ( :skip_plausible_tracking , true )
|> render ( " confirm.html " ,
access_token : access_token ,
refresh_token : refresh_token ,
expires_at : expires_at ,
site : site ,
2024-03-21 13:37:10 +03:00
selected_property_or_view : property_or_view ,
selected_property_or_view_name : property_or_view_name ,
2024-03-12 20:08:25 +03:00
start_date : start_date ,
end_date : end_date ,
2024-03-21 13:37:10 +03:00
property? : Plausible.Google.API . property? ( property_or_view ) ,
2024-03-12 20:08:25 +03:00
legacy : legacy ,
layout : { PlausibleWeb.LayoutView , " focus.html " }
)
end
def import ( conn , %{
2024-03-21 13:37:10 +03:00
" property_or_view " = > property_or_view ,
2024-03-12 20:08:25 +03:00
" start_date " = > start_date ,
" end_date " = > end_date ,
" access_token " = > access_token ,
" refresh_token " = > refresh_token ,
" expires_at " = > expires_at ,
" legacy " = > legacy
} ) do
site = conn . assigns . site
current_user = conn . assigns . current_user
redirect_route =
if legacy == " true " do
Routes . site_path ( conn , :settings_integrations , site . domain )
else
Routes . site_path ( conn , :settings_imports_exports , site . domain )
end
2024-03-21 13:37:10 +03:00
if Plausible.Google.API . property? ( property_or_view ) do
{ :ok , _ } =
Plausible.Imported.GoogleAnalytics4 . new_import (
site ,
current_user ,
property : property_or_view ,
2024-03-22 13:17:02 +03:00
label : property_or_view ,
2024-03-21 13:37:10 +03:00
start_date : start_date ,
end_date : end_date ,
access_token : access_token ,
refresh_token : refresh_token ,
token_expires_at : expires_at
)
else
2024-03-12 20:08:25 +03:00
Plausible.Imported.UniversalAnalytics . new_import (
site ,
current_user ,
2024-03-21 13:37:10 +03:00
view_id : property_or_view ,
2024-03-22 13:17:02 +03:00
label : property_or_view ,
2024-03-12 20:08:25 +03:00
start_date : start_date ,
end_date : end_date ,
access_token : access_token ,
refresh_token : refresh_token ,
token_expires_at : expires_at ,
legacy : legacy == " true "
)
2024-03-21 13:37:10 +03:00
end
2024-03-12 20:08:25 +03:00
conn
|> put_flash ( :success , " Import scheduled. An email will be sent when it completes. " )
|> redirect ( external : redirect_route )
end
end