Esempio n. 1
0
    def calculate_path(self, request: Request) -> Dict[str, Any]:
        team = self.team
        filter = PathFilter(request=request,
                            data={"insight": INSIGHT_PATHS},
                            team=self.team)

        funnel_filter = None
        funnel_filter_data = request.GET.get(
            "funnel_filter") or request.data.get("funnel_filter")
        if funnel_filter_data:
            if isinstance(funnel_filter_data, str):
                funnel_filter_data = json.loads(funnel_filter_data)
            funnel_filter = Filter(data={
                "insight": INSIGHT_FUNNELS,
                **funnel_filter_data
            },
                                   team=self.team)

        #  backwards compatibility
        if filter.path_type:
            filter = filter.with_data(
                {PATHS_INCLUDE_EVENT_TYPES: [filter.path_type]})
        resp = ClickhousePaths(filter=filter,
                               team=team,
                               funnel_filter=funnel_filter).run()

        return {"result": resp}
Esempio n. 2
0
def get_filter(team, data: dict = {}, request: Optional[HttpRequest] = None):
    from posthog.models.filters.filter import Filter
    from posthog.models.filters.retention_filter import RetentionFilter
    from posthog.models.filters.sessions_filter import SessionsFilter
    from posthog.models.filters.stickiness_filter import StickinessFilter

    insight = data.get("insight")
    if not insight and request:
        insight = request.GET.get("insight")
    if insight == INSIGHT_RETENTION:
        return RetentionFilter(data={
            **data, "insight": INSIGHT_RETENTION
        },
                               request=request)
    elif insight == INSIGHT_SESSIONS:
        return SessionsFilter(data={
            **data, "insight": INSIGHT_SESSIONS
        },
                              request=request)
    elif insight == INSIGHT_STICKINESS or (insight == INSIGHT_TRENDS
                                           and data.get("shown_as")
                                           == "Stickiness"):
        return StickinessFilter(data=data,
                                request=request,
                                team=team,
                                get_earliest_timestamp=earliest_timestamp_func)
    elif insight == INSIGHT_PATHS:
        return PathFilter(data={
            **data, "insight": INSIGHT_PATHS
        },
                          request=request)
    return Filter(data=data, request=request)
Esempio n. 3
0
        def test_paths_in_window(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])

            with freeze_time("2020-04-14T03:25:34.000Z"):
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_1", event="$pageview", team=self.team,
                )
            with freeze_time("2020-04-14T03:30:34.000Z"):
                event_factory(
                    properties={"$current_url": "/about"}, distinct_id="person_1", event="$pageview", team=self.team,
                )

            with freeze_time("2020-04-15T03:25:34.000Z"):
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_1", event="$pageview", team=self.team,
                )
            with freeze_time("2020-04-15T03:30:34.000Z"):
                event_factory(
                    properties={"$current_url": "/about"}, distinct_id="person_1", event="$pageview", team=self.team,
                )

            response = paths().run(team=self.team, filter=PathFilter(data={"date_from": "2020-04-13"}))

            self.assertEqual(response[0]["source"], "1_/")
            self.assertEqual(response[0]["target"], "2_/about")
            self.assertEqual(response[0]["value"], 2)
Esempio n. 4
0
    def calculate_path_persons(
        self, request: Request
    ) -> Dict[str, Tuple[list, Optional[str], Optional[str]]]:
        if request.user.is_anonymous or not self.team:
            return {"result": ([], None, None)}

        filter = PathFilter(request=request,
                            data={"insight": INSIGHT_PATHS},
                            team=self.team)

        funnel_filter = None
        funnel_filter_data = request.GET.get(
            "funnel_filter") or request.data.get("funnel_filter")
        if funnel_filter_data:
            if isinstance(funnel_filter_data, str):
                funnel_filter_data = json.loads(funnel_filter_data)
            funnel_filter = Filter(data={
                "insight": INSIGHT_FUNNELS,
                **funnel_filter_data
            },
                                   team=self.team)

        people, should_paginate = ClickhousePathsPersons(
            filter, self.team, funnel_filter=funnel_filter).run()
        limit = filter.limit or 100
        next_url = format_query_params_absolute_url(
            request, filter.offset + limit) if should_paginate else None
        initial_url = format_query_params_absolute_url(request, 0)

        # cached_function expects a dict with the key result
        return {"result": (people, next_url, initial_url)}
