segment.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan, Sumner Evans
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "net/http"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix/id"
  24. )
  25. const SegmentURL = "https://api.segment.io/v1/track"
  26. type SegmentClient struct {
  27. key string
  28. userID string
  29. log log.Logger
  30. client http.Client
  31. }
  32. var Segment SegmentClient
  33. func (sc *SegmentClient) trackSync(userID id.UserID, event string, properties map[string]interface{}) error {
  34. var buf bytes.Buffer
  35. var segmentUserID string
  36. if Segment.userID != "" {
  37. segmentUserID = Segment.userID
  38. } else {
  39. segmentUserID = userID.String()
  40. }
  41. err := json.NewEncoder(&buf).Encode(map[string]interface{}{
  42. "userId": segmentUserID,
  43. "event": event,
  44. "properties": properties,
  45. })
  46. if err != nil {
  47. return err
  48. }
  49. req, err := http.NewRequest("POST", SegmentURL, &buf)
  50. if err != nil {
  51. return err
  52. }
  53. req.SetBasicAuth(sc.key, "")
  54. resp, err := sc.client.Do(req)
  55. if err != nil {
  56. return err
  57. }
  58. _ = resp.Body.Close()
  59. if resp.StatusCode != 200 {
  60. return fmt.Errorf("unexpected status code %d", resp.StatusCode)
  61. }
  62. return nil
  63. }
  64. func (sc *SegmentClient) IsEnabled() bool {
  65. return len(sc.key) > 0
  66. }
  67. func (sc *SegmentClient) Track(userID id.UserID, event string, properties ...map[string]interface{}) {
  68. if !sc.IsEnabled() {
  69. return
  70. } else if len(properties) > 1 {
  71. panic("Track should be called with at most one property map")
  72. }
  73. go func() {
  74. props := map[string]interface{}{}
  75. if len(properties) > 0 {
  76. props = properties[0]
  77. }
  78. props["bridge"] = "whatsapp"
  79. err := sc.trackSync(userID, event, props)
  80. if err != nil {
  81. sc.log.Errorfln("Error tracking %s: %v", event, err)
  82. } else {
  83. sc.log.Debugln("Tracked", event)
  84. }
  85. }()
  86. }