Skip to content

Commit

Permalink
fix: Default event name (#19335)
Browse files Browse the repository at this point in the history
  • Loading branch information
timgl authored Dec 15, 2023
1 parent 48632bc commit ea40b68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions posthog/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,16 @@ def test_normal_date(self):

class TestDefaultEventName(BaseTest):
def test_no_events(self):
self.assertEqual(get_default_event_name(), "$pageview")
self.assertEqual(get_default_event_name(self.team), "$pageview")

def test_take_screen(self):
EventDefinition.objects.create(name="$screen", team=self.team)
self.assertEqual(get_default_event_name(), "$screen")
self.assertEqual(get_default_event_name(self.team), "$screen")

def test_prefer_pageview(self):
EventDefinition.objects.create(name="$pageview", team=self.team)
EventDefinition.objects.create(name="$screen", team=self.team)
self.assertEqual(get_default_event_name(), "$pageview")
self.assertEqual(get_default_event_name(self.team), "$pageview")


class TestLoadDataFromRequest(TestCase):
Expand Down
9 changes: 5 additions & 4 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def render_template(
"current_user": None,
"current_team": None,
"preflight": json.loads(preflight_check(request).getvalue()),
"default_event_name": get_default_event_name(),
"default_event_name": "$pageview",
"switched_team": getattr(request, "switched_team", None),
**posthog_app_context,
}
Expand Down Expand Up @@ -399,6 +399,7 @@ def render_template(
)
posthog_app_context["current_team"] = team_serialized.data
posthog_app_context["frontend_apps"] = get_frontend_apps(user.team.pk)
posthog_app_context["default_event_name"] = get_default_event_name(user.team)

context["posthog_app_context"] = json.dumps(posthog_app_context, default=json_uuid_convert)

Expand Down Expand Up @@ -456,12 +457,12 @@ def get_self_capture_api_token(request: Optional[HttpRequest]) -> Optional[str]:
return None


def get_default_event_name():
def get_default_event_name(team: "Team"):
from posthog.models import EventDefinition

if EventDefinition.objects.filter(name="$pageview").exists():
if EventDefinition.objects.filter(team=team, name="$pageview").exists():
return "$pageview"
elif EventDefinition.objects.filter(name="$screen").exists():
elif EventDefinition.objects.filter(team=team, name="$screen").exists():
return "$screen"
return "$pageview"

Expand Down

0 comments on commit ea40b68

Please sign in to comment.