diff --git a/posthog.go b/posthog.go index 4c2aa0d..79ca2f7 100644 --- a/posthog.go +++ b/posthog.go @@ -189,6 +189,11 @@ func (c *client) Enqueue(msg Message) (err error) { if err != nil { c.Errorf("unable to get feature variants - %s", err) } + + if m.Properties == nil { + m.Properties = NewProperties() + } + for feature, variant := range featureVariants { propKey := fmt.Sprintf("$feature/%s", feature) m.Properties[propKey] = variant diff --git a/posthog_test.go b/posthog_test.go index 3b7c77a..f0bf009 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -929,3 +929,41 @@ func TestDisabledFlag(t *testing.T) { t.Errorf("flag listed in /decide/ response should have value 'false'") } } + +func TestCaptureSendFlags(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(fixture("test-api-feature-flag.json"))) + })) + defer server.Close() + + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ + Endpoint: server.URL, + Verbose: true, + Logger: t, + BatchSize: 1, + now: mockTime, + uid: mockId, + + PersonalApiKey: "some very secret key", + }) + defer client.Close() + + // Without this call client.Close hangs forever + // Ref: https://github.com/PostHog/posthog-go/issues/28 + client.IsFeatureEnabled( + FeatureFlagPayload{ + Key: "simpleFlag", + DistinctId: "hey", + }, + ) + + err := client.Enqueue(Capture{ + Event: "Download", + DistinctId: "123456", + SendFeatureFlags: true, + }) + + if err != nil { + t.Fatal(err) + } +}