Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-bees-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'PostHog': patch
---

Preserve per-capture GeoIP override properties when super properties are configured.
9 changes: 9 additions & 0 deletions src/PostHog/PostHogClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ bool CaptureCore(
}

var captureContext = PostHogContextHelper.ResolveCaptureContext(distinctId, properties);
object? geoIpDisableOverride = null;
var hasGeoIpDisableOverride = captureContext.Properties?.TryGetValue(
PostHogProperties.GeoIpDisable,
out geoIpDisableOverride) == true;

var capturedEvent = new CapturedEvent(
eventName,
Expand All @@ -306,6 +310,11 @@ bool CaptureCore(

capturedEvent.Properties.Merge(_options.Value.SuperProperties);

if (hasGeoIpDisableOverride)
{
capturedEvent.Properties[PostHogProperties.GeoIpDisable] = geoIpDisableOverride!;
}

// Stamp $is_server last so a super property can't override the SDK's server/client classification.
if (_options.Value.IsServer)
{
Expand Down
31 changes: 31 additions & 0 deletions tests/UnitTests/PostHogClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,37 @@ static JsonElement GetOnlyBatchItem(FakeHttpMessageHandler.RequestHandler batchH

public class TheCaptureMethod
{
[Theory]
[InlineData(true, false)]
[InlineData(false, true)]
public async Task CapturePropertiesOverrideConfiguredGeoIpDisable(bool superValue, bool perCaptureValue)
{
var container = new TestContainer(services => services.Configure<PostHogOptions>(options =>
{
options.SuperProperties["$geoip_disable"] = superValue;
}));
var requestHandler = container.FakeHttpMessageHandler.AddBatchResponse();
var client = container.Activate<PostHogClient>();

client.Capture(
"test-user",
"geoip-event",
new Dictionary<string, object>
{
["$geoip_disable"] = perCaptureValue,
["$ip"] = "203.0.113.42"
});
await client.FlushAsync();

using var document = JsonDocument.Parse(requestHandler.GetReceivedRequestBody(indented: false));
var properties = document.RootElement
.GetProperty("batch")[0]
.GetProperty("properties");

Assert.Equal(perCaptureValue, properties.GetProperty("$geoip_disable").GetBoolean());
Assert.Equal("203.0.113.42", properties.GetProperty("$ip").GetString());
}

[Fact]
public async Task SendsEnrichedCapturedEventsWhenSendFeatureFlagsTrueButDoesNotMakeSameDecideCallTwice()
{
Expand Down
Loading