Skip to content

Commit

Permalink
fix(analytics): Posthog event logging updates (#772)
Browse files Browse the repository at this point in the history
* feat: Add openadapt version to posthog events

* feat: Update tracking text of replay, visualize and delete submenus

* fix: Fix code in plotting.py not using correct arguments

* fix: Update broken tests
  • Loading branch information
KIRA009 authored Jun 19, 2024
1 parent b090920 commit 59c2714
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
11 changes: 9 additions & 2 deletions openadapt/app/tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
text (str): The text of the action.
parent (QWidget): The parent widget.
"""
self.tracking_text = kwargs.pop("tracking_text", None)
super().__init__(*args, **kwargs)
if not self.tracking_text:
self.tracking_text = self.text()
self.triggered.connect(self.track_event)

def track_event(self) -> None:
"""Track the event."""
posthog = get_posthog_instance()
posthog.capture(event="action_triggered", properties={"action": self.text()})
posthog.capture(
event="action_triggered", properties={"action": self.tracking_text}
)


class SystemTrayIcon:
Expand Down Expand Up @@ -454,7 +459,9 @@ def populate_menu(self, menu: QMenu, action: Callable, action_type: str) -> None
recording.timestamp
).strftime("%Y-%m-%d %H:%M:%S")
action_text = f"{formatted_timestamp}: {recording.task_description}"
recording_action = TrackedQAction(action_text)
recording_action = TrackedQAction(
action_text, tracking_text=f"{action_type.title()} recording"
)
recording_action.triggered.connect(partial(action, recording))
self.recording_actions[action_type].append(recording_action)
menu.addAction(recording_action)
Expand Down
14 changes: 7 additions & 7 deletions openadapt/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
from itertools import cycle
import math
import os
import unicodedata
import sys
import unicodedata


import matplotlib.pyplot as plt
import numpy as np
from loguru import logger
from PIL import Image, ImageDraw, ImageEnhance, ImageFont
import matplotlib.pyplot as plt
import numpy as np

from openadapt import common, models, utils
from openadapt.config import PERFORMANCE_PLOTS_DIR_PATH, config
from openadapt.models import ActionEvent
from openadapt import common, models, utils


# TODO: move parameters to config
Expand Down Expand Up @@ -352,9 +351,10 @@ def plot_performance(

from openadapt.db import crud

if not recording:
recording = crud.get_latest_recording()
session = crud.get_new_session(read_only=True)

if not recording:
recording = crud.get_latest_recording(session)
perf_stats = crud.get_perf_stats(session, recording)
for perf_stat in perf_stats:
event_type = perf_stat.event_type
Expand Down
7 changes: 7 additions & 0 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Callable
import ast
import base64
import importlib.metadata
import inspect
import os
import sys
Expand Down Expand Up @@ -893,6 +894,12 @@ def capture(self, *args: tuple, **kwargs: dict) -> None:
**kwargs: The event properties.
"""
kwargs.setdefault("distinct_id", config.UNIQUE_USER_ID)
properties = kwargs.get("properties", {})
properties.setdefault("version", importlib.metadata.version("openadapt"))
if not is_running_from_executable():
# for cases when we need to test events in development
properties.setdefault("is_development", True)
kwargs["properties"] = properties
super().capture(*args, **kwargs)


Expand Down
20 changes: 13 additions & 7 deletions tests/openadapt/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ def test_get_scale_ratios() -> None:
def test_posthog_capture() -> None:
"""Tests utils.get_posthog_instance."""
with patch("posthog.Posthog.capture") as mock_capture:
posthog = utils.get_posthog_instance()
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
mock_capture.assert_called_once_with(
event="test_event",
properties={"test_prop": "test_val"},
distinct_id=config.UNIQUE_USER_ID,
)
with patch("importlib.metadata.version") as mock_version:
mock_version.return_value = "1.0.0"
posthog = utils.get_posthog_instance()
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
mock_capture.assert_called_once_with(
event="test_event",
properties={
"test_prop": "test_val",
"version": "1.0.0",
"is_development": True,
},
distinct_id=config.UNIQUE_USER_ID,
)

0 comments on commit 59c2714

Please sign in to comment.