Skip to content

Commit

Permalink
feat(web-analytics): Support diff on Web Goals dashboard
Browse files Browse the repository at this point in the history
We were not computing diff for the web goals section of our dashboard. This wasn't detected earlier because we don't have any actions on our local setup - we should change that.

Besides adding diff, we're also now properly formatting it in the UI, it was previously using the old formatting, therefore percentages were not accurate.
  • Loading branch information
rafaeelaudibert committed Dec 18, 2024
1 parent c12b6fd commit b452af2
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 168 deletions.
98 changes: 52 additions & 46 deletions posthog/hogql_queries/web_analytics/test/test_web_goals.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from datetime import datetime
from typing import Optional

from freezegun import freeze_time

from posthog.hogql_queries.web_analytics.web_goals import WebGoalsQueryRunner
from posthog.models import Action, Person, Element
from posthog.models.utils import uuid7
from posthog.schema import (
CompareFilter,
DateRange,
SessionTableVersion,
HogQLQueryModifiers,
Expand All @@ -21,29 +21,7 @@


class TestWebGoalsQueryRunner(ClickhouseTestMixin, APIBaseTest):
def _create_events(self, data, event="$pageview"):
person_result = []
for id, timestamps in data:
with freeze_time(timestamps[0][0]):
person_result.append(
_create_person(
team_id=self.team.pk,
distinct_ids=[id],
properties={
"name": id,
**({"email": "[email protected]"} if id == "test" else {}),
},
)
)
for timestamp, session_id, pathname in timestamps:
_create_event(
team=self.team,
event=event,
distinct_id=id,
timestamp=timestamp,
properties={"$session_id": session_id, "$pathname": pathname},
)
return person_result
TIMESTAMP = "2024-12-01"

def _create_person(self):
distinct_id = str(uuid7())
Expand All @@ -70,6 +48,7 @@ def _visit_web_analytics(self, person: Person, session_id: Optional[str] = None)
team=self.team,
event="$pageview",
distinct_id=person.uuid,
timestamp=self.TIMESTAMP,
properties={
"$pathname": "/project/2/web",
"$current_url": "https://us.posthog.com/project/2/web",
Expand All @@ -82,6 +61,7 @@ def _click_pay(self, person: Person, session_id: Optional[str] = None):
team=self.team,
event="$autocapture",
distinct_id=person.uuid,
timestamp=self.TIMESTAMP,
elements=[Element(nth_of_type=1, nth_child=0, tag_name="button", text="Pay $10")],
properties={"$session_id": session_id or person.uuid},
)
Expand Down Expand Up @@ -130,6 +110,7 @@ def _run_web_goals_query(
limit=None,
path_cleaning_filters=None,
properties=None,
compare=True,
session_table_version: SessionTableVersion = SessionTableVersion.V2,
filter_test_accounts: Optional[bool] = False,
):
Expand All @@ -139,59 +120,84 @@ def _run_web_goals_query(
properties=properties or [],
limit=limit,
filterTestAccounts=filter_test_accounts,
compareFilter=CompareFilter(compare=compare),
)
self.team.path_cleaning_filters = path_cleaning_filters or []
runner = WebGoalsQueryRunner(team=self.team, query=query, modifiers=modifiers)
return runner.calculate()

def test_no_crash_when_no_data_or_actions(self):
results = self._run_web_goals_query("all", None).results
results = self._run_web_goals_query("2024-11-01", None).results
assert results == []

def test_no_crash_when_no_data_and_some_actions(self):
self._create_actions()
results = self._run_web_goals_query("all", None).results
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", 0, 0, None],
["Visited Web Analytics", 0, 0, None],
["Clicked Pay", 0, 0, None],
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (0, 0), (0, 0), (0, 0)],
["Clicked Pay", (0, 0), (0, 0), (0, 0)],
]

def test_no_comparison(self):
self._create_actions()
p1, s1 = self._create_person()
self._visit_web_analytics(p1, s1)

results = self._run_web_goals_query("2024-11-01", None, compare=False).results
assert results == [
["Contacted Sales", (0, None), (0, None), (0, None)],
["Visited Web Analytics", (1, None), (1, None), (1, None)],
["Clicked Pay", (0, None), (0, None), (0, None)],
]

def test_one_user_one_action(self):
self._create_actions()
p1, s1 = self._create_person()
self._visit_web_analytics(p1, s1)
results = self._run_web_goals_query("all", None).results
assert results == [["Contacted Sales", 0, 0, 0], ["Visited Web Analytics", 1, 1, 1], ["Clicked Pay", 0, 0, 0]]
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (1, 0), (1, 0), (1, 0)],
["Clicked Pay", (0, 0), (0, 0), (0, 0)],
]

def test_one_user_two_similar_actions_across_sessions(self):
self._create_actions()
p1, s1 = self._create_person()
self._visit_web_analytics(p1, s1)
s2 = str(uuid7())
self._visit_web_analytics(p1, s2)
results = self._run_web_goals_query("all", None).results
assert results == [["Contacted Sales", 0, 0, 0], ["Visited Web Analytics", 1, 2, 1], ["Clicked Pay", 0, 0, 0]]
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (1, 0), (2, 0), (1, 0)],
["Clicked Pay", (0, 0), (0, 0), (0, 0)],
]

def test_one_user_two_different_actions(self):
self._create_actions()
p1, s1 = self._create_person()
self._visit_web_analytics(p1, s1)
self._click_pay(p1, s1)
results = self._run_web_goals_query("all", None).results
assert results == [["Contacted Sales", 0, 0, 0], ["Visited Web Analytics", 1, 1, 1], ["Clicked Pay", 1, 1, 1]]
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (1, 0), (1, 0), (1, 0)],
["Clicked Pay", (1, 0), (1, 0), (1, 0)],
]

def test_one_users_one_action_each(self):
self._create_actions()
p1, s1 = self._create_person()
p2, s2 = self._create_person()
self._visit_web_analytics(p1, s1)
self._click_pay(p2, s2)
results = self._run_web_goals_query("all", None).results
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", 0, 0, 0],
["Visited Web Analytics", 1, 1, 0.5],
["Clicked Pay", 1, 1, 0.5],
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (1, 0), (1, 0), (0.5, 0)],
["Clicked Pay", (1, 0), (1, 0), (0.5, 0)],
]

def test_many_users_and_actions(self):
Expand All @@ -217,11 +223,11 @@ def test_many_users_and_actions(self):
self._click_pay(p, s)
self._click_pay(p, s)

results = self._run_web_goals_query("all", None).results
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", 0, 0, 0],
["Visited Web Analytics", 11, 12, 11 / 15],
["Clicked Pay", 7, 8, 7 / 15],
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (11, 0), (12, 0), (11 / 15, 0)],
["Clicked Pay", (7, 0), (8, 0), (7 / 15, 0)],
]

def test_dont_show_deleted_actions(self):
Expand All @@ -231,8 +237,8 @@ def test_dont_show_deleted_actions(self):
self._visit_web_analytics(p1, s1)
self._click_pay(p2, s2)
actions[0].delete()
results = self._run_web_goals_query("all", None).results
results = self._run_web_goals_query("2024-11-01", None).results
assert results == [
["Contacted Sales", 0, 0, 0],
["Visited Web Analytics", 1, 1, 0.5],
["Contacted Sales", (0, 0), (0, 0), (0, 0)],
["Visited Web Analytics", (1, 0), (1, 0), (0.5, 0)],
]
Loading

0 comments on commit b452af2

Please sign in to comment.