From f1fc326ae305c5ef6bb044932c100c728cf153da Mon Sep 17 00:00:00 2001 From: Joao Vitor Date: Sat, 10 Aug 2024 08:20:31 -0300 Subject: [PATCH 1/3] adding new config prop Config.DefaultProperties --- config.go | 5 +++++ fixtures/test-merge-capture.json | 22 +++++++++++++++++++++ posthog.go | 1 + posthog_test.go | 34 ++++++++++++++++++++++++++++++++ properties.go | 10 ++++++++++ properties_test.go | 12 +++++++++++ 6 files changed, 84 insertions(+) create mode 100644 fixtures/test-merge-capture.json diff --git a/config.go b/config.go index ee8d2dd..e8f29ef 100644 --- a/config.go +++ b/config.go @@ -52,6 +52,11 @@ type Config struct { // `os.Stderr`. Logger Logger + // Properties that will be included + // in every event sent by the client. This is useful for adding common metadata + // like service name or app version across all events. + DefaultProperties Properties + // The callback object that will be used by the client to notify the // application when messages sends to the backend API succeeded or failed. Callback Callback diff --git a/fixtures/test-merge-capture.json b/fixtures/test-merge-capture.json new file mode 100644 index 0000000..d754fdd --- /dev/null +++ b/fixtures/test-merge-capture.json @@ -0,0 +1,22 @@ +{ + "api_key": "Csyjlnlun3OzyNJAafdlv", + "batch": [ + { + "distinct_id": "123456", + "event": "Download", + "library": "posthog-go", + "library_version": "1.0.0", + "properties": { + "$lib": "posthog-go", + "$lib_version": "1.0.0", + "application": "PostHog Go", + "platform": "macos", + "service": "api", + "version": "1.0.0" + }, + "send_feature_flags": false, + "timestamp": "2015-07-10T23:00:00Z", + "type": "capture" + } + ] +} diff --git a/posthog.go b/posthog.go index 5d64de6..7b5f20a 100644 --- a/posthog.go +++ b/posthog.go @@ -227,6 +227,7 @@ func (c *client) Enqueue(msg Message) (err error) { } m.Properties["$active_feature_flags"] = featureKeys } + m.Properties.Merge(c.DefaultProperties) c.setLastCapturedEvent(m) msg = m diff --git a/posthog_test.go b/posthog_test.go index b8bdcd0..817c0ca 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -436,6 +436,40 @@ func TestCaptureWithTimestamp(t *testing.T) { } } +func TestCaptureWithDefaultProperties(t *testing.T) { + var ref = strings.TrimSpace(fixture("test-merge-capture.json")) + + body, server := mockServer() + defer server.Close() + + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ + Endpoint: server.URL, + Verbose: true, + DefaultProperties: NewProperties().Set("service", "api"), + Logger: t, + BatchSize: 1, + now: mockTime, + uid: mockId, + }) + defer client.Close() + + client.Enqueue(Capture{ + Event: "Download", + DistinctId: "123456", + Properties: Properties{ + "application": "PostHog Go", + "version": "1.0.0", + "platform": "macos", // :) + }, + SendFeatureFlags: false, + Timestamp: time.Date(2015, time.July, 10, 23, 0, 0, 0, time.UTC), + }) + + if res := string(<-body); ref != res { + t.Errorf("invalid response:\n- expected %s\n- received: %s", ref, res) + } +} + func TestCaptureMany(t *testing.T) { var ref = strings.TrimSpace(fixture("test-many-capture.json")) diff --git a/properties.go b/properties.go index 49d7fbe..9a5efb9 100644 --- a/properties.go +++ b/properties.go @@ -22,3 +22,13 @@ func (p Properties) Set(name string, value interface{}) Properties { p[name] = value return p } + +// Merge adds the properties from the provided `props` into the receiver `p`. +// If a property in `props` already exists in `p`, its value will be overwritten. +func (p Properties) Merge(props Properties) Properties { + for k, v := range props { + p[k] = v + } + + return p +} diff --git a/properties_test.go b/properties_test.go index 2d00d5e..4fb6a8e 100644 --- a/properties_test.go +++ b/properties_test.go @@ -34,5 +34,17 @@ func TestPropertiesMulti(t *testing.T) { if !reflect.DeepEqual(p0, p1) { t.Errorf("invalid properties produced by chained setters:\n- expected %#v\n- found: %#v", p0, p1) } +} + +func TestPropertiesMerge(t *testing.T) { + defaultProps := Properties{"currency": "USD", "service": "api"} + + props := NewProperties().Set("title", "A").Set("value", 0.5).Set("currency", "BRL") + props.Merge(defaultProps) + expected := Properties{"title": "A", "value": 0.5, "currency": "USD", "service": "api"} + + if !reflect.DeepEqual(props, Properties{"title": "A", "value": 0.5, "currency": "USD", "service": "api"}) { + t.Errorf("invalid properties produced by merge:\n- expected %#v\n- found: %#v", expected, props) + } } From 7aa3fd94fb71425e28ae6e2468bbe6b22c552f69 Mon Sep 17 00:00:00 2001 From: Joao Vitor Date: Sat, 10 Aug 2024 12:39:19 -0300 Subject: [PATCH 2/3] change config name from DefaultProperties to DefaultEventProperties --- config.go | 8 ++++---- posthog.go | 2 +- posthog_test.go | 14 +++++++------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index e8f29ef..1fe0d3a 100644 --- a/config.go +++ b/config.go @@ -52,10 +52,10 @@ type Config struct { // `os.Stderr`. Logger Logger - // Properties that will be included - // in every event sent by the client. This is useful for adding common metadata - // like service name or app version across all events. - DefaultProperties Properties + // Properties that will be included in every event sent by the client. + // This is useful for adding common metadata like service name or app version + // across all events. + DefaultEventProperties Properties // The callback object that will be used by the client to notify the // application when messages sends to the backend API succeeded or failed. diff --git a/posthog.go b/posthog.go index 7b5f20a..f6ed9c5 100644 --- a/posthog.go +++ b/posthog.go @@ -227,7 +227,7 @@ func (c *client) Enqueue(msg Message) (err error) { } m.Properties["$active_feature_flags"] = featureKeys } - m.Properties.Merge(c.DefaultProperties) + m.Properties.Merge(c.DefaultEventProperties) c.setLastCapturedEvent(m) msg = m diff --git a/posthog_test.go b/posthog_test.go index 817c0ca..ed56c76 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -443,13 +443,13 @@ func TestCaptureWithDefaultProperties(t *testing.T) { defer server.Close() client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ - Endpoint: server.URL, - Verbose: true, - DefaultProperties: NewProperties().Set("service", "api"), - Logger: t, - BatchSize: 1, - now: mockTime, - uid: mockId, + Endpoint: server.URL, + Verbose: true, + DefaultEventProperties: NewProperties().Set("service", "api"), + Logger: t, + BatchSize: 1, + now: mockTime, + uid: mockId, }) defer client.Close() From fda2f6e59a7a3b81cc11bf9b2252308827da7bc0 Mon Sep 17 00:00:00 2001 From: Joao Vitor Date: Sat, 10 Aug 2024 12:50:10 -0300 Subject: [PATCH 3/3] better config description --- config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 1fe0d3a..51ff05d 100644 --- a/config.go +++ b/config.go @@ -53,8 +53,8 @@ type Config struct { Logger Logger // Properties that will be included in every event sent by the client. - // This is useful for adding common metadata like service name or app version - // across all events. + // This is useful for adding common metadata like service name or app version across all events. + // If a property conflict occurs, the value from DefaultEventProperties will overwrite any existing value. DefaultEventProperties Properties // The callback object that will be used by the client to notify the