@@ -113,91 +113,89 @@ You can always inspect the context:
113113iex> PostHog .get_context ()
114114%{distinct_id: " distinct_id_of_the_user" }
115115iex> PostHog .get_event_context (" sensitive_event" )
116- %{distinct_id: " distinct_id_of_the_user" , " $process_person_profile" : true }
116+ %{distinct_id: " distinct_id_of_the_user" , " $process_person_profile" : false }
117117```
118118
119119## Feature Flags
120120
121- ` PostHog.FeatureFlags.check/2 ` is the main function for checking a feature flag.
121+ Evaluate feature flags once for a user with ` PostHog.FeatureFlags.evaluate_flags/1 ` ,
122+ then read values from the returned snapshot.
122123
123124``` elixir
124- # Simple boolean feature flag
125- iex> PostHog .FeatureFlags .check (" example-feature-flag-1" , " user123" )
126- {:ok , true }
127-
128- # Note how it automatically sets `$feature/example-feature-flag-1` property in the context
129- iex> PostHog .get_context ()
130- %{" $feature/example-feature-flag-1" => true }
131-
132- # It will attempt to take distinct_id from the context if it's not provided
133- iex> PostHog .set_context (%{distinct_id: " user123" })
134- :ok
135- iex> PostHog .FeatureFlags .check (" example-feature-flag-1" )
136- {:ok , true }
137-
138- # You can also pass a map with body parameters that will be sent to the /flags API as-is
139- iex> PostHog .FeatureFlags .check (" example-feature-flag-1" , %{distinct_id: " user123" , groups: %{group_type: " group_id" }})
140- {:ok , true }
125+ # Boolean feature flag
126+ {:ok , snapshot} = PostHog .FeatureFlags .evaluate_flags (" user123" )
127+ if PostHog .FeatureFlags .Evaluations .enabled? (snapshot, " new-dashboard" ) do
128+ # Do something differently for this user
129+ end
141130
142- # It returns variant if it's set
143- iex> PostHog .FeatureFlags .check (" example-feature-flag-2" , " user123" )
144- {:ok , " variant2" }
131+ # Multivariate feature flag
132+ case PostHog .FeatureFlags .Evaluations .get_flag (snapshot, " checkout-flow" ) do
133+ " variant-a" -> :variant_a
134+ true -> :enabled_boolean_flag
135+ false -> :disabled
136+ nil -> :not_returned
137+ end
145138
146- # Returns error if feature flag doesn't exist
147- iex> PostHog .FeatureFlags .check (" example-feature-flag-3" , " user123" )
148- {:error , %PostHog .UnexpectedResponseError {message: " Feature flag example-feature-flag-3 was not found in the response" , response: .. .}}
139+ # Optional payload
140+ payload = PostHog .FeatureFlags .Evaluations .get_flag_payload (snapshot, " checkout-flow" )
149141```
150142
151- If you're feeling adventurous and/or simply writing a script, you can use the ` PostHog.FeatureFlags.check!/2 ` helper instead and it will return a boolean or raise an error.
152-
153- ``` elixir
154- # Simple boolean feature flag
155- iex> PostHog .FeatureFlags .check! (" example-feature-flag-1" , " user123" )
156- true
157-
158- # Works for variants too
159- iex> PostHog .FeatureFlags .check! (" example-feature-flag-2" , " user123" )
160- " variant2"
161-
162- # Raises error if feature flag doesn't exist
163- iex> PostHog .FeatureFlags .check! (" example-feature-flag-3" , " user123" )
164- ** (PostHog .UnexpectedResponseError ) Feature flag example- feature- flag- 3 was not found in the response
165- ```
143+ ` get_flag/2 ` returns the variant string for multivariate flags, ` true ` for enabled
144+ boolean flags, ` false ` for disabled flags, and ` nil ` when the flag was not returned
145+ by the evaluation.
166146
167- ### Getting the Full Flag Result
147+ ### Include feature flag information when capturing events
168148
169- If you need more than just the value -- for example, the payload configured for a
170- flag or variant -- use ` PostHog.FeatureFlags.get_feature_flag_result/2 ` :
149+ If you want to break down or filter captured events by feature flag value, put the
150+ same snapshot in the process context before capturing events :
171151
172152``` elixir
173- iex> PostHog .FeatureFlags .get_feature_flag_result (" my-flag" , " user123" )
174- {:ok , %PostHog .FeatureFlags .Result {key: " my-flag" , enabled: true , variant: nil , payload: nil }}
153+ {:ok , snapshot} = PostHog .FeatureFlags .evaluate_flags (" user123" )
175154
176- # Multivariant flag with a JSON payload
177- iex > PostHog . FeatureFlags . get_feature_flag_result ( " my-experiment " , " user123 " )
178- { :ok , % PostHog . FeatureFlags . Result { key: " my-experiment " , enabled: true , variant: " control " , payload: %{ " button_color " => " blue " }}}
155+ if PostHog . FeatureFlags . Evaluations . enabled? (snapshot, " new-dashboard " ) do
156+ # Do something differently for this user
157+ end
179158
180- # Flag not found
181- iex> PostHog .FeatureFlags .get_feature_flag_result (" non-existent-flag" , " user123" )
182- {:ok , nil }
159+ PostHog .FeatureFlags .set_in_context (snapshot)
160+ PostHog .capture (" page_viewed" , %{distinct_id: " user123" })
183161```
184162
185- By default this sends a ` $feature_flag_called ` event, which PostHog uses to
186- track feature flag usage in your analytics, and to measure experiment exposure
187- when the flag is linked to an A/B test. You can opt out with ` send_event: false ` :
163+ This attaches ` $feature/<flag-key> ` properties and ` $active_feature_flags ` without
164+ making another ` /flags ` request. To reduce event property bloat, filter the
165+ snapshot first :
188166
189167``` elixir
190- iex> PostHog .FeatureFlags .get_feature_flag_result (" my-flag" , " user123" , send_event: false )
191- {:ok , %PostHog .FeatureFlags .Result {key: " my-flag" , enabled: true , variant: nil , payload: nil }}
168+ # Attach only flags accessed with enabled?/2 or get_flag/2
169+ PostHog .FeatureFlags .set_in_context (
170+ PostHog .FeatureFlags .Evaluations .only_accessed (snapshot)
171+ )
172+
173+ # Or attach only specific flags
174+ PostHog .FeatureFlags .set_in_context (
175+ PostHog .FeatureFlags .Evaluations .only (snapshot, [" checkout-flow" , " new-dashboard" ])
176+ )
192177```
193178
194- A bang variant is also available:
179+ ### Evaluating only specific flags
180+
181+ By default, ` evaluate_flags/1 ` evaluates every flag for the user. If you only need
182+ a few flags, pass ` :flag_keys ` to request only those flags:
195183
196184``` elixir
197- iex> PostHog .FeatureFlags .get_feature_flag_result! (" my-flag" , " user123" )
198- %PostHog .FeatureFlags .Result {key: " my-flag" , enabled: true , variant: nil , payload: nil }
185+ {:ok , snapshot} =
186+ PostHog .FeatureFlags .evaluate_flags (%{
187+ distinct_id: " user123" ,
188+ flag_keys: [" checkout-flow" , " new-dashboard" ]
189+ })
199190```
200191
192+ > #### Deprecated feature flag helpers {: .warning}
193+ >
194+ > ` PostHog.FeatureFlags.check/2 ` , ` PostHog.FeatureFlags.check!/2 ` ,
195+ > ` PostHog.FeatureFlags.get_feature_flag_result/2 ` , and
196+ > ` PostHog.FeatureFlags.get_feature_flag_result!/2 ` still work during the
197+ > migration period, but prefer ` evaluate_flags/1 ` for new code.
198+
201199## Error Tracking
202200
203201Error Tracking is enabled by default.
0 commit comments