0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 09:16:49 +01:00

Fix recordings on funnel trends (#8621)

* fix recordings on funnel trends

* update tests

* update snapshots
This commit is contained in:
Rick Marron 2022-02-23 01:42:55 -08:00 committed by GitHub
parent 9496394952
commit 3f6c472155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 607 additions and 0 deletions

View File

@ -17,6 +17,21 @@ class ClickhouseFunnelTrendsActors(ClickhouseFunnelTrends, ActorBaseQuery):
def aggregation_group_type_index(self):
return self._filter.aggregation_group_type_index
def _get_funnel_person_step_events(self):
if self._filter.include_recordings:
# Get the event that should be used to match the recording
funnel_to_step = self._filter.funnel_to_step
is_drop_off = self._filter.drop_off
if funnel_to_step is None or is_drop_off:
# If there is no funnel_to_step or if we are looking for drop off, we need to get the users final event
return ", final_matching_events as matching_events"
else:
# Otherwise, we return the event of the funnel_to_step
self.params.update({"matching_events_step_num": funnel_to_step})
return ", step_%(matching_events_step_num)s_matching_events as matching_events"
return ""
def actor_query(self, limit_actors: Optional[bool] = True):
drop_off = self._filter.drop_off
if drop_off is None:

View File

@ -0,0 +1,496 @@
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_returns_recordings
'
SELECT aggregation_target AS actor_id,
step_1_matching_events as matching_events
FROM
(SELECT aggregation_target,
toStartOfDay(timestamp) AS entrance_period_start,
max(steps) AS steps_completed ,
groupArray(10)(step_0_matching_event) as step_0_matching_events,
groupArray(10)(step_1_matching_event) as step_1_matching_events,
groupArray(10)(step_2_matching_event) as step_2_matching_events,
groupArray(10)(final_matching_event) as final_matching_events
FROM
(SELECT *,
if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY
AND latest_1 < latest_2
AND latest_2 <= latest_0 + INTERVAL 14 DAY, 3, if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY, 2, 1)) AS steps ,
if(isNotNull(latest_1)
AND latest_1 <= latest_0 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_0), toDateTime(latest_1)), NULL) step_1_conversion_time,
if(isNotNull(latest_2)
AND latest_2 <= latest_1 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_1), toDateTime(latest_2)), NULL) step_2_conversion_time,
("latest_0",
"uuid_0",
"$session_id_0",
"$window_id_0") as step_0_matching_event,
("latest_1",
"uuid_1",
"$session_id_1",
"$window_id_1") as step_1_matching_event,
("latest_2",
"uuid_2",
"$session_id_2",
"$window_id_2") as step_2_matching_event,
if(isNull(latest_0),(null, null, null, null),if(isNull(latest_1), step_0_matching_event, if(isNull(latest_2), step_1_matching_event, step_2_matching_event))) as final_matching_event
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
if(latest_2 < latest_1, NULL, latest_2) as latest_2,
if(latest_2 < latest_1, NULL, "uuid_2") as "uuid_2",
if(latest_2 < latest_1, NULL, "$session_id_2") as "$session_id_2",
if(latest_2 < latest_1, NULL, "$window_id_2") as "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
min(latest_1) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_1,
last_value("uuid_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_1",
last_value("$session_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_1",
last_value("$window_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
if(event = 'step one', 1, 0) as step_0,
if(step_0 = 1, timestamp, null) as latest_0,
if(step_0 = 1, "uuid", null) as "uuid_0",
if(step_0 = 1, "$session_id", null) as "$session_id_0",
if(step_0 = 1, "$window_id", null) as "$window_id_0",
if(event = 'step two', 1, 0) as step_1,
if(step_1 = 1, timestamp, null) as latest_1,
if(step_1 = 1, "uuid", null) as "uuid_1",
if(step_1 = 1, "$session_id", null) as "$session_id_1",
if(step_1 = 1, "$window_id", null) as "$window_id_1",
if(event = 'step three', 1, 0) as step_2,
if(step_2 = 1, timestamp, null) as latest_2,
if(step_2 = 1, "uuid", null) as "uuid_2",
if(step_2 = 1, "$session_id", null) as "$session_id_2",
if(step_2 = 1, "$window_id", null) as "$window_id_2"
FROM
(SELECT e.event as event,
e.team_id as team_id,
e.distinct_id as distinct_id,
e.timestamp as timestamp,
pdi.person_id as aggregation_target,
e.uuid AS uuid,
e.$session_id as "$session_id",
e.$window_id as "$window_id"
FROM events e
INNER JOIN
(SELECT distinct_id,
argMax(person_id, version) as person_id
FROM person_distinct_id2
WHERE team_id = 2
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id
WHERE team_id = 2
AND event IN ['step one', 'step three', 'step two']
AND timestamp >= '2021-05-01 00:00:00'
AND timestamp <= '2021-05-07 23:59:59' ) events
WHERE (step_0 = 1
OR step_1 = 1
OR step_2 = 1) ))))
WHERE step_0 = 1 SETTINGS allow_experimental_window_functions = 1 )
WHERE toDateTime(entrance_period_start) = '2021-05-01 00:00:00'
GROUP BY aggregation_target,
entrance_period_start)
WHERE steps_completed >= 2
ORDER BY aggregation_target
LIMIT 100
OFFSET 0 SETTINGS allow_experimental_window_functions = 1
'
---
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_returns_recordings.1
'
SELECT DISTINCT session_id
FROM session_recording_events
WHERE team_id = 2
and has_full_snapshot = 1
and session_id in ['s1b']
'
---
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_with_drop_off
'
SELECT aggregation_target AS actor_id,
final_matching_events as matching_events
FROM
(SELECT aggregation_target,
toStartOfDay(timestamp) AS entrance_period_start,
max(steps) AS steps_completed ,
groupArray(10)(step_0_matching_event) as step_0_matching_events,
groupArray(10)(step_1_matching_event) as step_1_matching_events,
groupArray(10)(step_2_matching_event) as step_2_matching_events,
groupArray(10)(final_matching_event) as final_matching_events
FROM
(SELECT *,
if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY
AND latest_1 < latest_2
AND latest_2 <= latest_0 + INTERVAL 14 DAY, 3, if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY, 2, 1)) AS steps ,
if(isNotNull(latest_1)
AND latest_1 <= latest_0 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_0), toDateTime(latest_1)), NULL) step_1_conversion_time,
if(isNotNull(latest_2)
AND latest_2 <= latest_1 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_1), toDateTime(latest_2)), NULL) step_2_conversion_time,
("latest_0",
"uuid_0",
"$session_id_0",
"$window_id_0") as step_0_matching_event,
("latest_1",
"uuid_1",
"$session_id_1",
"$window_id_1") as step_1_matching_event,
("latest_2",
"uuid_2",
"$session_id_2",
"$window_id_2") as step_2_matching_event,
if(isNull(latest_0),(null, null, null, null),if(isNull(latest_1), step_0_matching_event, if(isNull(latest_2), step_1_matching_event, step_2_matching_event))) as final_matching_event
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
if(latest_2 < latest_1, NULL, latest_2) as latest_2,
if(latest_2 < latest_1, NULL, "uuid_2") as "uuid_2",
if(latest_2 < latest_1, NULL, "$session_id_2") as "$session_id_2",
if(latest_2 < latest_1, NULL, "$window_id_2") as "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
min(latest_1) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_1,
last_value("uuid_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_1",
last_value("$session_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_1",
last_value("$window_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
if(event = 'step one', 1, 0) as step_0,
if(step_0 = 1, timestamp, null) as latest_0,
if(step_0 = 1, "uuid", null) as "uuid_0",
if(step_0 = 1, "$session_id", null) as "$session_id_0",
if(step_0 = 1, "$window_id", null) as "$window_id_0",
if(event = 'step two', 1, 0) as step_1,
if(step_1 = 1, timestamp, null) as latest_1,
if(step_1 = 1, "uuid", null) as "uuid_1",
if(step_1 = 1, "$session_id", null) as "$session_id_1",
if(step_1 = 1, "$window_id", null) as "$window_id_1",
if(event = 'step three', 1, 0) as step_2,
if(step_2 = 1, timestamp, null) as latest_2,
if(step_2 = 1, "uuid", null) as "uuid_2",
if(step_2 = 1, "$session_id", null) as "$session_id_2",
if(step_2 = 1, "$window_id", null) as "$window_id_2"
FROM
(SELECT e.event as event,
e.team_id as team_id,
e.distinct_id as distinct_id,
e.timestamp as timestamp,
pdi.person_id as aggregation_target,
e.uuid AS uuid,
e.$session_id as "$session_id",
e.$window_id as "$window_id"
FROM events e
INNER JOIN
(SELECT distinct_id,
argMax(person_id, version) as person_id
FROM person_distinct_id2
WHERE team_id = 2
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id
WHERE team_id = 2
AND event IN ['step one', 'step three', 'step two']
AND timestamp >= '2021-05-01 00:00:00'
AND timestamp <= '2021-05-07 23:59:59' ) events
WHERE (step_0 = 1
OR step_1 = 1
OR step_2 = 1) ))))
WHERE step_0 = 1 SETTINGS allow_experimental_window_functions = 1 )
WHERE toDateTime(entrance_period_start) = '2021-05-01 00:00:00'
GROUP BY aggregation_target,
entrance_period_start)
WHERE steps_completed >= 1
AND steps_completed < 3
ORDER BY aggregation_target
LIMIT 100
OFFSET 0 SETTINGS allow_experimental_window_functions = 1
'
---
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_with_drop_off.1
'
SELECT DISTINCT session_id
FROM session_recording_events
WHERE team_id = 2
and has_full_snapshot = 1
and session_id in ['s1a']
'
---
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_with_no_to_step
'
SELECT aggregation_target AS actor_id,
final_matching_events as matching_events
FROM
(SELECT aggregation_target,
toStartOfDay(timestamp) AS entrance_period_start,
max(steps) AS steps_completed ,
groupArray(10)(step_0_matching_event) as step_0_matching_events,
groupArray(10)(step_1_matching_event) as step_1_matching_events,
groupArray(10)(step_2_matching_event) as step_2_matching_events,
groupArray(10)(final_matching_event) as final_matching_events
FROM
(SELECT *,
if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY
AND latest_1 < latest_2
AND latest_2 <= latest_0 + INTERVAL 14 DAY, 3, if(latest_0 < latest_1
AND latest_1 <= latest_0 + INTERVAL 14 DAY, 2, 1)) AS steps ,
if(isNotNull(latest_1)
AND latest_1 <= latest_0 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_0), toDateTime(latest_1)), NULL) step_1_conversion_time,
if(isNotNull(latest_2)
AND latest_2 <= latest_1 + INTERVAL 14 DAY, dateDiff('second', toDateTime(latest_1), toDateTime(latest_2)), NULL) step_2_conversion_time,
("latest_0",
"uuid_0",
"$session_id_0",
"$window_id_0") as step_0_matching_event,
("latest_1",
"uuid_1",
"$session_id_1",
"$window_id_1") as step_1_matching_event,
("latest_2",
"uuid_2",
"$session_id_2",
"$window_id_2") as step_2_matching_event,
if(isNull(latest_0),(null, null, null, null),if(isNull(latest_1), step_0_matching_event, if(isNull(latest_2), step_1_matching_event, step_2_matching_event))) as final_matching_event
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
latest_1,
"uuid_1",
"$session_id_1",
"$window_id_1",
step_2,
if(latest_2 < latest_1, NULL, latest_2) as latest_2,
if(latest_2 < latest_1, NULL, "uuid_2") as "uuid_2",
if(latest_2 < latest_1, NULL, "$session_id_2") as "$session_id_2",
if(latest_2 < latest_1, NULL, "$window_id_2") as "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
step_0,
latest_0,
"uuid_0",
"$session_id_0",
"$window_id_0",
step_1,
min(latest_1) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_1,
last_value("uuid_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_1",
last_value("$session_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_1",
last_value("$window_id_1") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_1",
step_2,
min(latest_2) over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) latest_2,
last_value("uuid_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "uuid_2",
last_value("$session_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$session_id_2",
last_value("$window_id_2") over (PARTITION by aggregation_target
ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING) "$window_id_2"
FROM
(SELECT aggregation_target,
timestamp,
if(event = 'step one', 1, 0) as step_0,
if(step_0 = 1, timestamp, null) as latest_0,
if(step_0 = 1, "uuid", null) as "uuid_0",
if(step_0 = 1, "$session_id", null) as "$session_id_0",
if(step_0 = 1, "$window_id", null) as "$window_id_0",
if(event = 'step two', 1, 0) as step_1,
if(step_1 = 1, timestamp, null) as latest_1,
if(step_1 = 1, "uuid", null) as "uuid_1",
if(step_1 = 1, "$session_id", null) as "$session_id_1",
if(step_1 = 1, "$window_id", null) as "$window_id_1",
if(event = 'step three', 1, 0) as step_2,
if(step_2 = 1, timestamp, null) as latest_2,
if(step_2 = 1, "uuid", null) as "uuid_2",
if(step_2 = 1, "$session_id", null) as "$session_id_2",
if(step_2 = 1, "$window_id", null) as "$window_id_2"
FROM
(SELECT e.event as event,
e.team_id as team_id,
e.distinct_id as distinct_id,
e.timestamp as timestamp,
pdi.person_id as aggregation_target,
e.uuid AS uuid,
e.$session_id as "$session_id",
e.$window_id as "$window_id"
FROM events e
INNER JOIN
(SELECT distinct_id,
argMax(person_id, version) as person_id
FROM person_distinct_id2
WHERE team_id = 2
GROUP BY distinct_id
HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id
WHERE team_id = 2
AND event IN ['step one', 'step three', 'step two']
AND timestamp >= '2021-05-01 00:00:00'
AND timestamp <= '2021-05-07 23:59:59' ) events
WHERE (step_0 = 1
OR step_1 = 1
OR step_2 = 1) ))))
WHERE step_0 = 1 SETTINGS allow_experimental_window_functions = 1 )
WHERE toDateTime(entrance_period_start) = '2021-05-01 00:00:00'
GROUP BY aggregation_target,
entrance_period_start)
WHERE steps_completed >= 3
ORDER BY aggregation_target
LIMIT 100
OFFSET 0 SETTINGS allow_experimental_window_functions = 1
'
---
# name: TestFunnelTrendsPersons.test_funnel_trend_persons_with_no_to_step.1
'
SELECT DISTINCT session_id
FROM session_recording_events
WHERE team_id = 2
and has_full_snapshot = 1
and session_id in ['s1c']
'
---

