mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
bridgev2: add analytics sending method
This commit is contained in:
parent
0328ed1c9f
commit
bc22852f06
7 changed files with 101 additions and 0 deletions
|
|
@ -21,6 +21,7 @@ type Config struct {
|
|||
Homeserver HomeserverConfig `yaml:"homeserver"`
|
||||
AppService AppserviceConfig `yaml:"appservice"`
|
||||
Matrix MatrixConfig `yaml:"matrix"`
|
||||
Analytics AnalyticsConfig `yaml:"analytics"`
|
||||
Provisioning ProvisioningConfig `yaml:"provisioning"`
|
||||
PublicMedia PublicMediaConfig `yaml:"public_media"`
|
||||
DirectMedia DirectMediaConfig `yaml:"direct_media"`
|
||||
|
|
@ -78,6 +79,12 @@ type MatrixConfig struct {
|
|||
UploadFileThreshold int64 `yaml:"upload_file_threshold"`
|
||||
}
|
||||
|
||||
type AnalyticsConfig struct {
|
||||
Token string `yaml:"token"`
|
||||
URL string `yaml:"url"`
|
||||
UserID string `yaml:"user_id"`
|
||||
}
|
||||
|
||||
type ProvisioningConfig struct {
|
||||
Prefix string `yaml:"prefix"`
|
||||
SharedSecret string `yaml:"shared_secret"`
|
||||
|
|
|
|||
|
|
@ -87,6 +87,10 @@ func doUpgrade(helper up.Helper) {
|
|||
helper.Copy(up.Bool, "matrix", "federate_rooms")
|
||||
helper.Copy(up.Int, "matrix", "upload_file_threshold")
|
||||
|
||||
helper.Copy(up.Str|up.Null, "analytics", "token")
|
||||
helper.Copy(up.Str|up.Null, "analytics", "url")
|
||||
helper.Copy(up.Str|up.Null, "analytics", "user_id")
|
||||
|
||||
helper.Copy(up.Str, "provisioning", "prefix")
|
||||
if secret, ok := helper.Get(up.Str, "provisioning", "shared_secret"); !ok || secret == "generate" {
|
||||
sharedSecret := random.String(64)
|
||||
|
|
@ -176,6 +180,7 @@ var SpacedBlocks = [][]string{
|
|||
{"appservice", "as_token"},
|
||||
{"appservice", "username_template"},
|
||||
{"matrix"},
|
||||
{"analytics"},
|
||||
{"provisioning"},
|
||||
{"public_media"},
|
||||
{"direct_media"},
|
||||
|
|
|
|||
62
bridgev2/matrix/analytics.go
Normal file
62
bridgev2/matrix/analytics.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package matrix
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
func (br *Connector) trackSync(userID id.UserID, event string, properties map[string]any) error {
|
||||
var buf bytes.Buffer
|
||||
var analyticsUserID string
|
||||
if br.Config.Analytics.UserID != "" {
|
||||
analyticsUserID = br.Config.Analytics.UserID
|
||||
} else {
|
||||
analyticsUserID = userID.String()
|
||||
}
|
||||
err := json.NewEncoder(&buf).Encode(map[string]any{
|
||||
"userId": analyticsUserID,
|
||||
"event": event,
|
||||
"properties": properties,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, br.Config.Analytics.URL, &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.SetBasicAuth(br.Config.Analytics.Token, "")
|
||||
resp, err := br.AS.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status code %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (br *Connector) Track(userID id.UserID, event string, props map[string]any) {
|
||||
if br.Config.Analytics.Token == "" || br.Config.Analytics.URL == "" {
|
||||
return
|
||||
}
|
||||
|
||||
if props == nil {
|
||||
props = map[string]any{}
|
||||
}
|
||||
props["bridge"] = br.Bridge.Network.GetName().BeeperBridgeType
|
||||
go func() {
|
||||
err := br.trackSync(userID, event, props)
|
||||
if err != nil {
|
||||
br.Log.Err(err).Str("component", "analytics").Str("event", event).Msg("Error tracking event")
|
||||
} else {
|
||||
br.Log.Debug().Str("component", "analytics").Str("event", event).Msg("Tracked event")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
@ -103,6 +103,9 @@ var (
|
|||
_ bridgev2.MatrixConnector = (*Connector)(nil)
|
||||
_ bridgev2.MatrixConnectorWithServer = (*Connector)(nil)
|
||||
_ bridgev2.MatrixConnectorWithPostRoomBridgeHandling = (*Connector)(nil)
|
||||
_ bridgev2.MatrixConnectorWithPublicMedia = (*Connector)(nil)
|
||||
_ bridgev2.MatrixConnectorWithNameDisambiguation = (*Connector)(nil)
|
||||
_ bridgev2.MatrixConnectorWithAnalytics = (*Connector)(nil)
|
||||
)
|
||||
|
||||
func NewConnector(cfg *bridgeconfig.Config) *Connector {
|
||||
|
|
|
|||
|
|
@ -205,6 +205,15 @@ matrix:
|
|||
# rather than keeping the whole file in memory.
|
||||
upload_file_threshold: 5242880
|
||||
|
||||
# Segment-compatible analytics endpoint for tracking some events, like provisioning API login and encryption errors.
|
||||
analytics:
|
||||
# API key to send with tracking requests. Tracking is disabled if this is null.
|
||||
token: null
|
||||
# Address to send tracking requests to.
|
||||
url: https://api.segment.io/v1/track
|
||||
# Optional user ID for tracking events. If null, defaults to using Matrix user ID.
|
||||
user_id: null
|
||||
|
||||
# Settings for provisioning API
|
||||
provisioning:
|
||||
# Prefix for the provisioning API paths.
|
||||
|
|
|
|||
|
|
@ -73,6 +73,10 @@ type MatrixConnectorWithPostRoomBridgeHandling interface {
|
|||
HandleNewlyBridgedRoom(ctx context.Context, roomID id.RoomID) error
|
||||
}
|
||||
|
||||
type MatrixConnectorWithAnalytics interface {
|
||||
Track(userID id.UserID, event string, properties map[string]any)
|
||||
}
|
||||
|
||||
type MatrixSendExtra struct {
|
||||
Timestamp time.Time
|
||||
MessageMeta *database.Message
|
||||
|
|
|
|||
|
|
@ -253,3 +253,14 @@ func (user *User) GetManagementRoom(ctx context.Context) (id.RoomID, error) {
|
|||
func (user *User) Save(ctx context.Context) error {
|
||||
return user.Bridge.DB.User.Update(ctx, user.User)
|
||||
}
|
||||
|
||||
func (br *Bridge) Track(userID id.UserID, event string, props map[string]any) {
|
||||
analyticSender, ok := br.Matrix.(MatrixConnectorWithAnalytics)
|
||||
if ok {
|
||||
analyticSender.Track(userID, event, props)
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) Track(event string, props map[string]any) {
|
||||
user.Bridge.Track(user.MXID, event, props)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue