This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic analytics section for segment compatible api
- Loading branch information
John Newman
committed
Oct 9, 2023
1 parent
408d2c1
commit c140b8f
Showing
7 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 15 additions & 12 deletions
27
mautrix_facebook/segment_analytics.py → mautrix_facebook/analytics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
from __future__ import annotations | ||
|
||
from urllib.parse import urlunparse | ||
import logging | ||
|
||
from yarl import URL | ||
import aiohttp | ||
|
||
from mautrix.util import background_task | ||
|
||
from . import user as u | ||
|
||
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 | ||
analytics_url: str | None = None | ||
analytics_token: str | None = None | ||
analytics_user_id: str | None = None | ||
|
||
|
||
async def _track(user: u.User, event: str, properties: dict) -> None: | ||
await http.post( | ||
segment_url, | ||
analytics_url, | ||
json={ | ||
"userId": segment_user_id or user.mxid, | ||
"userId": analytics_user_id or user.mxid, | ||
"event": event, | ||
"properties": {"bridge": "facebook", **properties}, | ||
}, | ||
auth=aiohttp.BasicAuth(login=segment_key, encoding="utf-8"), | ||
auth=aiohttp.BasicAuth(login=analytics_token, encoding="utf-8"), | ||
) | ||
log.debug(f"Tracked {event}") | ||
|
||
|
||
def track(user: u.User, event: str, properties: dict | None = None): | ||
if segment_key: | ||
if analytics_token: | ||
background_task.create(_track(user, event, properties or {})) | ||
|
||
|
||
def init(key, user_id: str | None = None): | ||
global segment_key, segment_user_id, http | ||
segment_key = key | ||
segment_user_id = user_id | ||
def init(host: str | None, token: str | None, user_id: str | None = None): | ||
if not host or not token: | ||
return | ||
global analytics_url, analytics_token, analytics_user_id, http | ||
analytics_url = urlunparse(("https", host, "/v1/track", "", "", "")) | ||
analytics_token = token | ||
analytics_user_id = user_id | ||
http = aiohttp.ClientSession() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters