Skip to content

Commit

Permalink
feat(web-analytics): Filter test users from web analytics (#18751)
Browse files Browse the repository at this point in the history
Filter test users from web analytics
  • Loading branch information
robbie-c authored Nov 20, 2023
1 parent 9a5c644 commit a9528ad
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions posthog/hogql_queries/web_analytics/web_analytics_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ def property_filters_without_pathname(self) -> List[Union[EventPropertyFilter, P
return [p for p in self.query.properties if p.key != "$pathname"]

def session_where(self, include_previous_period: Optional[bool] = None):
properties = [
parse_expr(
"events.timestamp < {date_to} AND events.timestamp >= minus({date_from}, toIntervalHour(1))",
placeholders={
"date_from": self.query_date_range.previous_period_date_from_as_hogql()
if include_previous_period
else self.query_date_range.date_from_as_hogql(),
"date_to": self.query_date_range.date_to_as_hogql(),
},
)
] + self.property_filters_without_pathname
properties = (
[
parse_expr(
"events.timestamp < {date_to} AND events.timestamp >= minus({date_from}, toIntervalHour(1))",
placeholders={
"date_from": self.query_date_range.previous_period_date_from_as_hogql()
if include_previous_period
else self.query_date_range.date_from_as_hogql(),
"date_to": self.query_date_range.date_to_as_hogql(),
},
)
]
+ self.property_filters_without_pathname
+ self._test_account_filters
)
return property_to_expr(
properties,
self.team,
Expand Down Expand Up @@ -91,17 +95,29 @@ def session_having(self, include_previous_period: Optional[bool] = None):
)

def events_where(self):
properties = [
parse_expr(
"events.timestamp >= {date_from}",
placeholders={"date_from": self.query_date_range.date_from_as_hogql()},
)
] + self.query.properties
properties = (
[
parse_expr(
"events.timestamp >= {date_from}",
placeholders={"date_from": self.query_date_range.date_from_as_hogql()},
)
]
+ self.query.properties
+ self._test_account_filters
)

return property_to_expr(
properties,
self.team,
)

@cached_property
def _test_account_filters(self):
if isinstance(self.team.test_account_filters, list) and len(self.team.test_account_filters) > 0:
return self.team.test_account_filters
else:
return []

def _is_stale(self, cached_result_package):
date_to = self.query_date_range.date_to()
interval = self.query_date_range.interval_name
Expand Down

0 comments on commit a9528ad

Please sign in to comment.