Esempio n. 5
0
 def calculate_path(self, request: request.Request) -> Dict[str, Any]:
     team = self.team
     filter = PathFilter(request=request,
                         data={
                             **request.data, "insight": INSIGHT_PATHS
                         })
     resp = paths.Paths().run(filter=filter, team=team)
     return {"result": resp}
Esempio n. 6
0
    def test_determine_compared_filter(self):
        from posthog.queries.base import determine_compared_filter

        filter = PathFilter(data={"date_from": "2020-05-22", "date_to": "2020-05-29"})
        compared_filter = determine_compared_filter(filter)

        self.assertIsInstance(compared_filter, PathFilter)
        self.assertDictContainsSubset({"date_from": "2020-05-15", "date_to": "2020-05-22",}, compared_filter.to_dict())
Esempio n. 7
0
 def get_list(self, request):
     team = self.team
     date_query = request_to_date_query(request.GET, exact=False)
     filter = PathFilter(request=request)
     start_point = request.GET.get("start")
     request_type = request.GET.get("type", None)
     resp = paths.Paths().run(
         filter=filter, start_point=start_point, date_query=date_query, request_type=request_type, team=team,
     )
     return resp
Esempio n. 8
0
def insert_cohort_actors_into_ch(cohort: Cohort, filter_data: Dict):
    insight_type = filter_data.get("insight")
    query_builder: ActorBaseQuery

    if insight_type == INSIGHT_TRENDS:
        filter = Filter(data=filter_data, team=cohort.team)
        entity = get_target_entity(filter)
        query_builder = ClickhouseTrendsActors(cohort.team, entity, filter)
    elif insight_type == INSIGHT_STICKINESS:
        stickiness_filter = StickinessFilter(data=filter_data,
                                             team=cohort.team)
        entity = get_target_entity(stickiness_filter)
        query_builder = ClickhouseStickinessActors(cohort.team, entity,
                                                   stickiness_filter)
    elif insight_type == INSIGHT_FUNNELS:
        funnel_filter = Filter(data=filter_data, team=cohort.team)
        if funnel_filter.correlation_person_entity:
            query_builder = FunnelCorrelationActors(filter=funnel_filter,
                                                    team=cohort.team)
        else:
            funnel_actor_class = get_funnel_actor_class(funnel_filter)
            query_builder = funnel_actor_class(filter=funnel_filter,
                                               team=cohort.team)
    elif insight_type == INSIGHT_PATHS:
        path_filter = PathFilter(data=filter_data, team=cohort.team)
        query_builder = ClickhousePathsActors(path_filter,
                                              cohort.team,
                                              funnel_filter=None)
    else:
        if settings.DEBUG:
            raise ValueError(
                f"Insight type: {insight_type} not supported for cohort creation"
            )
        else:
            capture_exception(
                Exception(
                    f"Insight type: {insight_type} not supported for cohort creation"
                ))

    if query_builder.is_aggregating_by_groups:
        if settings.DEBUG:
            raise ValueError(
                f"Query type: Group based queries are not supported for cohort creation"
            )
        else:
            capture_exception(
                Exception(
                    f"Query type: Group based queries are not supported for cohort creation"
                ))
    else:
        query, params = query_builder.actor_query(limit_actors=False)

    insert_actors_into_cohort_by_query(cohort, query, params)
