@@ -18,9 +18,10 @@ defmodule PostHog.Config do
1818 ] ,
1919 api_key: [
2020 type: :string ,
21- required: true ,
21+ default: "" ,
2222 doc: """
2323 Your PostHog Project API key. Find it in your project's settings under the Project ID section.
24+ If omitted or empty after trimming whitespace, PostHog starts in disabled/no-op mode.
2425 """
2526 ] ,
2627 api_client_module: [
@@ -184,15 +185,24 @@ defmodule PostHog.Config do
184185
185186 with { :ok , validated } <-
186187 NimbleOptions . validate ( normalized_options , @ compiled_configuration_schema ) do
188+ api_key_blank? = blank_api_key? ( validated )
187189 log_blank_api_key ( validated )
188190
189191 config = Map . new ( validated )
190- client = config . api_client_module . client ( config . api_key , config . api_host )
192+
193+ client =
194+ if api_key_blank? do
195+ nil
196+ else
197+ config . api_client_module . client ( config . api_key , config . api_host )
198+ end
199+
191200 global_properties = Map . merge ( config . global_properties , @ system_global_properties )
192201
193202 final_config =
194203 config
195204 |> Map . put ( :api_client , client )
205+ |> Map . put ( :enabled , not api_key_blank? )
196206 |> Map . put (
197207 :in_app_modules ,
198208 config . in_app_otp_apps |> Enum . flat_map ( & Application . spec ( & 1 , :modules ) ) |> MapSet . new ( )
@@ -219,11 +229,21 @@ defmodule PostHog.Config do
219229 normalized_options
220230 end
221231 end )
232+ |> then ( fn normalized_options ->
233+ if disabled_options? ( normalized_options ) and
234+ not Keyword . has_key? ( normalized_options , :api_host ) do
235+ Keyword . put ( normalized_options , :api_host , @ default_api_host )
236+ else
237+ normalized_options
238+ end
239+ end )
222240 end
223241
224242 defp normalize_api_key ( api_key ) when is_binary ( api_key ) , do: String . trim ( api_key )
225243 defp normalize_api_key ( api_key ) , do: api_key
226244
245+ defp disabled_options? ( options ) , do: Keyword . get ( options , :api_key , "" ) == ""
246+
227247 defp normalize_api_host ( api_host ) when is_binary ( api_host ) do
228248 api_host
229249 |> String . trim ( )
@@ -235,10 +255,12 @@ defmodule PostHog.Config do
235255
236256 defp normalize_api_host ( api_host ) , do: api_host
237257
258+ defp blank_api_key? ( validated ) , do: validated [ :api_key ] == ""
259+
238260 defp log_blank_api_key ( validated ) do
239- if validated [ :api_key ] == "" do
261+ if blank_api_key? ( validated ) do
240262 Logger . error (
241- "posthog api_key is empty after trimming whitespace; check your project API key "
263+ "posthog api_key is empty after trimming whitespace; PostHog will start in disabled/no-op mode "
242264 )
243265 end
244266 end
0 commit comments