浏览代码

Allow setting segment_user_id in config (#84)

Scott Weber 2 年之前
父节点
当前提交
e56e7a4e9f

+ 1 - 0
mautrix_instagram/__main__.py

@@ -66,6 +66,7 @@ class InstagramBridge(Bridge):
             shared_secret=cfg["shared_secret"],
             device_seed=self.config["instagram.device_seed"],
             segment_key=cfg["segment_key"],
+            segment_user_id=cfg["segment_user_id"],
         )
         self.az.app.add_subapp(cfg["prefix"], self.provisioning_api.app)
 

+ 1 - 0
mautrix_instagram/config.py

@@ -98,6 +98,7 @@ class Config(BaseBridgeConfig):
         if base["bridge.provisioning.shared_secret"] == "generate":
             base["bridge.provisioning.shared_secret"] = self._new_token()
         copy("bridge.provisioning.segment_key")
+        copy("bridge.provisioning.segment_user_id")
 
         copy("bridge.command_prefix")
 

+ 2 - 0
mautrix_instagram/example-config.yaml

@@ -302,6 +302,8 @@ bridge:
         shared_secret: generate
         # Segment API key to enable analytics tracking for web server endpoints. Set to null to disable.
         segment_key: null
+        # Optional user_id to use when sending Segment events. If null, defaults to using mxID.
+        segment_user_id: null
 
     # The prefix for commands. Only required in non-management rooms.
     command_prefix: "!ig"

+ 8 - 2
mautrix_instagram/web/provisioning_api.py

@@ -59,12 +59,18 @@ class ProvisioningAPI:
     log: TraceLogger = logging.getLogger("mau.web.provisioning")
     app: web.Application
 
-    def __init__(self, shared_secret: str, device_seed: str, segment_key: str | None) -> None:
+    def __init__(
+        self,
+        shared_secret: str,
+        device_seed: str,
+        segment_key: str | None,
+        segment_user_id: str | None,
+    ) -> None:
         self.app = web.Application()
         self.shared_secret = shared_secret
         self.device_seed = device_seed
         if segment_key:
-            init_segment(segment_key)
+            init_segment(segment_key, segment_user_id)
         self.app.router.add_get("/api/whoami", self.status)
         self.app.router.add_options("/api/login", self.login_options)
         self.app.router.add_options("/api/login/2fa", self.login_options)

+ 5 - 3
mautrix_instagram/web/segment.py

@@ -12,13 +12,14 @@ log = logging.getLogger("mau.web.public.analytics")
 segment_url: URL = URL("https://api.segment.io/v1/track")
 http: aiohttp.ClientSession | None = None
 segment_key: str | None = None
+segment_user_id: str | None = None
 
 
 async def _track(user: u.User, event: str, properties: dict) -> None:
     await http.post(
         segment_url,
         json={
-            "userId": user.mxid,
+            "userId": segment_user_id or user.mxid,
             "event": event,
             "properties": {"bridge": "instagram", **properties},
         },
@@ -32,7 +33,8 @@ def track(user: u.User, event: str, properties: dict | None = None):
         asyncio.create_task(_track(user, event, properties or {}))
 
 
-def init(key):
-    global segment_key, http
+def init(key, user_id: str | None = None):
+    global segment_key, segment_user_id, http
     segment_key = key
+    segment_user_id = user_id
     http = aiohttp.ClientSession()