Esempio n. 9
0
        def test_screen_paths(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])
            event_factory(
                properties={"$screen_name": "/"}, distinct_id="person_1", event="$screen", team=self.team,
            )
            event_factory(
                properties={"$screen_name": "/about"}, distinct_id="person_1", event="$screen", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_2a", "person_2b"])
            event_factory(
                properties={"$screen_name": "/"}, distinct_id="person_2b", event="$screen", team=self.team,
            )
            event_factory(
                properties={"$screen_name": "/pricing"}, distinct_id="person_2a", event="$screen", team=self.team,
            )
            event_factory(
                properties={"$screen_name": "/about"}, distinct_id="person_2b", event="$screen", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
            event_factory(
                properties={"$screen_name": "/pricing"}, distinct_id="person_3", event="$screen", team=self.team,
            )
            event_factory(
                properties={"$screen_name": "/"}, distinct_id="person_3", event="$screen", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
            event_factory(
                properties={"$screen_name": "/"}, distinct_id="person_4", event="$screen", team=self.team,
            )
            event_factory(
                properties={"$screen_name": "/pricing"}, distinct_id="person_4", event="$screen", team=self.team,
            )

            response = paths().run(team=self.team, filter=PathFilter(data={"path_type": "$screen"}))
            self.assertEqual(response[0]["source"], "1_/", response)
            self.assertEqual(response[0]["target"], "2_/pricing")
            self.assertEqual(response[0]["value"], 2)

            self.assertEqual(response[1]["source"], "1_/")
            self.assertEqual(response[1]["target"], "2_/about")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "1_/pricing")
            self.assertEqual(response[2]["target"], "2_/")
            self.assertEqual(response[2]["value"], 1)

            self.assertEqual(response[3]["source"], "2_/pricing", response[3])
            self.assertEqual(response[3]["target"], "3_/about")
            self.assertEqual(response[3]["value"], 1)
Esempio n. 10
0
        def test_paths_in_window(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])

            events = [
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2020-04-14 03:25:34",
                ),
                event_factory(
                    properties={"$current_url": "/about"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2020-04-14 03:30:34",
                ),
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2020-04-15 03:25:34",
                ),
                event_factory(
                    properties={"$current_url": "/about"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2020-04-15 03:30:34",
                ),
            ]

            create_all_events(events)
            filter = PathFilter(data={"date_from": "2020-04-13"})
            response = paths(team=self.team, filter=filter).run(team=self.team,
                                                                filter=filter)

            self.assertEqual(response[0]["source"], "1_/")
            self.assertEqual(response[0]["target"], "2_/about")
            self.assertEqual(response[0]["value"], 2)
Esempio n. 11
0
        def test_custom_event_paths(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])
            event_factory(distinct_id="person_1", event="custom_event_1", team=self.team)
            event_factory(distinct_id="person_1", event="custom_event_3", team=self.team)
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_1", event="$pageview", team=self.team,
            )  # should be ignored

            person_factory(team_id=self.team.pk, distinct_ids=["person_2"])
            event_factory(distinct_id="person_2", event="custom_event_1", team=self.team)
            event_factory(distinct_id="person_2", event="custom_event_2", team=self.team)
            event_factory(distinct_id="person_2", event="custom_event_3", team=self.team)

            person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
            event_factory(distinct_id="person_3", event="custom_event_2", team=self.team)
            event_factory(distinct_id="person_3", event="custom_event_1", team=self.team)

            person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
            event_factory(distinct_id="person_4", event="custom_event_1", team=self.team)
            event_factory(distinct_id="person_4", event="custom_event_2", team=self.team)

            response = paths().run(team=self.team, filter=PathFilter(data={"path_type": "custom_event"}))

            self.assertEqual(response[0]["source"], "1_custom_event_1", response)
            self.assertEqual(response[0]["target"], "2_custom_event_2")
            self.assertEqual(response[0]["value"], 2)

            self.assertEqual(response[1]["source"], "1_custom_event_1")
            self.assertEqual(response[1]["target"], "2_custom_event_3")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "1_custom_event_2")
            self.assertEqual(response[2]["target"], "2_custom_event_1")
            self.assertEqual(response[2]["value"], 1)

            self.assertEqual(response[3]["source"], "2_custom_event_2", response[3])
            self.assertEqual(response[3]["target"], "3_custom_event_3")
            self.assertEqual(response[3]["value"], 1)
Esempio n. 12
0
 def calculate_path(self, request: Request) -> List[Dict[str, Any]]:
     team = self.team
     filter = PathFilter(request=request, data={"insight": INSIGHT_PATHS})
     resp = ClickhousePaths().run(filter=filter, team=team)
     return resp