View File

@ -0,0 +1,96 @@
from datetime import datetime, timedelta
from uuid import uuid4
from django.utils import timezone
from ee.clickhouse.models.session_recording_event import create_session_recording_event
from ee.clickhouse.queries.funnels.funnel_trends_persons import ClickhouseFunnelTrendsActors
from ee.clickhouse.test.test_journeys import journeys_for
from ee.clickhouse.util import ClickhouseTestMixin, snapshot_clickhouse_queries
from posthog.constants import INSIGHT_FUNNELS, FunnelVizType
from posthog.models.filters import Filter
from posthog.test.base import APIBaseTest
def _create_session_recording_event(team_id, distinct_id, session_id, timestamp, window_id="", has_full_snapshot=True):
create_session_recording_event(
uuid=uuid4(),
team_id=team_id,
distinct_id=distinct_id,
timestamp=timestamp,
session_id=session_id,
window_id=window_id,
snapshot_data={"timestamp": timestamp.timestamp(), "has_full_snapshot": has_full_snapshot,},
)
filter_data = {
"insight": INSIGHT_FUNNELS,
"funnel_viz_type": FunnelVizType.TRENDS,
"interval": "day",
"date_from": "2021-05-01 00:00:00",
"date_to": "2021-05-07 23:59:59",
"funnel_window_days": 14,
"funnel_from_step": 0,
"entrance_period_start": "2021-05-01 00:00:00",
"drop_off": False,
"events": [{"id": "step one", "order": 0}, {"id": "step two", "order": 1}, {"id": "step three", "order": 2},],
"include_recordings": "true",
}
class TestFunnelTrendsPersons(ClickhouseTestMixin, APIBaseTest):
@snapshot_clickhouse_queries
def test_funnel_trend_persons_returns_recordings(self):
persons = journeys_for(
{
"user_one": [
{"event": "step one", "timestamp": datetime(2021, 5, 1), "properties": {"$session_id": "s1a"}},
{"event": "step two", "timestamp": datetime(2021, 5, 2), "properties": {"$session_id": "s1b"}},
{"event": "step three", "timestamp": datetime(2021, 5, 3), "properties": {"$session_id": "s1c"}},
],
},
self.team,
)
_create_session_recording_event(self.team.pk, "user_one", "s1b", timezone.now() + timedelta(days=1))
filter = Filter(data={"funnel_to_step": 1, **filter_data})
_, results = ClickhouseFunnelTrendsActors(filter, self.team).get_actors()
self.assertEqual([person["id"] for person in results], [persons["user_one"].uuid])
self.assertEqual([person["matched_recordings"][0]["session_id"] for person in results], ["s1b"])
@snapshot_clickhouse_queries
def test_funnel_trend_persons_with_no_to_step(self):
persons = journeys_for(
{
"user_one": [
{"event": "step one", "timestamp": datetime(2021, 5, 1), "properties": {"$session_id": "s1a"}},
{"event": "step two", "timestamp": datetime(2021, 5, 2), "properties": {"$session_id": "s1b"}},
{"event": "step three", "timestamp": datetime(2021, 5, 3), "properties": {"$session_id": "s1c"}},
],
},
self.team,
)
_create_session_recording_event(self.team.pk, "user_one", "s1c", timezone.now() + timedelta(days=1))
filter = Filter(data=filter_data)
_, results = ClickhouseFunnelTrendsActors(filter, self.team).get_actors()
self.assertEqual([person["id"] for person in results], [persons["user_one"].uuid])
self.assertEqual([person["matched_recordings"][0]["session_id"] for person in results], ["s1c"])
@snapshot_clickhouse_queries
def test_funnel_trend_persons_with_drop_off(self):
persons = journeys_for(
{
"user_one": [
{"event": "step one", "timestamp": datetime(2021, 5, 1), "properties": {"$session_id": "s1a"}},
],
},
self.team,
)
_create_session_recording_event(self.team.pk, "user_one", "s1a", timezone.now() + timedelta(days=1))
filter = Filter(data={**filter_data, "drop_off": True})
_, results = ClickhouseFunnelTrendsActors(filter, self.team).get_actors()
self.assertEqual([person["id"] for person in results], [persons["user_one"].uuid])
self.assertEqual([person["matched_recordings"][0].get("session_id") for person in results], ["s1a"])