diff --git a/lib/posthog.ex b/lib/posthog.ex index a6cc9c9..32e9287 100644 --- a/lib/posthog.ex +++ b/lib/posthog.ex @@ -36,6 +36,25 @@ defmodule PostHog do @spec config(supervisor_name()) :: PostHog.Config.config() def config(name \\ __MODULE__), do: PostHog.Registry.config(name) + @doc """ + Returns whether a named `PostHog` supervisor is enabled. + + A supervisor is disabled when it was started without a configured API key. In + disabled mode, capture calls become no-ops. + + ## Examples + + Check the default `PostHog` instance: + + PostHog.enabled?() + + Check a named instance: + + PostHog.enabled?(MyPostHog) + """ + @spec enabled?(supervisor_name()) :: boolean() + def enabled?(name \\ __MODULE__), do: config(name).enabled + @doc false def bare_capture(event, distinct_id, %{} = properties), do: bare_capture(__MODULE__, event, distinct_id, properties) diff --git a/test/posthog_test.exs b/test/posthog_test.exs index 324d864..b79d433 100644 --- a/test/posthog_test.exs +++ b/test/posthog_test.exs @@ -21,6 +21,17 @@ defmodule PostHogTest do end end + describe "enabled?/1" do + test "returns true when the PostHog instance has an API key" do + assert PostHog.enabled?() + end + + @tag config: [api_key: "", supervisor_name: DisabledPostHog] + test "returns false when the PostHog instance has no API key" do + refute PostHog.enabled?(DisabledPostHog) + end + end + describe "bare_capture/4" do test "simple call" do PostHog.bare_capture("case tested", "distinct_id")