-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
analytics: generisize to any Segment-compatible API
Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information
1 parent
9e7cd5d
commit 5b62118
Showing
6 changed files
with
76 additions
and
61 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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from __future__ import annotations | ||
|
||
from urllib.parse import urlunparse | ||
import logging | ||
|
||
import aiohttp | ||
|
||
from mautrix.util import background_task | ||
|
||
from . import user as u | ||
|
||
log = logging.getLogger("mau.web.public.analytics") | ||
http: aiohttp.ClientSession | 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: | ||
assert analytics_token | ||
assert analytics_url | ||
assert http | ||
await http.post( | ||
analytics_url, | ||
json={ | ||
"userId": analytics_user_id or user.mxid, | ||
"event": event, | ||
"properties": {"bridge": "linkedin", **properties}, | ||
}, | ||
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 analytics_token: | ||
background_task.create(_track(user, event, properties or {})) | ||
|
||
|
||
def init(base_url: str | None, token: str | None, user_id: str | None = None): | ||
if not base_url or not token: | ||
return | ||
log.info("Initialising segment-compatible analytics") | ||
global analytics_url, analytics_token, analytics_user_id, http | ||
analytics_url = urlunparse(("https", base_url, "/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 was deleted.
Oops, something went wrong.
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