mirror of
https://github.com/baserow/baserow.git
synced 2026-12-06 17:28:38 +00:00
* feat(analytics): record where each change comes from Every action event sent to PostHog carries `action_source`: browser, api, mcp, assistant, agent, automation or builder. Actions by an identity are now sent without a person profile instead of being skipped. * fix(analytics): report application builder changes as application * style: format the action source test * fix(analytics): report agent row actions as agent from their dispatch context * fix(analytics): send no email with events of identities * refactor(analytics): one name for the action source and no email for identities by default * feat(analytics): keep the starting channel on jobs and label scheduled and public changes * chore: remove the local venv link * chore: drop the changelog entry * fix(analytics): keep the source on exports, MCP connects and button clicks * test(analytics): cover the dispatch context labels and public-view exports
252 lines
7.4 KiB
Python
252 lines
7.4 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
from django.db import transaction
|
|
from django.test.utils import override_settings
|
|
|
|
import pytest
|
|
|
|
from baserow.core.action.registries import ActionType
|
|
from baserow.core.action.signals import ActionCommandType
|
|
from baserow.core.posthog import (
|
|
capture_event_action_done,
|
|
capture_event_without_person,
|
|
capture_user_event,
|
|
get_posthog_client,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_posthog_instance():
|
|
from baserow.core import posthog
|
|
|
|
posthog._posthog = None
|
|
yield
|
|
posthog._posthog = None
|
|
|
|
|
|
class TestActionType(ActionType):
|
|
type = "test"
|
|
analytics_params = ["must_be_kept"]
|
|
|
|
class Params:
|
|
must_be_kept: str
|
|
must_be_removed: str
|
|
|
|
def do(cls, *args, **kwargs):
|
|
pass
|
|
|
|
def scope(cls, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=False)
|
|
def test_not_capture_event_if_not_enabled(data_fixture):
|
|
posthog = get_posthog_client()
|
|
|
|
assert posthog.disabled is True
|
|
posthog.capture = MagicMock()
|
|
|
|
user = data_fixture.create_user()
|
|
capture_user_event(user, "test", {})
|
|
posthog.capture.assert_not_called()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
def test_capture_event_if_enabled(data_fixture):
|
|
posthog = get_posthog_client()
|
|
|
|
assert posthog.disabled is False
|
|
posthog.capture = MagicMock()
|
|
|
|
user = data_fixture.create_user()
|
|
workspace = data_fixture.create_workspace()
|
|
capture_user_event(user, "test", {}, session="session", workspace=workspace)
|
|
posthog.capture.assert_called_once_with(
|
|
distinct_id=user.id,
|
|
event="test",
|
|
properties={
|
|
"user_email": user.email,
|
|
"user_session": "session",
|
|
"workspace_id": workspace.id,
|
|
},
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
@patch("baserow.core.posthog.capture_user_event")
|
|
def test_capture_event_action_done(
|
|
mock_capture_event, data_fixture, django_capture_on_commit_callbacks
|
|
):
|
|
user = data_fixture.create_user()
|
|
|
|
def action_done():
|
|
capture_event_action_done(
|
|
sender=None,
|
|
user=user,
|
|
action_type=TestActionType(),
|
|
action_params={"must_be_kept": "yes", "must_be_removed": "yes"},
|
|
action_timestamp=None,
|
|
action_command_type=ActionCommandType.DO,
|
|
workspace=None,
|
|
session="session",
|
|
action_source="browser",
|
|
)
|
|
|
|
with django_capture_on_commit_callbacks(execute=True):
|
|
with pytest.raises(RuntimeError), transaction.atomic():
|
|
action_done()
|
|
raise RuntimeError()
|
|
mock_capture_event.assert_not_called()
|
|
|
|
with django_capture_on_commit_callbacks(execute=True):
|
|
action_done()
|
|
mock_capture_event.assert_not_called()
|
|
mock_capture_event.assert_called_once_with(
|
|
user,
|
|
"test",
|
|
{"must_be_kept": "yes", "action_source": "browser"},
|
|
workspace=None,
|
|
session="session",
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
@patch("baserow.core.posthog.capture_user_event", side_effect=RuntimeError)
|
|
def test_capture_event_action_done_failure_does_not_skip_other_callbacks(
|
|
mock_capture_event, data_fixture, django_capture_on_commit_callbacks
|
|
):
|
|
next_callback = MagicMock()
|
|
|
|
with django_capture_on_commit_callbacks(execute=True):
|
|
capture_event_action_done(
|
|
sender=None,
|
|
user=data_fixture.create_user(),
|
|
action_type=TestActionType(),
|
|
action_params={"must_be_kept": "yes", "must_be_removed": "yes"},
|
|
action_timestamp=None,
|
|
action_command_type=ActionCommandType.DO,
|
|
workspace=None,
|
|
session="session",
|
|
action_source="browser",
|
|
)
|
|
transaction.on_commit(next_callback)
|
|
|
|
mock_capture_event.assert_called_once()
|
|
next_callback.assert_called_once()
|
|
|
|
|
|
class NotCapturedActionType(TestActionType):
|
|
type = "not_captured"
|
|
capture_analytics_event = False
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
@patch("baserow.core.posthog.capture_user_event")
|
|
def test_capture_event_action_done_skips_opted_out_types(
|
|
mock_capture_event, data_fixture, django_capture_on_commit_callbacks
|
|
):
|
|
with django_capture_on_commit_callbacks(execute=True):
|
|
capture_event_action_done(
|
|
sender=None,
|
|
user=data_fixture.create_user(),
|
|
action_type=NotCapturedActionType(),
|
|
action_params={"must_be_kept": "yes"},
|
|
action_timestamp=None,
|
|
action_command_type=ActionCommandType.DO,
|
|
workspace=None,
|
|
session="session",
|
|
action_source="browser",
|
|
)
|
|
|
|
mock_capture_event.assert_not_called()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
@patch("baserow.core.posthog.capture_user_event")
|
|
def test_capture_event_action_done_sends_an_identity_without_a_person_profile(
|
|
mock_capture_event, data_fixture, django_capture_on_commit_callbacks
|
|
):
|
|
workspace = data_fixture.create_workspace()
|
|
identity = data_fixture.create_identity(workspace=workspace, name="Writer")
|
|
|
|
with django_capture_on_commit_callbacks(execute=True):
|
|
capture_event_action_done(
|
|
sender=None,
|
|
user=identity,
|
|
action_type=TestActionType(),
|
|
action_params={"must_be_kept": "yes"},
|
|
action_timestamp=None,
|
|
action_command_type=ActionCommandType.DO,
|
|
workspace=workspace,
|
|
session=None,
|
|
action_source="agent",
|
|
)
|
|
|
|
mock_capture_event.assert_called_once_with(
|
|
identity,
|
|
"test",
|
|
{"must_be_kept": "yes", "action_source": "agent"},
|
|
workspace=workspace,
|
|
session=None,
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=True)
|
|
def test_capture_event_without_a_person_profile_leaves_out_the_email(data_fixture):
|
|
posthog = get_posthog_client()
|
|
posthog.capture = MagicMock()
|
|
workspace = data_fixture.create_workspace()
|
|
identity = data_fixture.create_identity(workspace=workspace, name="Writer")
|
|
|
|
capture_user_event(identity, "test", {}, workspace=workspace)
|
|
|
|
posthog.capture.assert_called_once_with(
|
|
distinct_id=identity.id,
|
|
event="test",
|
|
properties={
|
|
"user_email": None,
|
|
"workspace_id": workspace.id,
|
|
"$process_person_profile": False,
|
|
},
|
|
)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@override_settings(POSTHOG_ENABLED=False)
|
|
@patch("baserow.core.posthog.capture_user_event")
|
|
def test_capture_event_action_done_reads_nothing_when_posthog_is_off(
|
|
mock_capture_event, data_fixture
|
|
):
|
|
action_type = MagicMock(capture_analytics_event=True)
|
|
|
|
capture_event_action_done(
|
|
sender=None,
|
|
user=data_fixture.create_user(),
|
|
action_type=action_type,
|
|
action_params={"must_be_kept": "yes"},
|
|
action_timestamp=None,
|
|
action_command_type=ActionCommandType.DO,
|
|
workspace=None,
|
|
session="session",
|
|
action_source="browser",
|
|
)
|
|
|
|
action_type.get_analytics_properties.assert_not_called()
|
|
mock_capture_event.assert_not_called()
|
|
|
|
|
|
def test_capture_event_without_person_skips_the_person_profile():
|
|
with patch("baserow.core.posthog.capture_event") as capture_event:
|
|
capture_event_without_person("agent_1", "test", {"a": 1})
|
|
|
|
capture_event.assert_called_once_with(
|
|
"agent_1", "test", {"a": 1, "$process_person_profile": False}
|
|
)
|