Skip to content

Commit

Permalink
Add ability to define event properties across all events (#63)
Browse files Browse the repository at this point in the history
* adding new config prop Config.DefaultProperties

* change config name from DefaultProperties to DefaultEventProperties

* better config description
  • Loading branch information
jvitoroc authored Aug 13, 2024
1 parent 344ac0d commit c386d6b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// 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
// application when messages sends to the backend API succeeded or failed.
Callback Callback
Expand Down
22 changes: 22 additions & 0 deletions fixtures/test-merge-capture.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
1 change: 1 addition & 0 deletions posthog.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (c *client) Enqueue(msg Message) (err error) {
}
m.Properties["$active_feature_flags"] = featureKeys
}
m.Properties.Merge(c.DefaultEventProperties)
c.setLastCapturedEvent(m)
msg = m

Expand Down
34 changes: 34 additions & 0 deletions posthog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
DefaultEventProperties: 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"))

Expand Down
10 changes: 10 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c386d6b

Please sign in to comment.