Skip to content

Commit

Permalink
Attempt to shore up the stats table broken down by initial channel type
Browse files Browse the repository at this point in the history
Both before and after worked fine on my machine, so unclear how this will improve things, but I believe it might
  • Loading branch information
robbie-c committed Dec 15, 2023
1 parent 4649e6c commit 2e7f4eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
68 changes: 65 additions & 3 deletions posthog/hogql_queries/web_analytics/stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ def counts_breakdown(self):
(
match(person.properties.$initial_utm_medium, '^(.*cp.*|ppc|retargeting|paid.*)$') OR
properties.$initial_gclid IS NOT NULL OR
properties.$initial_gad_source IS NOT NULL
person.properties.$initial_gclid IS NOT NULL OR
person.properties.$initial_gad_source IS NOT NULL
),
coalesce(
hogql_lookupPaidSourceType(person.properties.$initial_utm_source),
hogql_lookupPaidDomainType(person.properties.$initial_referring_domain),
if(
match(properties.$initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
match(person.properties.$initial_utm_campaign, '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Paid Shopping',
NULL
),
Expand Down Expand Up @@ -191,6 +191,68 @@ def bounce_breakdown(self):
case WebStatsBreakdown.Page:
# use initial pathname for bounce rate
return ast.Call(name="any", args=[ast.Field(chain=["person", "properties", "$initial_pathname"])])
case WebStatsBreakdown.InitialChannelType:
# use this for now, switch to person.$virt_initial_channel_type when it's working. If fixing or adding
# anything to this, keep in sync with channel_type.py
return parse_expr(
"""
multiIf(
match(any(person.properties.$initial_utm_campaign), 'cross-network'),
'Cross Network',
(
match(any(person.properties.$initial_utm_medium), '^(.*cp.*|ppc|retargeting|paid.*)$') OR
any(person.properties.$initial_gclid) IS NOT NULL OR
any(person.properties.$initial_gad_source) IS NOT NULL
),
coalesce(
hogql_lookupPaidSourceType(any(person.properties.$initial_utm_source)),
hogql_lookupPaidDomainType(any(person.properties.$initial_referring_domain)),
if(
match(any(person.properties.$initial_utm_campaign), '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Paid Shopping',
NULL
),
hogql_lookupPaidMediumType(any(person.properties.$initial_utm_medium)),
multiIf (
any(person.properties.$initial_gad_source) = '1',
'Paid Search',
match(any(person.properties.$initial_utm_campaign), '^(.*video.*)$'),
'Paid Video',
'Paid Other'
)
),
(
any(any(person.properties.$initial_referring_domain)) = '$direct'
AND (any(person.properties.$initial_utm_medium) IS NULL OR any(person.properties.$initial_utm_medium) = '')
AND (any(person.properties.$initial_utm_source) IS NULL OR any(person.properties.$initial_utm_source) IN ('', '(direct)', 'direct'))
),
'Direct',
coalesce(
hogql_lookupOrganicSourceType(any(person.properties.$initial_utm_source)),
hogql_lookupOrganicDomainType(any(person.properties.$initial_referring_domain)),
if(
match(any(person.properties.$initial_utm_campaign), '^(.*(([^a-df-z]|^)shop|shopping).*)$'),
'Organic Shopping',
NULL
),
hogql_lookupOrganicMediumType(any(person.properties.$initial_utm_medium)),
multiIf(
match(any(person.properties.$initial_utm_campaign), '^(.*video.*)$'),
'Organic Video',
match(any(person.properties.$initial_utm_medium), 'push$'),
'Push',
NULL
)
)
)"""
)
case _:
return ast.Call(name="any", args=[self.counts_breakdown()])

Expand Down
22 changes: 20 additions & 2 deletions posthog/hogql_queries/web_analytics/test/test_web_stats_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def _create_events(self, data, event="$pageview"):
)
return person_result

def _run_web_stats_table_query(self, date_from, date_to):
def _run_web_stats_table_query(self, date_from, date_to, breakdown_by=WebStatsBreakdown.Page):
query = WebStatsTableQuery(
dateRange=DateRange(date_from=date_from, date_to=date_to), properties=[], breakdownBy=WebStatsBreakdown.Page
dateRange=DateRange(date_from=date_from, date_to=date_to), properties=[], breakdownBy=breakdown_by
)
runner = WebStatsTableQueryRunner(team=self.team, query=query)
return runner.calculate()
Expand Down Expand Up @@ -93,3 +93,21 @@ def test_filter_test_accounts(self):
[],
results,
)

def test_breakdown_channel_type_doesnt_throw(self):
# not really testing the functionality yet, which is tested elsewhere, just that it runs
self._create_events(
[
("p1", [("2023-12-02", "s1a", "/"), ("2023-12-03", "s1a", "/login"), ("2023-12-13", "s1b", "/docs")]),
("p2", [("2023-12-10", "s2", "/")]),
]
)

results = self._run_web_stats_table_query(
"2023-12-01", "2023-12-03", breakdown_by=WebStatsBreakdown.InitialChannelType
).results

self.assertEqual(
[(None, 2, 1, 0)],
results,
)

0 comments on commit 2e7f4eb

Please sign in to comment.