Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from __future__ import annotations

import logging
import os
from functools import lru_cache
from typing import TYPE_CHECKING
Expand All @@ -26,6 +27,7 @@
import posthog as posthog_module

APPLICATION = "aws-sso-elevator"
logger = logging.getLogger(__name__)


@lru_cache(maxsize=1)
Expand All @@ -50,16 +52,20 @@ def capture(event: str, distinct_id: str, properties: dict | None = None) -> Non
"""Capture an analytics event if PostHog is configured.

Adds the global "application" property to all events for filtering.
Failures are logged but don't crash the app.

Args:
event: The event name (e.g., "aws_access_requested").
distinct_id: Unique identifier for the user (typically email).
properties: Optional dict of event properties.
"""
client = get_posthog_client()
if client:
all_properties = {"application": APPLICATION, **(properties or {})}
client.capture(distinct_id, event, properties=all_properties)
try:
client = get_posthog_client()
if client:
all_properties = {"application": APPLICATION, **(properties or {})}
client.capture(event, distinct_id=distinct_id, properties=all_properties)
except Exception:
logger.exception("Failed to capture analytics event")


def shutdown() -> None:
Expand Down
Loading