Provide analytics on Front-end
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -5,6 +5,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Config do
|
||||
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Events.Categories
|
||||
alias Mobilizon.Service.FrontEndAnalytics
|
||||
|
||||
@doc """
|
||||
Gets config.
|
||||
@@ -170,7 +171,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Config do
|
||||
public_key:
|
||||
get_in(Application.get_env(:web_push_encryption, :vapid_details), [:public_key])
|
||||
},
|
||||
export_formats: Config.instance_export_formats()
|
||||
export_formats: Config.instance_export_formats(),
|
||||
analytics: FrontEndAnalytics.config()
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,6 +75,10 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
|
||||
field(:web_push, :web_push, description: "Web Push settings for the instance")
|
||||
|
||||
field(:export_formats, :export_formats, description: "The instance list of export formats")
|
||||
|
||||
field(:analytics, list_of(:analytics),
|
||||
description: "Configuration for diverse analytics services"
|
||||
)
|
||||
end
|
||||
|
||||
@desc """
|
||||
@@ -330,6 +334,28 @@ defmodule Mobilizon.GraphQL.Schema.ConfigType do
|
||||
field(:public_key, :string, description: "The server's public WebPush VAPID key")
|
||||
end
|
||||
|
||||
object :analytics do
|
||||
field(:id, :string, description: "ID of the analytics service")
|
||||
field(:enabled, :boolean, description: "Whether the service is activated or not")
|
||||
|
||||
field(:configuration, list_of(:analytics_configuration),
|
||||
description: "A list of key-values configuration"
|
||||
)
|
||||
end
|
||||
|
||||
enum :analytics_configuration_type do
|
||||
value(:string, description: "A string")
|
||||
value(:integer, description: "An integer")
|
||||
value(:boolean, description: "A boolean")
|
||||
value(:float, description: "A float")
|
||||
end
|
||||
|
||||
object :analytics_configuration do
|
||||
field(:key, :string, description: "The key for the analytics configuration element")
|
||||
field(:value, :string, description: "The value for the analytics configuration element")
|
||||
field(:type, :analytics_configuration_type, description: "The analytics configuration type")
|
||||
end
|
||||
|
||||
@desc """
|
||||
Export formats configuration
|
||||
"""
|
||||
|
||||
53
lib/service/front_end_analytics/analytics.ex
Normal file
53
lib/service/front_end_analytics/analytics.ex
Normal file
@@ -0,0 +1,53 @@
|
||||
defmodule Mobilizon.Service.FrontEndAnalytics do
|
||||
@moduledoc """
|
||||
Behaviour for any analytics service
|
||||
"""
|
||||
|
||||
@callback id() :: String.t()
|
||||
|
||||
@doc """
|
||||
Whether the service is enabled
|
||||
"""
|
||||
@callback enabled?() :: boolean()
|
||||
|
||||
@doc """
|
||||
The configuration for the service
|
||||
"""
|
||||
@callback configuration() :: map()
|
||||
|
||||
@spec providers :: list(module())
|
||||
def providers do
|
||||
:mobilizon
|
||||
|> Application.get_env(:analytics, [])
|
||||
|> Keyword.get(:providers, [])
|
||||
end
|
||||
|
||||
@spec config :: map()
|
||||
def config do
|
||||
Enum.reduce(providers(), [], &load_config/2)
|
||||
end
|
||||
|
||||
@spec load_config(module(), map()) :: map()
|
||||
defp load_config(provider, acc) do
|
||||
acc ++
|
||||
[
|
||||
%{
|
||||
id: provider.id(),
|
||||
enabled: provider.enabled?(),
|
||||
configuration: convert_config(provider.configuration())
|
||||
}
|
||||
]
|
||||
end
|
||||
|
||||
@spec convert_config(Keyword.t()) :: list(map())
|
||||
defp convert_config(config) do
|
||||
Enum.reduce(config, [], fn {key, val}, acc ->
|
||||
acc ++ [%{key: key, value: val, type: type(val)}]
|
||||
end)
|
||||
end
|
||||
|
||||
defp type(val) when is_integer(val), do: :integer
|
||||
defp type(val) when is_float(val), do: :float
|
||||
defp type(val) when is_boolean(val), do: :boolean
|
||||
defp type(val) when is_binary(val), do: :string
|
||||
end
|
||||
30
lib/service/front_end_analytics/matomo.ex
Normal file
30
lib/service/front_end_analytics/matomo.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Mobilizon.Service.FrontEndAnalytics.Matomo do
|
||||
@moduledoc """
|
||||
Matomo analytics provider
|
||||
"""
|
||||
alias Mobilizon.Service.FrontEndAnalytics
|
||||
@behaviour FrontEndAnalytics
|
||||
|
||||
@impl FrontEndAnalytics
|
||||
def id, do: "matomo"
|
||||
|
||||
@doc """
|
||||
Whether the service is enabled
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def enabled? do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.get(:enabled, false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The configuration for the service
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def configuration do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.drop([:enabled])
|
||||
end
|
||||
end
|
||||
31
lib/service/front_end_analytics/plausible.ex
Normal file
31
lib/service/front_end_analytics/plausible.ex
Normal file
@@ -0,0 +1,31 @@
|
||||
defmodule Mobilizon.Service.FrontEndAnalytics.Plausible do
|
||||
@moduledoc """
|
||||
Plausible analytics provider
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.FrontEndAnalytics
|
||||
@behaviour FrontEndAnalytics
|
||||
|
||||
@impl FrontEndAnalytics
|
||||
def id, do: "plausible"
|
||||
|
||||
@doc """
|
||||
Whether the service is enabled
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def enabled? do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.get(:enabled, false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The configuration for the service
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def configuration do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.drop([:enabled])
|
||||
end
|
||||
end
|
||||
31
lib/service/front_end_analytics/sentry.ex
Normal file
31
lib/service/front_end_analytics/sentry.ex
Normal file
@@ -0,0 +1,31 @@
|
||||
defmodule Mobilizon.Service.FrontEndAnalytics.Sentry do
|
||||
@moduledoc """
|
||||
Sentry analytics provider
|
||||
"""
|
||||
|
||||
alias Mobilizon.Service.FrontEndAnalytics
|
||||
@behaviour FrontEndAnalytics
|
||||
|
||||
@impl FrontEndAnalytics
|
||||
def id, do: "sentry"
|
||||
|
||||
@doc """
|
||||
Whether the service is enabled
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def enabled? do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.get(:enabled, false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The configuration for the service
|
||||
"""
|
||||
@impl FrontEndAnalytics
|
||||
def configuration do
|
||||
:mobilizon
|
||||
|> Application.get_env(__MODULE__, [])
|
||||
|> Keyword.drop([:enabled])
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user