def track_lifecycle_event_property_filter_materialized(self): filter = Filter( data={ "insight": "LIFECYCLE", "events": [{"id": "$pageview", "type": "events"}], "interval": "week", "shown_as": "Lifecycle", "date_from": "-14d", "properties": [ { "key": "$host", "operator": "is_not", "value": [ "localhost:8000", "localhost:5000", "127.0.0.1:8000", "127.0.0.1:3000", "localhost:3000", ], } ], **DATE_RANGE, }, team=self.team, ) Trends().run(filter, self.team)
def track_trends_person_property_breakdown(self): filter = Filter( data={"events": [{"id": "$pageview"}], "breakdown": "$browser", "breakdown_type": "person", **DATE_RANGE,} ) with no_materialized_columns(): Trends().run(filter, self.team)
def track_trends_filter_by_action_current_url_materialized(self): action = Action.objects.create(team=self.team, name="docs view") ActionStep.objects.create( action=action, event="$pageview", url="docs", url_matching="contains", ) filter = Filter(data={"actions": [{"id": action.id}], **DATE_RANGE}, team=self.team) Trends().run(filter, self.team)
def track_trends_filter_by_action_with_person_filters_materialized(self): action = Action.objects.create(team=self.team, name=".com-users page views") ActionStep.objects.create( action=action, event="$pageview", properties=[{"key": "email", "operator": "icontains", "value": ".com", "type": "person"}], ) filter = Filter(data={"actions": [{"id": action.id}], **DATE_RANGE}, team=self.team) Trends().run(filter, self.team)
def track_trends_dau_person_property_filter_materialized(self): filter = Filter( data={ "events": [{"id": "$pageview", "math": "dau"}], "properties": [{"key": "email", "operator": "icontains", "value": ".com", "type": "person"}], **DATE_RANGE, } ) Trends().run(filter, self.team)
def test_breakdown_by_group_props(self): self._create_groups() journey = { "person1": [ { "event": "sign up", "timestamp": datetime(2020, 1, 2, 12), "properties": {"$group_0": "org:5"}, "group0_properties": {"industry": "finance"}, }, { "event": "sign up", "timestamp": datetime(2020, 1, 2, 13), "properties": {"$group_0": "org:6"}, "group0_properties": {"industry": "technology"}, }, { "event": "sign up", "timestamp": datetime(2020, 1, 2, 15), "properties": {"$group_0": "org:7", "$group_1": "company:10"}, "group0_properties": {"industry": "finance"}, "group1_properties": {"industry": "finance"}, }, ], } journeys_for(events_by_person=journey, team=self.team) filter = Filter( data={ "date_from": "2020-01-01T00:00:00Z", "date_to": "2020-01-12", "breakdown": "industry", "breakdown_type": "group", "breakdown_group_type_index": 0, "events": [{"id": "sign up", "name": "sign up", "type": "events", "order": 0,}], } ) response = Trends().run(filter, self.team,) self.assertEqual(len(response), 2) self.assertEqual(response[0]["breakdown_value"], "finance") self.assertEqual(response[0]["count"], 2) self.assertEqual(response[1]["breakdown_value"], "technology") self.assertEqual(response[1]["count"], 1) filter = filter.with_data( {"breakdown_value": "technology", "date_from": "2020-01-02T00:00:00Z", "date_to": "2020-01-03"} ) entity = Entity({"id": "sign up", "name": "sign up", "type": "events", "order": 0,}) res = self._get_trend_people(filter, entity) self.assertEqual(res[0]["distinct_ids"], ["person1"])
def test_test_account_filters_with_groups(self): self.team.test_account_filters = [ {"key": "key", "type": "group", "value": "value", "group_type_index": 0}, ] self.team.save() GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) create_group(self.team.pk, group_type_index=0, group_key="in", properties={"key": "value"}) create_group(self.team.pk, group_type_index=0, group_key="out", properties={"key": "othervalue"}) with freeze_time("2020-01-11T12:00:00Z"): Person.objects.create(distinct_ids=["person1"], team_id=self.team.pk) with freeze_time("2020-01-09T12:00:00Z"): Person.objects.create(distinct_ids=["person2"], team_id=self.team.pk) journeys_for( { "person1": [ {"event": "$pageview", "timestamp": datetime(2020, 1, 11, 12), "properties": {"$group_0": "out"},}, ], "person2": [ {"event": "$pageview", "timestamp": datetime(2020, 1, 9, 12), "properties": {"$group_0": "in"},}, {"event": "$pageview", "timestamp": datetime(2020, 1, 12, 12), "properties": {"$group_0": "in"},}, {"event": "$pageview", "timestamp": datetime(2020, 1, 15, 12), "properties": {"$group_0": "in"},}, ], }, self.team, ) result = Trends().run( Filter( data={ "date_from": "2020-01-12T00:00:00Z", "date_to": "2020-01-19T00:00:00Z", "events": [{"id": "$pageview", "type": "events", "order": 0}], "shown_as": TRENDS_LIFECYCLE, FILTER_TEST_ACCOUNTS: True, }, team=self.team, ), self.team, ) self.assertLifecycleResults( result, [ {"status": "dormant", "data": [0, -1, 0, 0, -1, 0, 0, 0]}, {"status": "new", "data": [0, 0, 0, 0, 0, 0, 0, 0]}, {"status": "resurrecting", "data": [1, 0, 0, 1, 0, 0, 0, 0]}, {"status": "returning", "data": [0, 0, 0, 0, 0, 0, 0, 0]}, ], )
def track_trends_filter_by_cohort_precalculated(self): self.cohort.last_calculation = now() self.cohort.save() filter = Filter( data={ "events": [{"id": "$pageview"}], "properties": [{"key": "id", "value": self.cohort.pk, "type": "cohort"}], **DATE_RANGE, }, team=self.team, ) Trends().run(filter, self.team)
def get_results(self): if self.query_filter.insight == INSIGHT_TRENDS: trend_results = Trends().run(self.query_filter, self.team) variants = self.get_trend_count_data_for_variants(trend_results) elif self.query_filter.insight == INSIGHT_FUNNELS: funnel_results = ClickhouseFunnel(self.query_filter, self.team).run() variants = self.get_funnel_conversion_rate_for_variants(funnel_results) else: raise ValidationError("Secondary metrics need to be funnel or trend insights") return {"result": variants}
def track_lifecycle(self): filter = Filter( data={ "insight": "LIFECYCLE", "events": [{"id": "$pageview", "type": "events"}], "interval": "week", "shown_as": "Lifecycle", "date_from": "-14d", **DATE_RANGE, }, team=self.team, ) Trends().run(filter, self.team)
def track_lifecycle_person_property_filter_materialized(self): filter = Filter( data={ "insight": "LIFECYCLE", "events": [{"id": "$pageview", "type": "events"}], "interval": "week", "shown_as": "Lifecycle", "date_from": "-14d", "properties": [{"key": "email", "operator": "icontains", "value": ".com", "type": "person"}], **DATE_RANGE, }, team=self.team, ) Trends().run(filter, self.team)
def track_trends_filter_by_cohort(self): self.cohort.last_calculation = None self.cohort.save() filter = Filter( data={ "events": [{"id": "$pageview"}], "properties": [{"key": "id", "value": self.cohort.pk, "type": "cohort"}], **DATE_RANGE, }, team=self.team, ) with no_materialized_columns(): Trends().run(filter, self.team)
def test_filtering_with_group_props(self): self._create_groups() Person.objects.create(team_id=self.team.pk, distinct_ids=["person1"], properties={"key": "value"}) _create_event( event="$pageview", distinct_id="person1", team=self.team, timestamp="2020-01-02T12:00:00Z", ) _create_event( event="$pageview", distinct_id="person1", team=self.team, properties={"$group_0": "org:5"}, timestamp="2020-01-02T12:00:00Z", ) _create_event( event="$pageview", distinct_id="person1", team=self.team, properties={"$group_0": "org:6"}, timestamp="2020-01-02T12:00:00Z", ) _create_event( event="$pageview", distinct_id="person1", team=self.team, properties={"$group_0": "org:6", "$group_1": "company:10"}, timestamp="2020-01-02T12:00:00Z", ) filter = Filter( { "date_from": "2020-01-01T00:00:00Z", "date_to": "2020-01-12T00:00:00Z", "events": [{"id": "$pageview", "type": "events", "order": 0}], "properties": [ {"key": "industry", "value": "finance", "type": "group", "group_type_index": 0}, {"key": "key", "value": "value", "type": "person"}, ], }, team=self.team, ) response = Trends().run(filter, self.team) self.assertEqual(response[0]["count"], 1)
def test_breakdown_with_filter_groups(self): self._create_groups() _create_event( event="sign up", distinct_id="person1", team=self.team, properties={"key": "oh", "$group_0": "org:7", "$group_1": "company:10"}, timestamp="2020-01-02T12:00:00Z", ) _create_event( event="sign up", distinct_id="person1", team=self.team, properties={"key": "uh", "$group_0": "org:5"}, timestamp="2020-01-02T12:00:01Z", ) _create_event( event="sign up", distinct_id="person1", team=self.team, properties={"key": "uh", "$group_0": "org:6"}, timestamp="2020-01-02T12:00:02Z", ) response = Trends().run( Filter( data={ "date_from": "2020-01-01T00:00:00Z", "date_to": "2020-01-12T00:00:00Z", "breakdown": "key", "events": [{"id": "sign up", "name": "sign up", "type": "events", "order": 0,}], "properties": [{"key": "industry", "value": "finance", "type": "group", "group_type_index": 0}], } ), self.team, ) self.assertEqual(len(response), 2) self.assertEqual(response[0]["breakdown_value"], "oh") self.assertEqual(response[0]["count"], 1) self.assertEqual(response[1]["breakdown_value"], "uh") self.assertEqual(response[1]["count"], 1)
def test_breakdown_by_group_props_with_person_filter(self): self._create_groups() Person.objects.create(team_id=self.team.pk, distinct_ids=["person1"], properties={"key": "value"}) _create_event( event="sign up", distinct_id="person1", team=self.team, properties={"$group_0": "org:5"}, timestamp="2020-01-02T12:00:00Z", person_properties={"key": "value"}, group0_properties={"industry": "finance"}, ) _create_event( event="sign up", distinct_id="person2", team=self.team, properties={"$group_0": "org:6"}, timestamp="2020-01-02T12:00:00Z", person_properties={}, group0_properties={"industry": "technology"}, ) filter = Filter( data={ "date_from": "2020-01-01T00:00:00Z", "date_to": "2020-01-12T00:00:00Z", "breakdown": "industry", "breakdown_type": "group", "breakdown_group_type_index": 0, "events": [{"id": "sign up", "name": "sign up", "type": "events", "order": 0,}], "properties": [{"key": "key", "value": "value", "type": "person"}], } ) response = Trends().run(filter, self.team,) self.assertEqual(len(response), 1) self.assertEqual(response[0]["breakdown_value"], "finance") self.assertEqual(response[0]["count"], 1)
def track_trends_event_property_filter_materialized(self): filter = Filter( data={ "events": [{"id": "$pageview"}], "properties": [ { "key": "$host", "operator": "is_not", "value": [ "localhost:8000", "localhost:5000", "127.0.0.1:8000", "127.0.0.1:3000", "localhost:3000", ], } ], **DATE_RANGE, } ) Trends().run(filter, self.team)
def test_lifecycle_edge_cases(self): # This test tests behavior when created_at is different from first matching event and dormant/resurrecting/returning logic with freeze_time("2020-01-11T12:00:00Z"): Person.objects.create(distinct_ids=["person1"], team_id=self.team.pk) journeys_for( { "person1": [ {"event": "$pageview", "timestamp": datetime(2020, 1, 12, 12),}, {"event": "$pageview", "timestamp": datetime(2020, 1, 13, 12),}, {"event": "$pageview", "timestamp": datetime(2020, 1, 15, 12),}, {"event": "$pageview", "timestamp": datetime(2020, 1, 16, 12),}, ], }, self.team, ) result = Trends().run( Filter( data={ "date_from": "2020-01-11T00:00:00Z", "date_to": "2020-01-18T00:00:00Z", "events": [{"id": "$pageview", "type": "events", "order": 0}], "shown_as": TRENDS_LIFECYCLE, }, team=self.team, ), self.team, ) self.assertLifecycleResults( result, [ {"status": "dormant", "data": [0, 0, 0, -1, 0, 0, -1, 0]}, {"status": "new", "data": [0, 0, 0, 0, 0, 0, 0, 0]}, {"status": "resurrecting", "data": [0, 1, 0, 0, 1, 0, 0, 0]}, {"status": "returning", "data": [0, 0, 1, 0, 0, 1, 0, 0]}, ], )
def track_trends_no_filter(self): filter = Filter(data={"events": [{"id": "$pageview"}], **DATE_RANGE}) Trends().run(filter, self.team)
def _run_lifecycle(self, data): filter = Filter( data={"events": [{"id": "$pageview", "type": "events", "order": 0}], "shown_as": TRENDS_LIFECYCLE, **data,}, team=self.team, ) return Trends().run(filter, self.team,)
def track_trends_event_property_breakdown_materialized(self): filter = Filter(data={"events": [{"id": "$pageview"}], "breakdown": "$host", **DATE_RANGE,}) Trends().run(filter, self.team)
def track_trends_dau(self): filter = Filter(data={"events": [{"id": "$pageview", "math": "dau"}], **DATE_RANGE,}) Trends().run(filter, self.team)