Skip to content

Commit 7bc72ef

Browse files
Merge pull request #24 from PostHog/add-enabled-flag
2 parents ecfabf1 + 4da0cef commit 7bc72ef

File tree

5 files changed

+141
-9
lines changed

5 files changed

+141
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Feature Flags now return a key called `payload` rather than `value` to better align with the other SDKs
1111
- PostHog now requires you to initialize `Posthog.Application` alongside your supervisor tree. This is required because of our `Cachex` system to properly track your FF usage.
1212
- We'll also include local evaluation in the near term, which will also require a GenServer, therefore, requiring us to use a Supervisor.
13+
- Added `enabled_capture` configuration option to disable PostHog tracking in development/test environments
1314

1415
## 0.4.4 - 2025-04-14
1516

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A powerful Elixir client for [PostHog](https://posthog.com), providing seamless
1212
- Batch Processing: Send multiple events efficiently
1313
- Custom Properties: Support for user, group, and person properties
1414
- Flexible Configuration: Customizable JSON library and API version
15+
- Environment Control: Disable tracking in development/test environments
1516

1617
## Installation
1718

@@ -74,7 +75,38 @@ config :posthog,
7475

7576
# Optional configurations
7677
config :posthog,
77-
json_library: Jason # Default JSON parser (optional)
78+
json_library: Jason, # Default JSON parser (optional)
79+
enabled: true # Whether to enable PostHog tracking (optional, defaults to true)
80+
```
81+
82+
### Disabling PostHog capture
83+
84+
You can disable PostHog tracking by setting `enabled_capture: false` in your configuration. This is particularly useful in development or test environments where you don't want to send actual events to PostHog.
85+
86+
When `enabled_capture` is set to `false`:
87+
88+
- All `Posthog.capture/3` and `Posthog.batch/3` calls will succeed silently
89+
- PostHog will still communicate with the server for Feature Flags
90+
91+
This is useful for:
92+
93+
- Development and test environments where you don't want to pollute your PostHog instance
94+
- Situations where you need to temporarily disable tracking
95+
96+
Example configuration for development:
97+
98+
```elixir
99+
# config/dev.exs
100+
config :posthog,
101+
enabled_capture: false # Disable tracking in development
102+
```
103+
104+
Example configuration for test:
105+
106+
```elixir
107+
# config/test.exs
108+
config :posthog,
109+
enabled_capture: false # Disable tracking in test environment
78110
```
79111

80112
## Usage

lib/posthog.ex

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,35 @@ defmodule Posthog do
2222
Optional configuration:
2323
2424
config :posthog,
25-
json_library: Jason, # Default JSON parser (optional)
26-
version: 3 # API version (optional, defaults to 3)
25+
json_library: Jason, # Default JSON parser (optional)
26+
enabled_capture: true # Whether to enable PostHog tracking (optional, defaults to true)
27+
# Set to false in development/test environments to disable tracking
28+
29+
### Disabling PostHog
30+
31+
You can disable PostHog tracking by setting `enabled: false` in your configuration.
32+
This is particularly useful in development or test environments where you don't want
33+
to send actual events to PostHog.
34+
35+
When `enabled_capture` is set to `false`:
36+
- All `Posthog.capture/3` and `Posthog.batch/3` calls will succeed silently
37+
- PostHog will still communicate with the server for Feature Flags
38+
39+
This is useful for:
40+
- Development and test environments where you don't want to pollute your PostHog instance
41+
- Situations where you need to temporarily disable tracking
42+
43+
Example configuration for development:
44+
45+
# config/dev.exs
46+
config :posthog,
47+
enabled_capture: false # Disable tracking in development
48+
49+
Example configuration for test:
50+
51+
# config/test.exs
52+
config :posthog,
53+
enabled_capture: false # Disable tracking in test environment
2754
2855
## Event Tracking
2956

lib/posthog/client.ex

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ defmodule Posthog.Client do
1919
api_url: "https://app.posthog.com", # Required
2020
api_key: "phc_your_project_api_key", # Required
2121
json_library: Jason, # Optional (default: Jason)
22-
version: 3 # Optional (default: 3)
22+
enabled_capture: true # Optional (default: true)
23+
# Set to false to disable all tracking
24+
25+
### Disabling capture
26+
27+
When `enabled_capture` is set to `false`:
28+
- All `Posthog.capture/3` and `Posthog.batch/3` calls will succeed silently
29+
- PostHog will still communicate with the server for Feature Flags
30+
31+
This is useful for:
32+
- Development and test environments where you don't want to pollute your PostHog instance
33+
- Situations where you need to temporarily disable tracking
34+
35+
Example configuration for disabling the client:
36+
37+
# config/dev.exs or config/test.exs
38+
config :posthog,
39+
enabled_capture: false
2340
2441
## API Endpoints
2542
@@ -154,11 +171,19 @@ defmodule Posthog.Client do
154171
@spec capture(event(), properties(), opts() | timestamp()) ::
155172
{:ok, response()} | {:error, response() | term()}
156173
def capture(event, params, opts) when is_list(opts) do
157-
post!("/capture", build_event(event, params, opts[:timestamp]), headers(opts[:headers]))
174+
if enabled_capture?() do
175+
post!("/capture", build_event(event, params, opts[:timestamp]), headers(opts[:headers]))
176+
else
177+
disabled_capture_response()
178+
end
158179
end
159180

160181
def capture(event, params, timestamp) when is_bitstring(event) or is_atom(event) do
161-
post!("/capture", build_event(event, params, timestamp), headers())
182+
if enabled_capture?() do
183+
post!("/capture", build_event(event, params, timestamp), headers())
184+
else
185+
disabled_capture_response()
186+
end
162187
end
163188

164189
@doc """
@@ -186,9 +211,12 @@ defmodule Posthog.Client do
186211
end
187212

188213
def batch(events, _opts, headers) do
189-
body = for {event, params, timestamp} <- events, do: build_event(event, params, timestamp)
190-
191-
post!("/capture", %{batch: body}, headers)
214+
if enabled_capture?() do
215+
body = for {event, params, timestamp} <- events, do: build_event(event, params, timestamp)
216+
post!("/capture", %{batch: body}, headers)
217+
else
218+
disabled_capture_response()
219+
end
192220
end
193221

194222
@doc """
@@ -388,6 +416,15 @@ defmodule Posthog.Client do
388416
end
389417
end
390418

419+
@doc false
420+
defp enabled_capture? do
421+
Application.get_env(@app, :enabled_capture, true)
422+
end
423+
424+
defp disabled_capture_response do
425+
{:ok, %{status: 200, headers: [], body: nil}}
426+
end
427+
391428
@doc false
392429
@spec encode(term(), module()) :: iodata()
393430
defp encode(data, Jason), do: Jason.encode_to_iodata!(data)

test/posthog/client_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Posthog.ClientTest do
22
use ExUnit.Case, async: true
3+
import Mimic
34

45
# Make private functions testable
56
@moduletag :capture_log
@@ -129,4 +130,38 @@ defmodule Posthog.ClientTest do
129130
)
130131
end
131132
end
133+
134+
describe "enabled_capture" do
135+
test "when enabled_capture is false, capture returns success without making request" do
136+
Application.put_env(:posthog, :enabled_capture, false)
137+
on_exit(fn -> Application.delete_env(:posthog, :enabled_capture) end)
138+
139+
assert Client.capture("test_event", %{distinct_id: "user_123"}, []) ==
140+
{:ok, %{status: 200, headers: [], body: nil}}
141+
end
142+
143+
test "when enabled_capture is false, batch returns success without making request" do
144+
Application.put_env(:posthog, :enabled_capture, false)
145+
on_exit(fn -> Application.delete_env(:posthog, :enabled_capture) end)
146+
147+
events = [
148+
{"test_event", %{distinct_id: "user_123"}, nil},
149+
{"another_event", %{distinct_id: "user_123"}, nil}
150+
]
151+
152+
assert Client.batch(events, []) ==
153+
{:ok, %{status: 200, headers: [], body: nil}}
154+
end
155+
156+
test "when enabled_capture is false, feature_flags still works" do
157+
Application.put_env(:posthog, :enabled_capture, false)
158+
on_exit(fn -> Application.delete_env(:posthog, :enabled_capture) end)
159+
160+
# Stub FF HTTP request
161+
stub_with(:hackney, HackneyStub)
162+
163+
assert {:ok, %{feature_flags: flags}} = Client.feature_flags("user_123", [])
164+
assert flags["my-awesome-flag"] == true
165+
end
166+
end
132167
end

0 commit comments

Comments
 (0)