Esempio n. 13
0
    def calculate_paths(self, filter: PathFilter, team: Team):
        date_query = request_to_date_query(
            {
                "date_from": filter._date_from,
                "date_to": filter._date_to
            },
            exact=False)
        resp = []
        prop_type = filter.prop_type
        event, event_filter = filter.target_event
        start_comparator = filter.comparator

        sessions = (Event.objects.add_person_id(team.pk).filter(
            team=team, **(event_filter), **date_query
        ).filter(~Q(event__in=[
            "$autocapture", "$pageview", "$identify", "$pageleave", "$screen"
        ]) if event is None else Q()).filter(
            filter.properties_to_Q(team_id=team.pk) if filter
            and filter.properties else Q()).annotate(previous_timestamp=Window(
                expression=Lag("timestamp", default=None),
                partition_by=F("person_id"),
                order_by=F("timestamp").asc(),
            )))

        sessions_sql, sessions_sql_params = sessions.query.sql_with_params()

        if event == "$autocapture":
            sessions_sql = self._add_elements(query_string=sessions_sql)

        events_notated = "\
        SELECT *, CASE WHEN EXTRACT('EPOCH' FROM (timestamp - previous_timestamp)) >= (60 * 30) OR previous_timestamp IS NULL THEN 1 ELSE 0 END AS new_session\
        FROM ({}) AS inner_sessions\
        ".format(sessions_sql)

        sessionified = "\
        SELECT events_notated.*, SUM(new_session) OVER (\
            ORDER BY person_id\
                    ,timestamp\
            ) AS session\
        FROM ({}) as events_notated\
        ".format(events_notated)

        if filter and filter.start_point:
            sessionified = self._apply_start_point(
                start_comparator=start_comparator,
                query_string=sessionified,
                start_point=filter.start_point,
            )

        final = "\
        SELECT {} as path_type, id, sessionified.session\
            ,ROW_NUMBER() OVER (\
                    PARTITION BY person_id\
                    ,session ORDER BY timestamp\
                    ) AS event_number\
        FROM ({}) as sessionified\
        ".format(prop_type, sessionified)

        counts = "\
        SELECT event_number || '_' || path_type as target_event, id as target_id, LAG(event_number || '_' || path_type, 1) OVER (\
            PARTITION BY session\
            ) AS source_event , LAG(id, 1) OVER (\
            PARTITION BY session\
            ) AS source_id from \
        ({}) as final\
        where event_number <= 4\
        ".format(final)

        query = "\
        SELECT source_event, target_event, MAX(target_id), MAX(source_id), count(*) from ({}) as counts\
        where source_event is not null and target_event is not null\
        group by source_event, target_event order by count desc limit 20\
        ".format(counts)

        cursor = connection.cursor()
        cursor.execute(query, sessions_sql_params)
        rows = cursor.fetchall()

        for row in rows:
            resp.append({
                "source": row[0],
                "target": row[1],
                "target_id": row[2],
                "source_id": row[3],
                "value": row[4],
            })

        resp = sorted(resp, key=lambda x: x["value"], reverse=True)
        return resp
Esempio n. 14
0
    def list(self, request, **kwargs):

        team = self.team
        filter = PathFilter(request=request)
        resp = ClickhousePaths().run(filter=filter, team=team)
        return Response(resp)
