Skip to content

Commit

Permalink
fix(web-analytics): Add logic to handle empty strings in channel type (
Browse files Browse the repository at this point in the history
…#21132)

Add logic to handle empty strings in channel type
  • Loading branch information
robbie-c authored Mar 25, 2024
1 parent 84becf9 commit a1f7c6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
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

0 comments on commit a1f7c6d

Please sign in to comment.