Skip to content

Commit 59c2714

Browse files
authored
fix(analytics): Posthog event logging updates (#772)
* 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
1 parent b090920 commit 59c2714

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

openadapt/app/tray.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,18 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
6262
text (str): The text of the action.
6363
parent (QWidget): The parent widget.
6464
"""
65+
self.tracking_text = kwargs.pop("tracking_text", None)
6566
super().__init__(*args, **kwargs)
67+
if not self.tracking_text:
68+
self.tracking_text = self.text()
6669
self.triggered.connect(self.track_event)
6770

6871
def track_event(self) -> None:
6972
"""Track the event."""
7073
posthog = get_posthog_instance()
71-
posthog.capture(event="action_triggered", properties={"action": self.text()})
74+
posthog.capture(
75+
event="action_triggered", properties={"action": self.tracking_text}
76+
)
7277

7378

7479
class SystemTrayIcon:
@@ -454,7 +459,9 @@ def populate_menu(self, menu: QMenu, action: Callable, action_type: str) -> None
454459
recording.timestamp
455460
).strftime("%Y-%m-%d %H:%M:%S")
456461
action_text = f"{formatted_timestamp}: {recording.task_description}"
457-
recording_action = TrackedQAction(action_text)
462+
recording_action = TrackedQAction(
463+
action_text, tracking_text=f"{action_type.title()} recording"
464+
)
458465
recording_action.triggered.connect(partial(action, recording))
459466
self.recording_actions[action_type].append(recording_action)
460467
menu.addAction(recording_action)

openadapt/plotting.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
from itertools import cycle
66
import math
77
import os
8-
import unicodedata
98
import sys
9+
import unicodedata
1010

11-
12-
import matplotlib.pyplot as plt
13-
import numpy as np
1411
from loguru import logger
1512
from PIL import Image, ImageDraw, ImageEnhance, ImageFont
13+
import matplotlib.pyplot as plt
14+
import numpy as np
1615

16+
from openadapt import common, models, utils
1717
from openadapt.config import PERFORMANCE_PLOTS_DIR_PATH, config
1818
from openadapt.models import ActionEvent
19-
from openadapt import common, models, utils
2019

2120

2221
# TODO: move parameters to config
@@ -352,9 +351,10 @@ def plot_performance(
352351

353352
from openadapt.db import crud
354353

355-
if not recording:
356-
recording = crud.get_latest_recording()
357354
session = crud.get_new_session(read_only=True)
355+
356+
if not recording:
357+
recording = crud.get_latest_recording(session)
358358
perf_stats = crud.get_perf_stats(session, recording)
359359
for perf_stat in perf_stats:
360360
event_type = perf_stat.event_type

openadapt/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any, Callable
1010
import ast
1111
import base64
12+
import importlib.metadata
1213
import inspect
1314
import os
1415
import sys
@@ -893,6 +894,12 @@ def capture(self, *args: tuple, **kwargs: dict) -> None:
893894
**kwargs: The event properties.
894895
"""
895896
kwargs.setdefault("distinct_id", config.UNIQUE_USER_ID)
897+
properties = kwargs.get("properties", {})
898+
properties.setdefault("version", importlib.metadata.version("openadapt"))
899+
if not is_running_from_executable():
900+
# for cases when we need to test events in development
901+
properties.setdefault("is_development", True)
902+
kwargs["properties"] = properties
896903
super().capture(*args, **kwargs)
897904

898905

tests/openadapt/test_utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ def test_get_scale_ratios() -> None:
2222
def test_posthog_capture() -> None:
2323
"""Tests utils.get_posthog_instance."""
2424
with patch("posthog.Posthog.capture") as mock_capture:
25-
posthog = utils.get_posthog_instance()
26-
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
27-
mock_capture.assert_called_once_with(
28-
event="test_event",
29-
properties={"test_prop": "test_val"},
30-
distinct_id=config.UNIQUE_USER_ID,
31-
)
25+
with patch("importlib.metadata.version") as mock_version:
26+
mock_version.return_value = "1.0.0"
27+
posthog = utils.get_posthog_instance()
28+
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
29+
mock_capture.assert_called_once_with(
30+
event="test_event",
31+
properties={
32+
"test_prop": "test_val",
33+
"version": "1.0.0",
34+
"is_development": True,
35+
},
36+
distinct_id=config.UNIQUE_USER_ID,
37+
)

0 commit comments

Comments
 (0)