From fa0a69d7460bd4e1e55f559665ac0523fa25e52e Mon Sep 17 00:00:00 2001 From: Scott Weber Date: Tue, 31 Jan 2023 14:11:01 -0500 Subject: [PATCH 1/3] Allow setting segment_user_id in config --- config/config.go | 3 ++- example-config.yaml | 2 ++ main.go | 4 ++++ segment.go | 9 ++++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 6398bd3..ea5463f 100644 --- a/config/config.go +++ b/config/config.go @@ -24,7 +24,8 @@ import ( type Config struct { *bridgeconfig.BaseConfig `yaml:",inline"` - SegmentKey string `yaml:"segment_key"` + SegmentKey string `yaml:"segment_key"` + SegmentUserId string `yaml:"segment_user_id"` Metrics struct { Enabled bool `yaml:"enabled"` diff --git a/example-config.yaml b/example-config.yaml index 4e2faaa..542978a 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -72,6 +72,8 @@ appservice: # Segment API key to track some events, like provisioning API login and encryption errors. segment_key: null +# Optional user_id to use when sending Segment events. If null, defaults to using mxID. +segment_user_id: null # Prometheus config. metrics: diff --git a/main.go b/main.go index 66d778b..23f4bb9 100644 --- a/main.go +++ b/main.go @@ -93,8 +93,12 @@ func (br *WABridge) Init() { Segment.log = br.Log.Sub("Segment") Segment.key = br.Config.SegmentKey + Segment.userID = br.Config.SegmentUserId if Segment.IsEnabled() { Segment.log.Infoln("Segment metrics are enabled") + if Segment.userID != "" { + Segment.log.Infoln("Overriding Segment user_id with %v", Segment.userID) + } } br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database")) diff --git a/segment.go b/segment.go index c943cb2..16b6e53 100644 --- a/segment.go +++ b/segment.go @@ -30,6 +30,7 @@ const SegmentURL = "https://api.segment.io/v1/track" type SegmentClient struct { key string + userID string log log.Logger client http.Client } @@ -38,8 +39,14 @@ var Segment SegmentClient func (sc *SegmentClient) trackSync(userID id.UserID, event string, properties map[string]interface{}) error { var buf bytes.Buffer + var segmentUserID string + if Segment.userID != "" { + segmentUserID = Segment.userID + } else { + segmentUserID = userID.String() + } err := json.NewEncoder(&buf).Encode(map[string]interface{}{ - "userId": userID, + "userId": segmentUserID, "event": event, "properties": properties, }) From 8d4c8afef88b88023fe8a3f00b08f1257837a7be Mon Sep 17 00:00:00 2001 From: Scott Weber Date: Tue, 31 Jan 2023 14:21:37 -0500 Subject: [PATCH 2/3] Forgot two ID uppercasings Co-authored-by: Tulir Asokan --- config/config.go | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index ea5463f..326f859 100644 --- a/config/config.go +++ b/config/config.go @@ -25,7 +25,7 @@ type Config struct { *bridgeconfig.BaseConfig `yaml:",inline"` SegmentKey string `yaml:"segment_key"` - SegmentUserId string `yaml:"segment_user_id"` + SegmentUserID string `yaml:"segment_user_id"` Metrics struct { Enabled bool `yaml:"enabled"` diff --git a/main.go b/main.go index 23f4bb9..1876d75 100644 --- a/main.go +++ b/main.go @@ -93,7 +93,7 @@ func (br *WABridge) Init() { Segment.log = br.Log.Sub("Segment") Segment.key = br.Config.SegmentKey - Segment.userID = br.Config.SegmentUserId + Segment.userID = br.Config.SegmentUserID if Segment.IsEnabled() { Segment.log.Infoln("Segment metrics are enabled") if Segment.userID != "" { From 5d4862f1113a3ed0401164367695a5bbe3631d4f Mon Sep 17 00:00:00 2001 From: Scott Weber Date: Tue, 31 Jan 2023 21:35:31 -0500 Subject: [PATCH 3/3] Forgot to copy segment_user_id in upgrade.go --- config/upgrade.go | 1 + 1 file changed, 1 insertion(+) diff --git a/config/upgrade.go b/config/upgrade.go index cedf95c..00cd3da 100644 --- a/config/upgrade.go +++ b/config/upgrade.go @@ -28,6 +28,7 @@ func DoUpgrade(helper *up.Helper) { bridgeconfig.Upgrader.DoUpgrade(helper) helper.Copy(up.Str|up.Null, "segment_key") + helper.Copy(up.Str|up.Null, "segment_user_id") helper.Copy(up.Bool, "metrics", "enabled") helper.Copy(up.Str, "metrics", "listen")