Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web-analytics): Add logic to handle empty strings in channel type #21132

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions posthog/hogql/database/schema/channel_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def create_channel_type_expr(
gclid: ast.Expr,
gad_source: ast.Expr,
) -> ast.Expr:
def wrap_with_null_if_empty(expr: ast.Expr) -> ast.Expr:
return ast.Call(name="nullIf", args=[expr, ast.Constant(value="")])

return parse_expr(
"""
multiIf(
Expand Down Expand Up @@ -95,8 +98,8 @@ def create_channel_type_expr(

(
{referring_domain} = '$direct'
AND ({medium} IS NULL OR {medium} = '')
AND ({source} IS NULL OR {source} IN ('', '(direct)', 'direct'))
AND ({medium} IS NULL)
AND ({source} IS NULL OR {source} IN ('(direct)', 'direct'))
),
'Direct',

Expand All @@ -122,11 +125,11 @@ def create_channel_type_expr(
)""",
start=None,
placeholders={
"campaign": campaign,
"medium": medium,
"source": source,
"campaign": wrap_with_null_if_empty(campaign),
"medium": wrap_with_null_if_empty(medium),
"source": wrap_with_null_if_empty(source),
"referring_domain": referring_domain,
"gclid": gclid,
"gad_source": gad_source,
"gclid": wrap_with_null_if_empty(gclid),
"gad_source": wrap_with_null_if_empty(gad_source),
},
)
15 changes: 15 additions & 0 deletions posthog/hogql/database/schema/test/test_channel_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ def test_direct(self):
),
)

def test_direct_empty_string(self):
self.assertEqual(
"Direct",
self._get_initial_channel_type(
{
"$initial_referring_domain": "$direct",
"$initial_utm_source": "",
"$initial_utm_medium": "",
"$initial_utm_campaign": "",
"$initial_gclid": "",
"$initial_gad_source": "",
}
),
)

def test_cross_network(self):
self.assertEqual(
"Cross Network",
Expand Down
Loading