Esempio n. 15
0
        def test_paths_start(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_1", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about"}, distinct_id="person_1", event="$pageview", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_2"])
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_2", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_2", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about"}, distinct_id="person_2", event="$pageview", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_3", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_3", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about"}, distinct_id="person_3", event="$pageview", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_4", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_4", event="$pageview", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_5a", "person_5b"])
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_5a", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about"}, distinct_id="person_5b", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_5a", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/help"}, distinct_id="person_5b", event="$pageview", team=self.team,
            )

            response = self.client.get("/api/insight/path/?type=%24pageview&start=%2Fpricing").json()

            response = paths().run(
                team=self.team, filter=PathFilter(data={"path_type": "$pageview", "start_point": "/pricing"}),
            )

            self.assertEqual(len(response), 5)

            self.assertTrue(response[0].items() >= {"source": "1_/pricing", "target": "2_/about", "value": 2}.items())
            self.assertTrue(response[1].items() >= {"source": "1_/pricing", "target": "2_/", "value": 1}.items())
            self.assertTrue(response[2].items() >= {"source": "2_/", "target": "3_/about", "value": 1}.items())
            self.assertTrue(response[3].items() >= {"source": "2_/about", "target": "3_/pricing", "value": 1}.items())
            self.assertTrue(response[4].items() >= {"source": "3_/pricing", "target": "4_/help", "value": 1}.items())

            response = paths().run(
                team=self.team, filter=PathFilter(data={"path_type": "$pageview", "start_point": "/"}),
            )

            self.assertEqual(len(response), 3)

            self.assertTrue(response[0].items() >= {"source": "1_/", "target": "2_/about", "value": 2}.items())
            self.assertTrue(response[1].items() >= {"source": "1_/", "target": "2_/pricing", "value": 2}.items())
            self.assertTrue(response[2].items() >= {"source": "2_/pricing", "target": "3_/about", "value": 1}.items())
Esempio n. 16
0
        def test_paths_properties_filter(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])
            event_factory(
                properties={"$current_url": "/", "$browser": "Chrome"},
                distinct_id="person_1",
                event="$pageview",
                team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about", "$browser": "Chrome"},
                distinct_id="person_1",
                event="$pageview",
                team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_2"])
            event_factory(
                properties={"$current_url": "/", "$browser": "Chrome"},
                distinct_id="person_2",
                event="$pageview",
                team=self.team,
            )
            event_factory(
                properties={"$current_url": "/pricing", "$browser": "Chrome"},
                distinct_id="person_2",
                event="$pageview",
                team=self.team,
            )
            event_factory(
                properties={"$current_url": "/about", "$browser": "Chrome"},
                distinct_id="person_2",
                event="$pageview",
                team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_3", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_3", event="$pageview", team=self.team,
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
            event_factory(
                properties={"$current_url": "/"}, distinct_id="person_4", event="$pageview", team=self.team,
            )
            event_factory(
                properties={"$current_url": "/pricing"}, distinct_id="person_4", event="$pageview", team=self.team,
            )

            filter = PathFilter(data={"properties": [{"key": "$browser", "value": "Chrome", "type": "event"}]})

            response = paths().run(team=self.team, filter=filter)

            self.assertEqual(response[0]["source"], "1_/")
            self.assertEqual(response[0]["target"], "2_/about")
            self.assertEqual(response[0]["value"], 1)

            self.assertEqual(response[1]["source"], "1_/")
            self.assertEqual(response[1]["target"], "2_/pricing")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "2_/pricing")
            self.assertEqual(response[2]["target"], "3_/about")
            self.assertEqual(response[2]["value"], 1)
Esempio n. 17
0
        def test_autocapture_paths(self):
            person_factory(team_id=self.team.pk, distinct_ids=["person_1"])

            event_factory(
                event="$autocapture",
                team=self.team,
                distinct_id="person_1",
                elements=[
                    Element(tag_name="a", text="hello", href="/a-url", nth_child=1, nth_of_type=0),
                    Element(tag_name="button", nth_child=0, nth_of_type=0),
                    Element(tag_name="div", nth_child=0, nth_of_type=0),
                    Element(tag_name="div", nth_child=0, nth_of_type=0, attr_id="nested",),
                ],
            )

            event_factory(
                event="$autocapture",
                team=self.team,
                distinct_id="person_1",
                elements=[
                    Element(tag_name="a", text="goodbye", nth_child=2, nth_of_type=0, attr_id="someId",),
                    Element(tag_name="div", nth_child=0, nth_of_type=0),
                    # make sure elements don't get double counted if they're part of the same event
                    Element(href="/a-url-2", nth_child=0, nth_of_type=0),
                ],
            )

            person_factory(team_id=self.team.pk, distinct_ids=["person_2"])
            event_factory(
                event="$autocapture",
                team=self.team,
                distinct_id="person_2",
                elements=[
                    Element(tag_name="a", text="hello1", href="/a-url", nth_child=1, nth_of_type=0,),
                    Element(tag_name="button", nth_child=0, nth_of_type=0),
                    Element(tag_name="div", nth_child=0, nth_of_type=0),
                    Element(tag_name="div", nth_child=0, nth_of_type=0, attr_id="nested",),
                ],
            )

            event_factory(
                event="$autocapture",
                team=self.team,
                distinct_id="person_2",
                elements=[
                    Element(tag_name="a", text="goodbye1", nth_child=2, nth_of_type=0, attr_id="someId",),
                    Element(tag_name="div", nth_child=0, nth_of_type=0),
                    # make sure elements don't get double counted if they're part of the same event
                    Element(href="/a-url-2", nth_child=0, nth_of_type=0),
                ],
            )

            event_factory(
                event="$autocapture",
                team=self.team,
                distinct_id="person_2",
                elements=[
                    Element(tag_name="a", text="goodbye1", nth_child=2, nth_of_type=0, attr_id="someId",),
                    Element(tag_name="div", nth_child=0, nth_of_type=0),
                    # make sure elements don't get double counted if they're part of the same event
                    Element(href="/a-url-2", nth_child=0, nth_of_type=0),
                ],
            )

            response = paths().run(team=self.team, filter=PathFilter(data={"path_type": "$autocapture"}))

            self.assertEqual(response[0]["source"], "1_<a> hello")
            self.assertEqual(response[0]["target"], "2_<a> goodbye")
            self.assertEqual(response[0]["value"], 1)

            self.assertEqual(response[1]["source"], "1_<a> hello1")
            self.assertEqual(response[1]["target"], "2_<a> goodbye1")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "2_<a> goodbye1")
            self.assertEqual(response[2]["target"], "3_<a> goodbye1")
            self.assertEqual(response[2]["value"], 1)

            elements = self.client.get("/api/paths/elements/").json()
            self.assertEqual(elements[0]["name"], "<a> goodbye1")  # first since captured twice
            self.assertEqual(elements[1]["name"], "<a> goodbye")
            self.assertEqual(elements[2]["name"], "<a> hello")
            self.assertEqual(elements[3]["name"], "<a> hello1")
            self.assertEqual(len(elements), 4)
