Skip to content

Commit adbe0bb

Browse files
committed
Stop adding local feature flag data to $feature_flag_called event
See PostHog/posthog-python#181 for more context.
1 parent 64f6494 commit adbe0bb

File tree

2 files changed

+37
-100
lines changed

2 files changed

+37
-100
lines changed

src/PostHog/PostHogClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public bool Capture(
153153

154154
var batchTask = sendFeatureFlags
155155
? AddFreshFeatureFlagDataAsync(distinctId, groups, capturedEvent)
156-
: _featureFlagsLoader.IsLoaded
156+
: _featureFlagsLoader.IsLoaded && eventName != "$feature_flag_called"
157157
? AddLocalFeatureFlagDataAsync(distinctId, groups, capturedEvent)
158158
: Task.FromResult(capturedEvent);
159159

tests/UnitTests/Features/FeatureFlagsTests.cs

Lines changed: 36 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -312,93 +312,47 @@ await client.IsFeatureEnabledAsync(featureKey: "flag-key",
312312
}
313313

314314
[Fact]
315-
public async Task CapturesFeatureFlagRealExample()
315+
public async Task CapturesFeatureFlagButNotAllFlags() // Ported from PostHog/posthog-python test_capture_is_called_but_does_not_add_all_flags
316316
{
317317
var container = new TestContainer("fake-personal-api-key");
318318
container.FakeHttpMessageHandler.AddLocalEvaluationResponse(
319319
"""
320320
{
321321
"flags": [
322322
{
323-
"id": 91866,
324-
"team_id": 110510,
325-
"name": "A multivariate feature flag that tells you what character you are",
326-
"key": "hogtied_got_character",
323+
"id": 1,
324+
"name": "Beta feature",
325+
"key": "complex-flag",
327326
"filters": {
328327
"groups": [
329328
{
330-
"variant": "cersei",
331329
"properties": [
332330
{
333-
"key": "join_date",
334-
"type": "person",
335-
"value": "-14d",
336-
"operator": "is_date_before"
337-
},
338-
{
339-
"key": "leave_date",
331+
"key": "region",
340332
"type": "person",
341-
"value": "2025-01-24 14:20:00",
342-
"operator": "is_date_after"
333+
"value": "USA",
334+
"operator": "exact"
343335
}
344336
],
345337
"rollout_percentage": 100
346-
},
338+
}
339+
]
340+
},
341+
"active": true
342+
},
343+
{
344+
"id": 2,
345+
"name": "Gamma feature",
346+
"key": "simple-flag",
347+
"filters": {
348+
"groups": [
347349
{
348-
"variant": "Cersei",
349-
"properties": [
350-
{
351-
"key": "email",
352-
"type": "person",
353-
"value": [
354-
355-
],
356-
"operator": "exact"
357-
}
358-
],
350+
"properties": [],
359351
"rollout_percentage": 100
360352
}
361-
],
362-
"payloads": {
363-
"Cersei": "25",
364-
"cersei": "{\"role\": \"burn it all down\"}",
365-
"tyrion": "100",
366-
"danaerys": "{\"role\": \"khaleesi\"}",
367-
"jon-snow": "{\"role\": \"king of the north\"}"
368-
},
369-
"multivariate": {
370-
"variants": [
371-
{
372-
"key": "tyrion",
373-
"name": "The one who talks",
374-
"rollout_percentage": 25
375-
},
376-
{
377-
"key": "danaerys",
378-
"name": "The mother of dragons",
379-
"rollout_percentage": 25
380-
},
381-
{
382-
"key": "jon-snow",
383-
"name": "Knows nothing",
384-
"rollout_percentage": 25
385-
},
386-
{
387-
"key": "cersei",
388-
"name": "Not nice",
389-
"rollout_percentage": 15
390-
},
391-
{
392-
"key": "Cersei",
393-
"name": "Capital",
394-
"rollout_percentage": 10
395-
}
396-
]
397-
}
353+
]
398354
},
399-
"deleted": false,
400-
"active": true,
401-
"ensure_experience_continuity": false
355+
"active": true
402356
}
403357
],
404358
"group_type_mapping": {
@@ -412,34 +366,22 @@ public async Task CapturesFeatureFlagRealExample()
412366
var requestHandler = container.FakeHttpMessageHandler.AddBatchResponse();
413367
var client = container.Activate<PostHogClient>();
414368

415-
var result = await client.IsFeatureEnabledAsync(
416-
"hogtied_got_character",
417-
distinctId: "659df793-429a-4517-84ff-747dfc103e6c",
418-
options: new FeatureFlagOptions
419-
{
420-
PersonProperties = new Dictionary<string, object?>
369+
Assert.True(
370+
await client.IsFeatureEnabledAsync(
371+
"complex-flag",
372+
distinctId: "659df793-429a-4517-84ff-747dfc103e6c",
373+
options: new FeatureFlagOptions
421374
{
422-
["join_date"] = "2023-02-02",
423-
["leave_date"] = "2025-02-02"
424-
},
425-
Groups =
426-
[
427-
new Group("organization", "01943db3-83be-0000-e7ea-ecae4d9b5afb"),
428-
new Group("project", "aaaa-bbbb-cccc", new Dictionary<string, object?>
375+
PersonProperties = new Dictionary<string, object?>
429376
{
430-
["size"] = "large"
431-
})
432-
],
433-
OnlyEvaluateLocally = true
434-
});
435-
436-
Assert.True(result);
377+
["region"] = "USA"
378+
},
379+
OnlyEvaluateLocally = true
380+
})
381+
);
437382

438383
await client.FlushAsync();
439384
var received = requestHandler.GetReceivedRequestBody(indented: true);
440-
// NOTE: $active_feature_flags is empty because the person properties is not passed
441-
// through to the capture call when evaluating a feature flag. Should we pass it through?
442-
// TODO: I need to confer with the authors of the other libraries.
443385
Assert.Equal(
444386
$$"""
445387
{
@@ -449,19 +391,14 @@ public async Task CapturesFeatureFlagRealExample()
449391
{
450392
"event": "$feature_flag_called",
451393
"properties": {
452-
"$feature_flag": "hogtied_got_character",
453-
"$feature_flag_response": "cersei",
394+
"$feature_flag": "complex-flag",
395+
"$feature_flag_response": true,
454396
"locally_evaluated": false,
455-
"$feature/hogtied_got_character": "cersei",
397+
"$feature/complex-flag": true,
456398
"distinct_id": "659df793-429a-4517-84ff-747dfc103e6c",
457399
"$lib": "posthog-dotnet",
458400
"$lib_version": "{{VersionConstants.Version}}",
459-
"$geoip_disable": true,
460-
"$groups": {
461-
"organization": "01943db3-83be-0000-e7ea-ecae4d9b5afb",
462-
"project": "aaaa-bbbb-cccc"
463-
},
464-
"$active_feature_flags": []
401+
"$geoip_disable": true
465402
},
466403
"timestamp": "2024-01-21T19:08:23\u002B00:00"
467404
}

0 commit comments

Comments
 (0)