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 test_retrieve_group(self): create_group( team_id=self.team.pk, group_type_index=0, group_key="key", properties={ "industry": "finance", "name": "Mr. Krabs" }, ) create_group( team_id=self.team.pk, group_type_index=1, group_key="foo//bar", properties={}, ) fail_response = self.client.get( f"/api/projects/{self.team.id}/groups/find?group_type_index=1&group_key=key" ) self.assertEqual(fail_response.status_code, 404) ok_response = self.client.get( f"/api/projects/{self.team.id}/groups/find?group_type_index=0&group_key=key" ) self.assertEqual(ok_response.status_code, 200) self.assertEqual( ok_response.json(), { "created_at": "2021-05-02T00:00:00Z", "group_key": "key", "group_properties": { "industry": "finance", "name": "Mr. Krabs" }, "group_type_index": 0, }, ) ok_response = self.client.get( f"/api/projects/{self.team.id}/groups/find?group_type_index=1&group_key=foo//bar" ) self.assertEqual(ok_response.status_code, 200) self.assertEqual( ok_response.json(), { "created_at": "2021-05-02T00:00:00Z", "group_key": "foo//bar", "group_properties": {}, "group_type_index": 1, }, )
def _create_groups(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:2", properties={})
def test_unordered_funnel_with_groups(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:2", properties={}) events_by_person = { "user_1": [ {"event": "user signed up", "timestamp": datetime(2020, 1, 3, 14), "properties": {"$group_0": "org:5"}}, { # same person, different group, so should count as different step 1 in funnel "event": "user signed up", "timestamp": datetime(2020, 1, 10, 14), "properties": {"$group_0": "org:6"}, }, ], "user_2": [ { # different person, same group, so should count as step two in funnel "event": "paid", "timestamp": datetime(2020, 1, 2, 14), "properties": {"$group_0": "org:5"}, } ], } journeys_for(events_by_person, self.team) params = FunnelRequest( events=json.dumps( [ EventPattern(id="user signed up", type="events", order=0), EventPattern(id="paid", type="events", order=1), ] ), date_from="2020-01-01", date_to="2020-01-14", aggregation_group_type_index=0, funnel_order_type="unordered", insight=INSIGHT_FUNNELS, ) result = get_funnel_ok(self.client, self.team.pk, params) assert result["user signed up"]["count"] == 2 assert result["paid"]["count"] == 1 assert result["paid"]["average_conversion_time"] == 86400 actors = get_funnel_actors_ok(self.client, result["user signed up"]["converted_people_url"]) assert len(actors) == 2
def test_groups_list(self): with freeze_time("2021-05-01"): create_group( team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={ "industry": "finance", "name": "Mr. Krabs" }, ) with freeze_time("2021-05-02"): create_group( team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}, ) create_group( team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={"name": "Plankton"}, ) response = self.client.get( f"/api/projects/{self.team.id}/groups?group_type_index=0").json() self.assertEqual( response, { "next": None, "previous": None, "results": [ { "created_at": "2021-05-02T00:00:00Z", "group_key": "org:6", "group_properties": { "industry": "technology" }, "group_type_index": 0, }, { "created_at": "2021-05-01T00:00:00Z", "group_key": "org:5", "group_properties": { "industry": "finance", "name": "Mr. Krabs" }, "group_type_index": 0, }, ], }, )
def test_property_definitions(self): create_group( team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={ "industry": "finance", "name": "Mr. Krabs" }, ) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={"name": "Plankton"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:2", properties={}) response = self.client.get( f"/api/projects/{self.team.id}/groups/property_definitions").json( ) self.assertEqual( response, { "0": [{ "name": "industry", "count": 2 }, { "name": "name", "count": 1 }], "1": [{ "name": "name", "count": 1 }], }, )
def test_empty_property_values(self): create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="org:1", properties={"industry": "finance"}) response = self.client.get( f"/api/projects/{self.team.id}/groups/property_values/?key=name&group_type_index=0" ).json() self.assertEqual(len(response), 0) self.assertEqual(response, [])
def test_aggregate_by_groups(self): create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:0", properties={"industry": "technology"} ) create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:1", properties={"industry": "agriculture"} ) create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:2", properties={"industry": "technology"} ) self._create_multiple_people( period=timedelta(weeks=1), event_properties=lambda i: {"$group_0": f"org:{i // 2}"}, ) with freeze_time("2020-02-15T13:01:01Z"): data = get_stickiness_time_series_ok( client=self.client, team=self.team, request={ "shown_as": "Stickiness", "date_from": "2020-01-01", "date_to": "2020-02-15", "events": [{"id": "watched movie", "math": "unique_group", "math_group_type_index": 0}], "interval": "week", }, ) assert data["watched movie"][1].value == 2 assert data["watched movie"][2].value == 0 assert data["watched movie"][3].value == 1 with freeze_time("2020-02-15T13:01:01Z"): week1_actors = get_people_from_url_ok(self.client, data["watched movie"][1].person_url) week2_actors = get_people_from_url_ok(self.client, data["watched movie"][2].person_url) week3_actors = get_people_from_url_ok(self.client, data["watched movie"][3].person_url) assert sorted([p["id"] for p in week1_actors]) == sorted(["org:0", "org:2"]) assert sorted([p["id"] for p in week2_actors]) == sorted([]) assert sorted([p["id"] for p in week3_actors]) == sorted(["org:1"])
def _create_related_groups_data(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="playlist", group_type_index=1) uuid = UUID("01795392-cc00-0003-7dc7-67a694604d72") Person.objects.create(uuid=uuid, team_id=self.team.pk, distinct_ids=["1", "2"]) Person.objects.create(team_id=self.team.pk, distinct_ids=["3"]) Person.objects.create(team_id=self.team.pk, distinct_ids=["4"]) create_group(self.team.pk, 0, "0::0") create_group(self.team.pk, 0, "0::1") create_group(self.team.pk, 1, "1::2") create_group(self.team.pk, 1, "1::3") create_group(self.team.pk, 1, "1::4") create_group(self.team.pk, 1, "1::5") _create_event( event="$pageview", team=self.team, distinct_id="1", timestamp="2021-05-05 00:00:00", properties={ "$group_0": "0::0", "$group_1": "1::2" }, ) _create_event( event="$pageview", team=self.team, distinct_id="1", timestamp="2021-05-05 00:00:00", properties={ "$group_0": "0::0", "$group_1": "1::3" }, ) _create_event( event="$pageview", team=self.team, distinct_id="1", timestamp="2021-05-05 00:00:00", properties={ "$group_0": "0::1", "$group_1": "1::3" }, ) # Event too old, not counted _create_event( event="$pageview", team=self.team, distinct_id="1", timestamp="2000-05-05 00:00:00", properties={ "$group_0": "0::0", "$group_1": "1::4" }, ) # No such group exists in groups table _create_event( event="$pageview", team=self.team, distinct_id="1", timestamp="2000-05-05 00:00:00", properties={ "$group_0": "0::0", "$group_1": "no such group" }, ) return uuid
def test_funnel_aggregation_with_groups_with_cohort_filtering(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:2", properties={}) _create_person(distinct_ids=[f"user_1"], team=self.team, properties={"email": "*****@*****.**"}) _create_person(distinct_ids=[f"user_2"], team=self.team, properties={"email": "*****@*****.**"}) _create_person(distinct_ids=[f"user_3"], team=self.team, properties={"email": "*****@*****.**"}) action1 = Action.objects.create(team=self.team, name="action1") ActionStep.objects.create( event="$pageview", action=action1, ) cohort = Cohort.objects.create( team=self.team, groups=[{ "properties": [{ "key": "email", "operator": "icontains", "value": "*****@*****.**", "type": "person" }] }], ) events_by_person = { "user_1": [ {"event": "$pageview", "timestamp": datetime(2020, 1, 2, 14), "properties": {"$group_0": "org:5"}}, {"event": "user signed up", "timestamp": datetime(2020, 1, 2, 14), "properties": {"$group_0": "org:5"}}, { "event": "user signed up", # same person, different group, so should count as different step 1 in funnel "timestamp": datetime(2020, 1, 10, 14), "properties": {"$group_0": "org:6"}, }, ], "user_2": [ { # different person, same group, so should count as step two in funnel "event": "paid", "timestamp": datetime(2020, 1, 3, 14), "properties": {"$group_0": "org:5"}, }, ], "user_3": [ {"event": "user signed up", "timestamp": datetime(2020, 1, 2, 14), "properties": {"$group_0": "org:7"}}, { # person not in cohort so should be filtered out "event": "paid", "timestamp": datetime(2020, 1, 3, 14), "properties": {"$group_0": "org:7"}, }, ], } journeys_for(events_by_person, self.team) cohort.calculate_people_ch(pending_version=0) filters = { "events": [ { "id": "user signed up", "type": "events", "order": 0, "properties": [{ "type": "precalculated-cohort", "key": "id", "value": cohort.pk }], }, { "id": "paid", "type": "events", "order": 1 }, ], "insight": INSIGHT_FUNNELS, "date_from": "2020-01-01", "date_to": "2020-01-14", "aggregation_group_type_index": 0, } filter = Filter(data=filters) funnel = ClickhouseFunnel(filter, self.team) result = funnel.run() self.assertEqual(result[0]["name"], "user signed up") self.assertEqual(result[0]["count"], 2) self.assertEqual(result[1]["name"], "paid") self.assertEqual(result[1]["count"], 1)
def test_filter_by_group_properties(self): create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:1", properties={"industry": "technology"} ) create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:2", properties={"industry": "agriculture"} ) create_group( team_id=self.team.pk, group_type_index=0, group_key=f"org:3", properties={"industry": "technology"} ) create_group(team_id=self.team.pk, group_type_index=0, group_key=f"org:4", properties={}) create_group( team_id=self.team.pk, group_type_index=1, group_key=f"company:1", properties={"industry": "technology"} ) create_group(team_id=self.team.pk, group_type_index=1, group_key=f"instance:1", properties={}) p1, p2, p3, p4 = self._create_multiple_people( period=timedelta(weeks=1), event_properties=lambda i: {"$group_0": f"org:{i}", "$group_1": "instance:1"}, ) with freeze_time("2020-02-15T13:01:01Z"): data = get_stickiness_time_series_ok( client=self.client, team=self.team, request={ "shown_as": "Stickiness", "date_from": "2020-01-01", "date_to": "2020-02-15", "events": [{"id": "watched movie"}], "properties": [{"key": "industry", "value": "technology", "type": "group", "group_type_index": 0}], "interval": "week", }, ) assert data["watched movie"][1].value == 1 assert data["watched movie"][2].value == 0 assert data["watched movie"][3].value == 1 with freeze_time("2020-02-15T13:01:01Z"): week1_actors = get_people_from_url_ok(self.client, data["watched movie"][1].person_url) week2_actors = get_people_from_url_ok(self.client, data["watched movie"][2].person_url) week3_actors = get_people_from_url_ok(self.client, data["watched movie"][3].person_url) assert sorted([p["id"] for p in week1_actors]) == sorted([str(p1.uuid)]) assert sorted([p["id"] for p in week2_actors]) == sorted([]) assert sorted([p["id"] for p in week3_actors]) == sorted([str(p3.uuid)])
def _create_groups_test_data(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={"another": "value"}) Person.objects.create(team_id=self.team.pk, distinct_ids=["p1"], properties={"$browser": "test"}) Person.objects.create(team_id=self.team.pk, distinct_ids=["p2"], properties={"$browser": "foobar"}) Person.objects.create(team_id=self.team.pk, distinct_ids=["p3"], properties={"$browser": "test"}) _create_event( team=self.team, event="$pageview", distinct_id="p1", timestamp="2020-01-02T12:00:00Z", properties={ "$group_0": "org:5", "$group_1": "company:1" }, ) _create_event( team=self.team, event="$pageview", distinct_id="p2", timestamp="2020-01-02T12:00:00Z", properties={ "$group_0": "org:6", "$group_1": "company:1" }, ) _create_event( team=self.team, event="$pageview", distinct_id="p1", timestamp="2020-01-02T12:00:00Z", properties={"$group_0": "org:6"}, ) _create_event( team=self.team, event="$pageview", distinct_id="p3", timestamp="2020-01-02T12:00:00Z", properties={"$group_0": "org:5"}, )
def _create_groups_and_events(self): GroupTypeMapping.objects.create(team=self.team, group_type="organization", group_type_index=0) GroupTypeMapping.objects.create(team=self.team, group_type="company", group_type_index=1) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:5", properties={"industry": "finance"}) create_group(team_id=self.team.pk, group_type_index=0, group_key="org:6", properties={"industry": "technology"}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:1", properties={}) create_group(team_id=self.team.pk, group_type_index=1, group_key="company:2", properties={}) Person.objects.create(team=self.team, distinct_ids=["person1", "alias1"]) Person.objects.create(team=self.team, distinct_ids=["person2"]) Person.objects.create(team=self.team, distinct_ids=["person3"]) _create_events( self.team, [ ("person1", _date(0), { "$group_0": "org:5", "$group_1": "company:1" }), ("person2", _date(0), { "$group_0": "org:6" }), ("person3", _date(0)), ("person1", _date(1), { "$group_0": "org:5" }), ("person2", _date(1), { "$group_0": "org:6" }), ("person1", _date(7), { "$group_0": "org:5" }), ("person2", _date(7), { "$group_0": "org:6" }), ("person1", _date(14), { "$group_0": "org:5" }), ("person1", _date(month=1, day=-6), { "$group_0": "org:5", "$group_1": "company:1" }), ("person2", _date(month=1, day=-6), { "$group_0": "org:6" }), ("person2", _date(month=1, day=1), { "$group_0": "org:6" }), ("person1", _date(month=1, day=1), { "$group_0": "org:5" }), ("person2", _date(month=1, day=15), { "$group_0": "org:6", "$group_1": "company:1" }), ], )