Esempio n. 18
0
        def test_current_url_paths_and_logic(self):

            with freeze_time("2012-01-01T03:21:34.000Z"):
                person_factory(team_id=self.team.pk, distinct_ids=["fake"])
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="fake", event="$pageview", team=self.team,
                )
                event_factory(
                    properties={"$current_url": "/about"}, distinct_id="fake", event="$pageview", team=self.team,
                )

            with freeze_time("2012-01-14T03:21:34.000Z"):
                person_factory(team_id=self.team.pk, distinct_ids=["person_1"])
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_1", event="$pageview", team=self.team,
                )
            with freeze_time("2012-01-14T03:28:34.000Z"):
                event_factory(
                    properties={"$current_url": "/about"}, distinct_id="person_1", event="$pageview", team=self.team,
                )

            with freeze_time("2012-01-14T03:21:34.000Z"):
                person_factory(team_id=self.team.pk, distinct_ids=["person_2a", "person_2b"])
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_2a", event="$pageview", team=self.team,
                )
            with freeze_time("2012-01-14T03:28:34.000Z"):
                event_factory(
                    properties={"$current_url": "/pricing"}, distinct_id="person_2b", event="$pageview", team=self.team,
                )
            with freeze_time("2012-01-14T03:29:34.000Z"):
                event_factory(
                    properties={"$current_url": "/about"}, distinct_id="person_2a", event="$pageview", team=self.team,
                )

            with freeze_time("2012-01-14T03:21:34.000Z"):
                person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
                event_factory(
                    properties={"$current_url": "/pricing"}, distinct_id="person_3", event="$pageview", team=self.team,
                )
            with freeze_time("2012-01-14T03:28:34.000Z"):
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_3", event="$pageview", team=self.team,
                )

            with freeze_time("2012-01-14T03:21:34.000Z"):
                person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
                event_factory(
                    properties={"$current_url": "/"}, distinct_id="person_4", event="$pageview", team=self.team,
                )
            with freeze_time("2012-01-14T03:28:34.000Z"):
                event_factory(
                    properties={"$current_url": "/pricing"}, distinct_id="person_4", event="$pageview", team=self.team,
                )

            with freeze_time("2012-01-15T03:21:34.000Z"):
                filter = PathFilter(data={"dummy": "dummy"})
                response = paths().run(team=self.team, filter=filter)

            self.assertEqual(response[0]["source"], "1_/", response)
            self.assertEqual(response[0]["target"], "2_/pricing")
            self.assertEqual(response[0]["value"], 2)

            self.assertEqual(response[1]["source"], "1_/")
            self.assertEqual(response[1]["target"], "2_/about")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "1_/pricing")
            self.assertEqual(response[2]["target"], "2_/")
            self.assertEqual(response[2]["value"], 1)

            self.assertEqual(response[3]["source"], "2_/pricing", response[3])
            self.assertEqual(response[3]["target"], "3_/about")
            self.assertEqual(response[3]["value"], 1)

            with freeze_time("2012-01-15T03:21:34.000Z"):
                date_from = now() - relativedelta(days=7)
                response = self.client.get(
                    "/api/insight/path/?insight=PATHS&date_from=" + date_from.strftime("%Y-%m-%d")
                ).json()
                self.assertEqual(len(response), 4)

                date_to = now()
                response = self.client.get(
                    "/api/insight/path/?insight=PATHS&date_to=" + date_to.strftime("%Y-%m-%d")
                ).json()
                self.assertEqual(len(response), 4)

                date_from = now() + relativedelta(days=7)
                response = self.client.get(
                    "/api/insight/path/?insight=PATHS&date_from=" + date_from.strftime("%Y-%m-%d")
                ).json()
                self.assertEqual(len(response), 0)

                date_to = now() - relativedelta(days=7)
                response = self.client.get(
                    "/api/insight/path/?insight=PATHS&date_to=" + date_to.strftime("%Y-%m-%d")
                ).json()
                self.assertEqual(len(response), 0)

                date_from = now() - relativedelta(days=7)
                date_to = now() + relativedelta(days=7)

                date_params = {"date_from": date_from.strftime("%Y-%m-%d"), "date_to": date_to.strftime("%Y-%m-%d")}

                filter = PathFilter(data={**date_params})
                response = paths().run(team=self.team, filter=filter)
                self.assertEqual(len(response), 4)

                date_from = now() + relativedelta(days=7)
                date_to = now() - relativedelta(days=7)
                date_params = {"date_from": date_from.strftime("%Y-%m-%d"), "date_to": date_to.strftime("%Y-%m-%d")}
                filter = PathFilter(data={**date_params})
                response = paths().run(team=self.team, filter=filter)
                self.assertEqual(len(response), 0)
Esempio n. 19
0
        def test_current_url_paths_and_logic(self):
            events = []
            person_factory(team_id=self.team.pk, distinct_ids=["fake"])
            events.extend([
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="fake",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-01 03:21:34",
                ),
                event_factory(
                    properties={"$current_url": "/about"},
                    distinct_id="fake",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-01 03:21:34",
                ),
            ])

            person_factory(team_id=self.team.pk,
                           distinct_ids=["person_1"],
                           properties={"email": "*****@*****.**"})
            events.append(
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:21:34",
                ))
            events.append(
                event_factory(
                    properties={"$current_url": "/about"},
                    distinct_id="person_1",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:28:34",
                ))

            person_factory(team_id=self.team.pk,
                           distinct_ids=["person_2a", "person_2b"])
            events.append(
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_2a",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:21:34",
                ))
            events.append(
                event_factory(
                    properties={"$current_url": "/pricing"},
                    distinct_id="person_2b",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:28:34",
                ))
            events.append(
                event_factory(
                    properties={"$current_url": "/about"},
                    distinct_id="person_2a",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:29:34",
                ))

            person_factory(team_id=self.team.pk, distinct_ids=["person_3"])
            events.append(
                event_factory(
                    properties={"$current_url": "/pricing"},
                    distinct_id="person_3",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:21:34",
                ))

            events.append(
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_3",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:28:34",
                ))

            person_factory(team_id=self.team.pk, distinct_ids=["person_4"])
            events.append(
                event_factory(
                    properties={"$current_url": "/"},
                    distinct_id="person_4",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:21:34",
                ))
            events.append(
                event_factory(
                    properties={"$current_url": "/pricing"},
                    distinct_id="person_4",
                    event="$pageview",
                    team=self.team,
                    timestamp="2012-01-14 03:28:34",
                ))

            create_all_events(events)

            with freeze_time("2012-01-15T03:21:34.000Z"):
                filter = PathFilter(data={"dummy": "dummy"})
                response = paths(team=self.team,
                                 filter=filter).run(team=self.team,
                                                    filter=filter)

            self.assertEqual(response[0]["source"], "1_/", response)
            self.assertEqual(response[0]["target"], "2_/pricing")
            self.assertEqual(response[0]["value"], 2)

            self.assertEqual(response[1]["source"], "1_/")
            self.assertEqual(response[1]["target"], "2_/about")
            self.assertEqual(response[1]["value"], 1)

            self.assertEqual(response[2]["source"], "1_/pricing")
            self.assertEqual(response[2]["target"], "2_/")
            self.assertEqual(response[2]["value"], 1)

            self.assertEqual(response[3]["source"], "2_/pricing", response[3])
            self.assertEqual(response[3]["target"], "3_/about")
            self.assertEqual(response[3]["value"], 1)

            with freeze_time("2012-01-15T03:21:34.000Z"):
                date_from = now() - relativedelta(days=7)
                response = self.client.get(
                    f"/api/projects/{self.team.id}/insights/path/?insight=PATHS&date_from="
                    + date_from.strftime("%Y-%m-%d")).json()
                self.assertEqual(len(response["result"]), 4)

                date_to = now()
                response = self.client.get(
                    f"/api/projects/{self.team.id}/insights/path/?insight=PATHS&date_to="
                    + date_to.strftime("%Y-%m-%d")).json()
                self.assertEqual(len(response["result"]), 4)

                date_from = now() + relativedelta(days=7)
                response = self.client.get(
                    f"/api/projects/{self.team.id}/insights/path/?insight=PATHS&date_from="
                    + date_from.strftime("%Y-%m-%d")).json()
                self.assertEqual(len(response["result"]), 0)

                date_to = now() - relativedelta(days=7)
                response = self.client.get(
                    f"/api/projects/{self.team.id}/insights/path/?insight=PATHS&date_to="
                    + date_to.strftime("%Y-%m-%d")).json()
                self.assertEqual(len(response["result"]), 0)

                date_from = now() - relativedelta(days=7)
                date_to = now() + relativedelta(days=7)

                date_params = {
                    "date_from": date_from.strftime("%Y-%m-%d"),
                    "date_to": date_to.strftime("%Y-%m-%d")
                }

                filter = PathFilter(data={**date_params})
                response = paths(team=self.team,
                                 filter=filter).run(team=self.team,
                                                    filter=filter)
                self.assertEqual(len(response), 4)

                # Test account filter
                filter = PathFilter(data={
                    **date_params, FILTER_TEST_ACCOUNTS: True
                },
                                    team=self.team)
                response = paths(team=self.team,
                                 filter=filter).run(team=self.team,
                                                    filter=filter)
                self.assertEqual(len(response), 3)

                date_from = now() + relativedelta(days=7)
                date_to = now() - relativedelta(days=7)
                date_params = {
                    "date_from": date_from.strftime("%Y-%m-%d"),
                    "date_to": date_to.strftime("%Y-%m-%d")
                }
                filter = PathFilter(data={**date_params}, team=self.team)
                response = paths(team=self.team,
                                 filter=filter).run(team=self.team,
                                                    filter=filter)
                self.assertEqual(len(response), 0)